dm-thin.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011-2012 Red Hat UK.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-thin-metadata.h"
  8. #include "dm-bio-prison-v1.h"
  9. #include "dm.h"
  10. #include <linux/device-mapper.h>
  11. #include <linux/dm-io.h>
  12. #include <linux/dm-kcopyd.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/log2.h>
  15. #include <linux/list.h>
  16. #include <linux/rculist.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/sort.h>
  22. #include <linux/rbtree.h>
  23. #define DM_MSG_PREFIX "thin"
  24. /*
  25. * Tunable constants
  26. */
  27. #define ENDIO_HOOK_POOL_SIZE 1024
  28. #define MAPPING_POOL_SIZE 1024
  29. #define COMMIT_PERIOD HZ
  30. #define NO_SPACE_TIMEOUT_SECS 60
  31. static unsigned int no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
  32. DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
  33. "A percentage of time allocated for copy on write");
  34. /*
  35. * The block size of the device holding pool data must be
  36. * between 64KB and 1GB.
  37. */
  38. #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
  39. #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
  40. /*
  41. * Device id is restricted to 24 bits.
  42. */
  43. #define MAX_DEV_ID ((1 << 24) - 1)
  44. /*
  45. * How do we handle breaking sharing of data blocks?
  46. * =================================================
  47. *
  48. * We use a standard copy-on-write btree to store the mappings for the
  49. * devices (note I'm talking about copy-on-write of the metadata here, not
  50. * the data). When you take an internal snapshot you clone the root node
  51. * of the origin btree. After this there is no concept of an origin or a
  52. * snapshot. They are just two device trees that happen to point to the
  53. * same data blocks.
  54. *
  55. * When we get a write in we decide if it's to a shared data block using
  56. * some timestamp magic. If it is, we have to break sharing.
  57. *
  58. * Let's say we write to a shared block in what was the origin. The
  59. * steps are:
  60. *
  61. * i) plug io further to this physical block. (see bio_prison code).
  62. *
  63. * ii) quiesce any read io to that shared data block. Obviously
  64. * including all devices that share this block. (see dm_deferred_set code)
  65. *
  66. * iii) copy the data block to a newly allocate block. This step can be
  67. * missed out if the io covers the block. (schedule_copy).
  68. *
  69. * iv) insert the new mapping into the origin's btree
  70. * (process_prepared_mapping). This act of inserting breaks some
  71. * sharing of btree nodes between the two devices. Breaking sharing only
  72. * effects the btree of that specific device. Btrees for the other
  73. * devices that share the block never change. The btree for the origin
  74. * device as it was after the last commit is untouched, ie. we're using
  75. * persistent data structures in the functional programming sense.
  76. *
  77. * v) unplug io to this physical block, including the io that triggered
  78. * the breaking of sharing.
  79. *
  80. * Steps (ii) and (iii) occur in parallel.
  81. *
  82. * The metadata _doesn't_ need to be committed before the io continues. We
  83. * get away with this because the io is always written to a _new_ block.
  84. * If there's a crash, then:
  85. *
  86. * - The origin mapping will point to the old origin block (the shared
  87. * one). This will contain the data as it was before the io that triggered
  88. * the breaking of sharing came in.
  89. *
  90. * - The snap mapping still points to the old block. As it would after
  91. * the commit.
  92. *
  93. * The downside of this scheme is the timestamp magic isn't perfect, and
  94. * will continue to think that data block in the snapshot device is shared
  95. * even after the write to the origin has broken sharing. I suspect data
  96. * blocks will typically be shared by many different devices, so we're
  97. * breaking sharing n + 1 times, rather than n, where n is the number of
  98. * devices that reference this data block. At the moment I think the
  99. * benefits far, far outweigh the disadvantages.
  100. */
  101. /*----------------------------------------------------------------*/
  102. /*
  103. * Key building.
  104. */
  105. enum lock_space {
  106. VIRTUAL,
  107. PHYSICAL
  108. };
  109. static bool build_key(struct dm_thin_device *td, enum lock_space ls,
  110. dm_block_t b, dm_block_t e, struct dm_cell_key *key)
  111. {
  112. key->virtual = (ls == VIRTUAL);
  113. key->dev = dm_thin_dev_id(td);
  114. key->block_begin = b;
  115. key->block_end = e;
  116. return dm_cell_key_has_valid_range(key);
  117. }
  118. static void build_data_key(struct dm_thin_device *td, dm_block_t b,
  119. struct dm_cell_key *key)
  120. {
  121. (void) build_key(td, PHYSICAL, b, b + 1llu, key);
  122. }
  123. static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
  124. struct dm_cell_key *key)
  125. {
  126. (void) build_key(td, VIRTUAL, b, b + 1llu, key);
  127. }
  128. /*----------------------------------------------------------------*/
  129. #define THROTTLE_THRESHOLD (1 * HZ)
  130. struct throttle {
  131. struct rw_semaphore lock;
  132. unsigned long threshold;
  133. bool throttle_applied;
  134. };
  135. static void throttle_init(struct throttle *t)
  136. {
  137. init_rwsem(&t->lock);
  138. t->throttle_applied = false;
  139. }
  140. static void throttle_work_start(struct throttle *t)
  141. {
  142. t->threshold = jiffies + THROTTLE_THRESHOLD;
  143. }
  144. static void throttle_work_update(struct throttle *t)
  145. {
  146. if (!t->throttle_applied && time_is_before_jiffies(t->threshold)) {
  147. down_write(&t->lock);
  148. t->throttle_applied = true;
  149. }
  150. }
  151. static void throttle_work_complete(struct throttle *t)
  152. {
  153. if (t->throttle_applied) {
  154. t->throttle_applied = false;
  155. up_write(&t->lock);
  156. }
  157. }
  158. static void throttle_lock(struct throttle *t)
  159. {
  160. down_read(&t->lock);
  161. }
  162. static void throttle_unlock(struct throttle *t)
  163. {
  164. up_read(&t->lock);
  165. }
  166. /*----------------------------------------------------------------*/
  167. /*
  168. * A pool device ties together a metadata device and a data device. It
  169. * also provides the interface for creating and destroying internal
  170. * devices.
  171. */
  172. struct dm_thin_new_mapping;
  173. /*
  174. * The pool runs in various modes. Ordered in degraded order for comparisons.
  175. */
  176. enum pool_mode {
  177. PM_WRITE, /* metadata may be changed */
  178. PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
  179. /*
  180. * Like READ_ONLY, except may switch back to WRITE on metadata resize. Reported as READ_ONLY.
  181. */
  182. PM_OUT_OF_METADATA_SPACE,
  183. PM_READ_ONLY, /* metadata may not be changed */
  184. PM_FAIL, /* all I/O fails */
  185. };
  186. struct pool_features {
  187. enum pool_mode mode;
  188. bool zero_new_blocks:1;
  189. bool discard_enabled:1;
  190. bool discard_passdown:1;
  191. bool error_if_no_space:1;
  192. };
  193. struct thin_c;
  194. typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
  195. typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
  196. typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
  197. #define CELL_SORT_ARRAY_SIZE 8192
  198. struct pool {
  199. struct list_head list;
  200. struct dm_target *ti; /* Only set if a pool target is bound */
  201. struct mapped_device *pool_md;
  202. struct block_device *data_dev;
  203. struct block_device *md_dev;
  204. struct dm_pool_metadata *pmd;
  205. dm_block_t low_water_blocks;
  206. uint32_t sectors_per_block;
  207. int sectors_per_block_shift;
  208. struct pool_features pf;
  209. bool low_water_triggered:1; /* A dm event has been sent */
  210. bool suspended:1;
  211. bool out_of_data_space:1;
  212. struct dm_bio_prison *prison;
  213. struct dm_kcopyd_client *copier;
  214. struct work_struct worker;
  215. struct workqueue_struct *wq;
  216. struct throttle throttle;
  217. struct delayed_work waker;
  218. struct delayed_work no_space_timeout;
  219. unsigned long last_commit_jiffies;
  220. unsigned int ref_count;
  221. spinlock_t lock;
  222. struct bio_list deferred_flush_bios;
  223. struct bio_list deferred_flush_completions;
  224. struct list_head prepared_mappings;
  225. struct list_head prepared_discards;
  226. struct list_head prepared_discards_pt2;
  227. struct list_head active_thins;
  228. struct dm_deferred_set *shared_read_ds;
  229. struct dm_deferred_set *all_io_ds;
  230. struct dm_thin_new_mapping *next_mapping;
  231. process_bio_fn process_bio;
  232. process_bio_fn process_discard;
  233. process_cell_fn process_cell;
  234. process_cell_fn process_discard_cell;
  235. process_mapping_fn process_prepared_mapping;
  236. process_mapping_fn process_prepared_discard;
  237. process_mapping_fn process_prepared_discard_pt2;
  238. struct dm_bio_prison_cell **cell_sort_array;
  239. mempool_t mapping_pool;
  240. };
  241. static void metadata_operation_failed(struct pool *pool, const char *op, int r);
  242. static enum pool_mode get_pool_mode(struct pool *pool)
  243. {
  244. return pool->pf.mode;
  245. }
  246. static void notify_of_pool_mode_change(struct pool *pool)
  247. {
  248. static const char *descs[] = {
  249. "write",
  250. "out-of-data-space",
  251. "read-only",
  252. "read-only",
  253. "fail"
  254. };
  255. const char *extra_desc = NULL;
  256. enum pool_mode mode = get_pool_mode(pool);
  257. if (mode == PM_OUT_OF_DATA_SPACE) {
  258. if (!pool->pf.error_if_no_space)
  259. extra_desc = " (queue IO)";
  260. else
  261. extra_desc = " (error IO)";
  262. }
  263. dm_table_event(pool->ti->table);
  264. DMINFO("%s: switching pool to %s%s mode",
  265. dm_device_name(pool->pool_md),
  266. descs[(int)mode], extra_desc ? : "");
  267. }
  268. /*
  269. * Target context for a pool.
  270. */
  271. struct pool_c {
  272. struct dm_target *ti;
  273. struct pool *pool;
  274. struct dm_dev *data_dev;
  275. struct dm_dev *metadata_dev;
  276. dm_block_t low_water_blocks;
  277. struct pool_features requested_pf; /* Features requested during table load */
  278. struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
  279. };
  280. /*
  281. * Target context for a thin.
  282. */
  283. struct thin_c {
  284. struct list_head list;
  285. struct dm_dev *pool_dev;
  286. struct dm_dev *origin_dev;
  287. sector_t origin_size;
  288. dm_thin_id dev_id;
  289. struct pool *pool;
  290. struct dm_thin_device *td;
  291. struct mapped_device *thin_md;
  292. bool requeue_mode:1;
  293. spinlock_t lock;
  294. struct list_head deferred_cells;
  295. struct bio_list deferred_bio_list;
  296. struct bio_list retry_on_resume_list;
  297. struct rb_root sort_bio_list; /* sorted list of deferred bios */
  298. /*
  299. * Ensures the thin is not destroyed until the worker has finished
  300. * iterating the active_thins list.
  301. */
  302. refcount_t refcount;
  303. struct completion can_destroy;
  304. };
  305. /*----------------------------------------------------------------*/
  306. static bool block_size_is_power_of_two(struct pool *pool)
  307. {
  308. return pool->sectors_per_block_shift >= 0;
  309. }
  310. static sector_t block_to_sectors(struct pool *pool, dm_block_t b)
  311. {
  312. return block_size_is_power_of_two(pool) ?
  313. (b << pool->sectors_per_block_shift) :
  314. (b * pool->sectors_per_block);
  315. }
  316. /*----------------------------------------------------------------*/
  317. struct discard_op {
  318. struct thin_c *tc;
  319. struct blk_plug plug;
  320. struct bio *parent_bio;
  321. struct bio *bio;
  322. };
  323. static void begin_discard(struct discard_op *op, struct thin_c *tc, struct bio *parent)
  324. {
  325. BUG_ON(!parent);
  326. op->tc = tc;
  327. blk_start_plug(&op->plug);
  328. op->parent_bio = parent;
  329. op->bio = NULL;
  330. }
  331. static int issue_discard(struct discard_op *op, dm_block_t data_b, dm_block_t data_e)
  332. {
  333. struct thin_c *tc = op->tc;
  334. sector_t s = block_to_sectors(tc->pool, data_b);
  335. sector_t len = block_to_sectors(tc->pool, data_e - data_b);
  336. return __blkdev_issue_discard(tc->pool_dev->bdev, s, len, GFP_NOIO, &op->bio);
  337. }
  338. static void end_discard(struct discard_op *op, int r)
  339. {
  340. if (op->bio) {
  341. /*
  342. * Even if one of the calls to issue_discard failed, we
  343. * need to wait for the chain to complete.
  344. */
  345. bio_chain(op->bio, op->parent_bio);
  346. op->bio->bi_opf = REQ_OP_DISCARD;
  347. submit_bio(op->bio);
  348. }
  349. blk_finish_plug(&op->plug);
  350. /*
  351. * Even if r is set, there could be sub discards in flight that we
  352. * need to wait for.
  353. */
  354. if (r && !op->parent_bio->bi_status)
  355. op->parent_bio->bi_status = errno_to_blk_status(r);
  356. bio_endio(op->parent_bio);
  357. }
  358. /*----------------------------------------------------------------*/
  359. /*
  360. * wake_worker() is used when new work is queued and when pool_resume is
  361. * ready to continue deferred IO processing.
  362. */
  363. static void wake_worker(struct pool *pool)
  364. {
  365. queue_work(pool->wq, &pool->worker);
  366. }
  367. /*----------------------------------------------------------------*/
  368. static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
  369. struct dm_bio_prison_cell **cell_result)
  370. {
  371. int r;
  372. struct dm_bio_prison_cell *cell_prealloc;
  373. /*
  374. * Allocate a cell from the prison's mempool.
  375. * This might block but it can't fail.
  376. */
  377. cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
  378. r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
  379. if (r) {
  380. /*
  381. * We reused an old cell; we can get rid of
  382. * the new one.
  383. */
  384. dm_bio_prison_free_cell(pool->prison, cell_prealloc);
  385. }
  386. return r;
  387. }
  388. static void cell_release(struct pool *pool,
  389. struct dm_bio_prison_cell *cell,
  390. struct bio_list *bios)
  391. {
  392. dm_cell_release(pool->prison, cell, bios);
  393. dm_bio_prison_free_cell(pool->prison, cell);
  394. }
  395. static void cell_visit_release(struct pool *pool,
  396. void (*fn)(void *, struct dm_bio_prison_cell *),
  397. void *context,
  398. struct dm_bio_prison_cell *cell)
  399. {
  400. dm_cell_visit_release(pool->prison, fn, context, cell);
  401. dm_bio_prison_free_cell(pool->prison, cell);
  402. }
  403. static void cell_release_no_holder(struct pool *pool,
  404. struct dm_bio_prison_cell *cell,
  405. struct bio_list *bios)
  406. {
  407. dm_cell_release_no_holder(pool->prison, cell, bios);
  408. dm_bio_prison_free_cell(pool->prison, cell);
  409. }
  410. static void cell_error_with_code(struct pool *pool,
  411. struct dm_bio_prison_cell *cell, blk_status_t error_code)
  412. {
  413. dm_cell_error(pool->prison, cell, error_code);
  414. dm_bio_prison_free_cell(pool->prison, cell);
  415. }
  416. static blk_status_t get_pool_io_error_code(struct pool *pool)
  417. {
  418. return pool->out_of_data_space ? BLK_STS_NOSPC : BLK_STS_IOERR;
  419. }
  420. static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
  421. {
  422. cell_error_with_code(pool, cell, get_pool_io_error_code(pool));
  423. }
  424. static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
  425. {
  426. cell_error_with_code(pool, cell, 0);
  427. }
  428. static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
  429. {
  430. cell_error_with_code(pool, cell, BLK_STS_DM_REQUEUE);
  431. }
  432. /*----------------------------------------------------------------*/
  433. /*
  434. * A global list of pools that uses a struct mapped_device as a key.
  435. */
  436. static struct dm_thin_pool_table {
  437. struct mutex mutex;
  438. struct list_head pools;
  439. } dm_thin_pool_table;
  440. static void pool_table_init(void)
  441. {
  442. mutex_init(&dm_thin_pool_table.mutex);
  443. INIT_LIST_HEAD(&dm_thin_pool_table.pools);
  444. }
  445. static void pool_table_exit(void)
  446. {
  447. mutex_destroy(&dm_thin_pool_table.mutex);
  448. }
  449. static void __pool_table_insert(struct pool *pool)
  450. {
  451. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  452. list_add(&pool->list, &dm_thin_pool_table.pools);
  453. }
  454. static void __pool_table_remove(struct pool *pool)
  455. {
  456. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  457. list_del(&pool->list);
  458. }
  459. static struct pool *__pool_table_lookup(struct mapped_device *md)
  460. {
  461. struct pool *pool = NULL, *tmp;
  462. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  463. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  464. if (tmp->pool_md == md) {
  465. pool = tmp;
  466. break;
  467. }
  468. }
  469. return pool;
  470. }
  471. static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
  472. {
  473. struct pool *pool = NULL, *tmp;
  474. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  475. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  476. if (tmp->md_dev == md_dev) {
  477. pool = tmp;
  478. break;
  479. }
  480. }
  481. return pool;
  482. }
  483. /*----------------------------------------------------------------*/
  484. struct dm_thin_endio_hook {
  485. struct thin_c *tc;
  486. struct dm_deferred_entry *shared_read_entry;
  487. struct dm_deferred_entry *all_io_entry;
  488. struct dm_thin_new_mapping *overwrite_mapping;
  489. struct rb_node rb_node;
  490. struct dm_bio_prison_cell *cell;
  491. };
  492. static void error_bio_list(struct bio_list *bios, blk_status_t error)
  493. {
  494. struct bio *bio;
  495. while ((bio = bio_list_pop(bios))) {
  496. bio->bi_status = error;
  497. bio_endio(bio);
  498. }
  499. }
  500. static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master,
  501. blk_status_t error)
  502. {
  503. struct bio_list bios;
  504. bio_list_init(&bios);
  505. spin_lock_irq(&tc->lock);
  506. bio_list_merge_init(&bios, master);
  507. spin_unlock_irq(&tc->lock);
  508. error_bio_list(&bios, error);
  509. }
  510. static void requeue_deferred_cells(struct thin_c *tc)
  511. {
  512. struct pool *pool = tc->pool;
  513. struct list_head cells;
  514. struct dm_bio_prison_cell *cell, *tmp;
  515. INIT_LIST_HEAD(&cells);
  516. spin_lock_irq(&tc->lock);
  517. list_splice_init(&tc->deferred_cells, &cells);
  518. spin_unlock_irq(&tc->lock);
  519. list_for_each_entry_safe(cell, tmp, &cells, user_list)
  520. cell_requeue(pool, cell);
  521. }
  522. static void requeue_io(struct thin_c *tc)
  523. {
  524. struct bio_list bios;
  525. bio_list_init(&bios);
  526. spin_lock_irq(&tc->lock);
  527. bio_list_merge_init(&bios, &tc->deferred_bio_list);
  528. bio_list_merge_init(&bios, &tc->retry_on_resume_list);
  529. spin_unlock_irq(&tc->lock);
  530. error_bio_list(&bios, BLK_STS_DM_REQUEUE);
  531. requeue_deferred_cells(tc);
  532. }
  533. static void error_retry_list_with_code(struct pool *pool, blk_status_t error)
  534. {
  535. struct thin_c *tc;
  536. rcu_read_lock();
  537. list_for_each_entry_rcu(tc, &pool->active_thins, list)
  538. error_thin_bio_list(tc, &tc->retry_on_resume_list, error);
  539. rcu_read_unlock();
  540. }
  541. static void error_retry_list(struct pool *pool)
  542. {
  543. error_retry_list_with_code(pool, get_pool_io_error_code(pool));
  544. }
  545. /*
  546. * This section of code contains the logic for processing a thin device's IO.
  547. * Much of the code depends on pool object resources (lists, workqueues, etc)
  548. * but most is exclusively called from the thin target rather than the thin-pool
  549. * target.
  550. */
  551. static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
  552. {
  553. struct pool *pool = tc->pool;
  554. sector_t block_nr = bio->bi_iter.bi_sector;
  555. if (block_size_is_power_of_two(pool))
  556. block_nr >>= pool->sectors_per_block_shift;
  557. else
  558. (void) sector_div(block_nr, pool->sectors_per_block);
  559. return block_nr;
  560. }
  561. /*
  562. * Returns the _complete_ blocks that this bio covers.
  563. */
  564. static void get_bio_block_range(struct thin_c *tc, struct bio *bio,
  565. dm_block_t *begin, dm_block_t *end)
  566. {
  567. struct pool *pool = tc->pool;
  568. sector_t b = bio->bi_iter.bi_sector;
  569. sector_t e = b + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
  570. b += pool->sectors_per_block - 1ull; /* so we round up */
  571. if (block_size_is_power_of_two(pool)) {
  572. b >>= pool->sectors_per_block_shift;
  573. e >>= pool->sectors_per_block_shift;
  574. } else {
  575. (void) sector_div(b, pool->sectors_per_block);
  576. (void) sector_div(e, pool->sectors_per_block);
  577. }
  578. if (e < b) {
  579. /* Can happen if the bio is within a single block. */
  580. e = b;
  581. }
  582. *begin = b;
  583. *end = e;
  584. }
  585. static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
  586. {
  587. struct pool *pool = tc->pool;
  588. sector_t bi_sector = bio->bi_iter.bi_sector;
  589. bio_set_dev(bio, tc->pool_dev->bdev);
  590. if (block_size_is_power_of_two(pool)) {
  591. bio->bi_iter.bi_sector =
  592. (block << pool->sectors_per_block_shift) |
  593. (bi_sector & (pool->sectors_per_block - 1));
  594. } else {
  595. bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
  596. sector_div(bi_sector, pool->sectors_per_block);
  597. }
  598. }
  599. static void remap_to_origin(struct thin_c *tc, struct bio *bio)
  600. {
  601. bio_set_dev(bio, tc->origin_dev->bdev);
  602. }
  603. static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
  604. {
  605. return op_is_flush(bio->bi_opf) &&
  606. dm_thin_changed_this_transaction(tc->td);
  607. }
  608. static void inc_all_io_entry(struct pool *pool, struct bio *bio)
  609. {
  610. struct dm_thin_endio_hook *h;
  611. if (bio_op(bio) == REQ_OP_DISCARD)
  612. return;
  613. h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  614. h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
  615. }
  616. static void issue(struct thin_c *tc, struct bio *bio)
  617. {
  618. struct pool *pool = tc->pool;
  619. if (!bio_triggers_commit(tc, bio)) {
  620. dm_submit_bio_remap(bio, NULL);
  621. return;
  622. }
  623. /*
  624. * Complete bio with an error if earlier I/O caused changes to
  625. * the metadata that can't be committed e.g, due to I/O errors
  626. * on the metadata device.
  627. */
  628. if (dm_thin_aborted_changes(tc->td)) {
  629. bio_io_error(bio);
  630. return;
  631. }
  632. /*
  633. * Batch together any bios that trigger commits and then issue a
  634. * single commit for them in process_deferred_bios().
  635. */
  636. spin_lock_irq(&pool->lock);
  637. bio_list_add(&pool->deferred_flush_bios, bio);
  638. spin_unlock_irq(&pool->lock);
  639. }
  640. static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
  641. {
  642. remap_to_origin(tc, bio);
  643. issue(tc, bio);
  644. }
  645. static void remap_and_issue(struct thin_c *tc, struct bio *bio,
  646. dm_block_t block)
  647. {
  648. remap(tc, bio, block);
  649. issue(tc, bio);
  650. }
  651. /*----------------------------------------------------------------*/
  652. /*
  653. * Bio endio functions.
  654. */
  655. struct dm_thin_new_mapping {
  656. struct list_head list;
  657. bool pass_discard:1;
  658. bool maybe_shared:1;
  659. /*
  660. * Track quiescing, copying and zeroing preparation actions. When this
  661. * counter hits zero the block is prepared and can be inserted into the
  662. * btree.
  663. */
  664. atomic_t prepare_actions;
  665. blk_status_t status;
  666. struct thin_c *tc;
  667. dm_block_t virt_begin, virt_end;
  668. dm_block_t data_block;
  669. struct dm_bio_prison_cell *cell;
  670. /*
  671. * If the bio covers the whole area of a block then we can avoid
  672. * zeroing or copying. Instead this bio is hooked. The bio will
  673. * still be in the cell, so care has to be taken to avoid issuing
  674. * the bio twice.
  675. */
  676. struct bio *bio;
  677. bio_end_io_t *saved_bi_end_io;
  678. };
  679. static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
  680. {
  681. struct pool *pool = m->tc->pool;
  682. if (atomic_dec_and_test(&m->prepare_actions)) {
  683. list_add_tail(&m->list, &pool->prepared_mappings);
  684. wake_worker(pool);
  685. }
  686. }
  687. static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
  688. {
  689. unsigned long flags;
  690. struct pool *pool = m->tc->pool;
  691. spin_lock_irqsave(&pool->lock, flags);
  692. __complete_mapping_preparation(m);
  693. spin_unlock_irqrestore(&pool->lock, flags);
  694. }
  695. static void copy_complete(int read_err, unsigned long write_err, void *context)
  696. {
  697. struct dm_thin_new_mapping *m = context;
  698. m->status = read_err || write_err ? BLK_STS_IOERR : 0;
  699. complete_mapping_preparation(m);
  700. }
  701. static void overwrite_endio(struct bio *bio)
  702. {
  703. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  704. struct dm_thin_new_mapping *m = h->overwrite_mapping;
  705. bio->bi_end_io = m->saved_bi_end_io;
  706. m->status = bio->bi_status;
  707. complete_mapping_preparation(m);
  708. }
  709. /*----------------------------------------------------------------*/
  710. /*
  711. * Workqueue.
  712. */
  713. /*
  714. * Prepared mapping jobs.
  715. */
  716. /*
  717. * This sends the bios in the cell, except the original holder, back
  718. * to the deferred_bios list.
  719. */
  720. static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  721. {
  722. struct pool *pool = tc->pool;
  723. unsigned long flags;
  724. struct bio_list bios;
  725. bio_list_init(&bios);
  726. cell_release_no_holder(pool, cell, &bios);
  727. if (!bio_list_empty(&bios)) {
  728. spin_lock_irqsave(&tc->lock, flags);
  729. bio_list_merge(&tc->deferred_bio_list, &bios);
  730. spin_unlock_irqrestore(&tc->lock, flags);
  731. wake_worker(pool);
  732. }
  733. }
  734. static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
  735. struct remap_info {
  736. struct thin_c *tc;
  737. struct bio_list defer_bios;
  738. struct bio_list issue_bios;
  739. };
  740. static void __inc_remap_and_issue_cell(void *context,
  741. struct dm_bio_prison_cell *cell)
  742. {
  743. struct remap_info *info = context;
  744. struct bio *bio;
  745. while ((bio = bio_list_pop(&cell->bios))) {
  746. if (op_is_flush(bio->bi_opf) || bio_op(bio) == REQ_OP_DISCARD)
  747. bio_list_add(&info->defer_bios, bio);
  748. else {
  749. inc_all_io_entry(info->tc->pool, bio);
  750. /*
  751. * We can't issue the bios with the bio prison lock
  752. * held, so we add them to a list to issue on
  753. * return from this function.
  754. */
  755. bio_list_add(&info->issue_bios, bio);
  756. }
  757. }
  758. }
  759. static void inc_remap_and_issue_cell(struct thin_c *tc,
  760. struct dm_bio_prison_cell *cell,
  761. dm_block_t block)
  762. {
  763. struct bio *bio;
  764. struct remap_info info;
  765. info.tc = tc;
  766. bio_list_init(&info.defer_bios);
  767. bio_list_init(&info.issue_bios);
  768. /*
  769. * We have to be careful to inc any bios we're about to issue
  770. * before the cell is released, and avoid a race with new bios
  771. * being added to the cell.
  772. */
  773. cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
  774. &info, cell);
  775. while ((bio = bio_list_pop(&info.defer_bios)))
  776. thin_defer_bio(tc, bio);
  777. while ((bio = bio_list_pop(&info.issue_bios)))
  778. remap_and_issue(info.tc, bio, block);
  779. }
  780. static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
  781. {
  782. cell_error(m->tc->pool, m->cell);
  783. list_del(&m->list);
  784. mempool_free(m, &m->tc->pool->mapping_pool);
  785. }
  786. static void complete_overwrite_bio(struct thin_c *tc, struct bio *bio)
  787. {
  788. struct pool *pool = tc->pool;
  789. /*
  790. * If the bio has the REQ_FUA flag set we must commit the metadata
  791. * before signaling its completion.
  792. */
  793. if (!bio_triggers_commit(tc, bio)) {
  794. bio_endio(bio);
  795. return;
  796. }
  797. /*
  798. * Complete bio with an error if earlier I/O caused changes to the
  799. * metadata that can't be committed, e.g, due to I/O errors on the
  800. * metadata device.
  801. */
  802. if (dm_thin_aborted_changes(tc->td)) {
  803. bio_io_error(bio);
  804. return;
  805. }
  806. /*
  807. * Batch together any bios that trigger commits and then issue a
  808. * single commit for them in process_deferred_bios().
  809. */
  810. spin_lock_irq(&pool->lock);
  811. bio_list_add(&pool->deferred_flush_completions, bio);
  812. spin_unlock_irq(&pool->lock);
  813. }
  814. static void process_prepared_mapping(struct dm_thin_new_mapping *m)
  815. {
  816. struct thin_c *tc = m->tc;
  817. struct pool *pool = tc->pool;
  818. struct bio *bio = m->bio;
  819. int r;
  820. if (m->status) {
  821. cell_error(pool, m->cell);
  822. goto out;
  823. }
  824. /*
  825. * Commit the prepared block into the mapping btree.
  826. * Any I/O for this block arriving after this point will get
  827. * remapped to it directly.
  828. */
  829. r = dm_thin_insert_block(tc->td, m->virt_begin, m->data_block);
  830. if (r) {
  831. metadata_operation_failed(pool, "dm_thin_insert_block", r);
  832. cell_error(pool, m->cell);
  833. goto out;
  834. }
  835. /*
  836. * Release any bios held while the block was being provisioned.
  837. * If we are processing a write bio that completely covers the block,
  838. * we already processed it so can ignore it now when processing
  839. * the bios in the cell.
  840. */
  841. if (bio) {
  842. inc_remap_and_issue_cell(tc, m->cell, m->data_block);
  843. complete_overwrite_bio(tc, bio);
  844. } else {
  845. inc_all_io_entry(tc->pool, m->cell->holder);
  846. remap_and_issue(tc, m->cell->holder, m->data_block);
  847. inc_remap_and_issue_cell(tc, m->cell, m->data_block);
  848. }
  849. out:
  850. list_del(&m->list);
  851. mempool_free(m, &pool->mapping_pool);
  852. }
  853. /*----------------------------------------------------------------*/
  854. static void free_discard_mapping(struct dm_thin_new_mapping *m)
  855. {
  856. struct thin_c *tc = m->tc;
  857. if (m->cell)
  858. cell_defer_no_holder(tc, m->cell);
  859. mempool_free(m, &tc->pool->mapping_pool);
  860. }
  861. static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
  862. {
  863. bio_io_error(m->bio);
  864. free_discard_mapping(m);
  865. }
  866. static void process_prepared_discard_success(struct dm_thin_new_mapping *m)
  867. {
  868. bio_endio(m->bio);
  869. free_discard_mapping(m);
  870. }
  871. static void process_prepared_discard_no_passdown(struct dm_thin_new_mapping *m)
  872. {
  873. int r;
  874. struct thin_c *tc = m->tc;
  875. r = dm_thin_remove_range(tc->td, m->cell->key.block_begin, m->cell->key.block_end);
  876. if (r) {
  877. metadata_operation_failed(tc->pool, "dm_thin_remove_range", r);
  878. bio_io_error(m->bio);
  879. } else
  880. bio_endio(m->bio);
  881. cell_defer_no_holder(tc, m->cell);
  882. mempool_free(m, &tc->pool->mapping_pool);
  883. }
  884. /*----------------------------------------------------------------*/
  885. static void passdown_double_checking_shared_status(struct dm_thin_new_mapping *m,
  886. struct bio *discard_parent)
  887. {
  888. /*
  889. * We've already unmapped this range of blocks, but before we
  890. * passdown we have to check that these blocks are now unused.
  891. */
  892. int r = 0;
  893. bool shared = true;
  894. struct thin_c *tc = m->tc;
  895. struct pool *pool = tc->pool;
  896. dm_block_t b = m->data_block, e, end = m->data_block + m->virt_end - m->virt_begin;
  897. struct discard_op op;
  898. begin_discard(&op, tc, discard_parent);
  899. while (b != end) {
  900. /* find start of unmapped run */
  901. for (; b < end; b++) {
  902. r = dm_pool_block_is_shared(pool->pmd, b, &shared);
  903. if (r)
  904. goto out;
  905. if (!shared)
  906. break;
  907. }
  908. if (b == end)
  909. break;
  910. /* find end of run */
  911. for (e = b + 1; e != end; e++) {
  912. r = dm_pool_block_is_shared(pool->pmd, e, &shared);
  913. if (r)
  914. goto out;
  915. if (shared)
  916. break;
  917. }
  918. r = issue_discard(&op, b, e);
  919. if (r)
  920. goto out;
  921. b = e;
  922. }
  923. out:
  924. end_discard(&op, r);
  925. }
  926. static void queue_passdown_pt2(struct dm_thin_new_mapping *m)
  927. {
  928. unsigned long flags;
  929. struct pool *pool = m->tc->pool;
  930. spin_lock_irqsave(&pool->lock, flags);
  931. list_add_tail(&m->list, &pool->prepared_discards_pt2);
  932. spin_unlock_irqrestore(&pool->lock, flags);
  933. wake_worker(pool);
  934. }
  935. static void passdown_endio(struct bio *bio)
  936. {
  937. /*
  938. * It doesn't matter if the passdown discard failed, we still want
  939. * to unmap (we ignore err).
  940. */
  941. queue_passdown_pt2(bio->bi_private);
  942. bio_put(bio);
  943. }
  944. static void process_prepared_discard_passdown_pt1(struct dm_thin_new_mapping *m)
  945. {
  946. int r;
  947. struct thin_c *tc = m->tc;
  948. struct pool *pool = tc->pool;
  949. struct bio *discard_parent;
  950. dm_block_t data_end = m->data_block + (m->virt_end - m->virt_begin);
  951. /*
  952. * Only this thread allocates blocks, so we can be sure that the
  953. * newly unmapped blocks will not be allocated before the end of
  954. * the function.
  955. */
  956. r = dm_thin_remove_range(tc->td, m->virt_begin, m->virt_end);
  957. if (r) {
  958. metadata_operation_failed(pool, "dm_thin_remove_range", r);
  959. bio_io_error(m->bio);
  960. cell_defer_no_holder(tc, m->cell);
  961. mempool_free(m, &pool->mapping_pool);
  962. return;
  963. }
  964. /*
  965. * Increment the unmapped blocks. This prevents a race between the
  966. * passdown io and reallocation of freed blocks.
  967. */
  968. r = dm_pool_inc_data_range(pool->pmd, m->data_block, data_end);
  969. if (r) {
  970. metadata_operation_failed(pool, "dm_pool_inc_data_range", r);
  971. bio_io_error(m->bio);
  972. cell_defer_no_holder(tc, m->cell);
  973. mempool_free(m, &pool->mapping_pool);
  974. return;
  975. }
  976. discard_parent = bio_alloc(NULL, 1, 0, GFP_NOIO);
  977. discard_parent->bi_end_io = passdown_endio;
  978. discard_parent->bi_private = m;
  979. if (m->maybe_shared)
  980. passdown_double_checking_shared_status(m, discard_parent);
  981. else {
  982. struct discard_op op;
  983. begin_discard(&op, tc, discard_parent);
  984. r = issue_discard(&op, m->data_block, data_end);
  985. end_discard(&op, r);
  986. }
  987. }
  988. static void process_prepared_discard_passdown_pt2(struct dm_thin_new_mapping *m)
  989. {
  990. int r;
  991. struct thin_c *tc = m->tc;
  992. struct pool *pool = tc->pool;
  993. /*
  994. * The passdown has completed, so now we can decrement all those
  995. * unmapped blocks.
  996. */
  997. r = dm_pool_dec_data_range(pool->pmd, m->data_block,
  998. m->data_block + (m->virt_end - m->virt_begin));
  999. if (r) {
  1000. metadata_operation_failed(pool, "dm_pool_dec_data_range", r);
  1001. bio_io_error(m->bio);
  1002. } else
  1003. bio_endio(m->bio);
  1004. cell_defer_no_holder(tc, m->cell);
  1005. mempool_free(m, &pool->mapping_pool);
  1006. }
  1007. static void process_prepared(struct pool *pool, struct list_head *head,
  1008. process_mapping_fn *fn)
  1009. {
  1010. struct list_head maps;
  1011. struct dm_thin_new_mapping *m, *tmp;
  1012. INIT_LIST_HEAD(&maps);
  1013. spin_lock_irq(&pool->lock);
  1014. list_splice_init(head, &maps);
  1015. spin_unlock_irq(&pool->lock);
  1016. list_for_each_entry_safe(m, tmp, &maps, list)
  1017. (*fn)(m);
  1018. }
  1019. /*
  1020. * Deferred bio jobs.
  1021. */
  1022. static int io_overlaps_block(struct pool *pool, struct bio *bio)
  1023. {
  1024. return bio->bi_iter.bi_size ==
  1025. (pool->sectors_per_block << SECTOR_SHIFT);
  1026. }
  1027. static int io_overwrites_block(struct pool *pool, struct bio *bio)
  1028. {
  1029. return (bio_data_dir(bio) == WRITE) &&
  1030. io_overlaps_block(pool, bio);
  1031. }
  1032. static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
  1033. bio_end_io_t *fn)
  1034. {
  1035. *save = bio->bi_end_io;
  1036. bio->bi_end_io = fn;
  1037. }
  1038. static int ensure_next_mapping(struct pool *pool)
  1039. {
  1040. if (pool->next_mapping)
  1041. return 0;
  1042. pool->next_mapping = mempool_alloc(&pool->mapping_pool, GFP_ATOMIC);
  1043. return pool->next_mapping ? 0 : -ENOMEM;
  1044. }
  1045. static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
  1046. {
  1047. struct dm_thin_new_mapping *m = pool->next_mapping;
  1048. BUG_ON(!pool->next_mapping);
  1049. memset(m, 0, sizeof(struct dm_thin_new_mapping));
  1050. INIT_LIST_HEAD(&m->list);
  1051. m->bio = NULL;
  1052. pool->next_mapping = NULL;
  1053. return m;
  1054. }
  1055. static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
  1056. sector_t begin, sector_t end)
  1057. {
  1058. struct dm_io_region to;
  1059. to.bdev = tc->pool_dev->bdev;
  1060. to.sector = begin;
  1061. to.count = end - begin;
  1062. dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
  1063. }
  1064. static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
  1065. dm_block_t data_begin,
  1066. struct dm_thin_new_mapping *m)
  1067. {
  1068. struct pool *pool = tc->pool;
  1069. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1070. h->overwrite_mapping = m;
  1071. m->bio = bio;
  1072. save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
  1073. inc_all_io_entry(pool, bio);
  1074. remap_and_issue(tc, bio, data_begin);
  1075. }
  1076. /*
  1077. * A partial copy also needs to zero the uncopied region.
  1078. */
  1079. static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
  1080. struct dm_dev *origin, dm_block_t data_origin,
  1081. dm_block_t data_dest,
  1082. struct dm_bio_prison_cell *cell, struct bio *bio,
  1083. sector_t len)
  1084. {
  1085. struct pool *pool = tc->pool;
  1086. struct dm_thin_new_mapping *m = get_next_mapping(pool);
  1087. m->tc = tc;
  1088. m->virt_begin = virt_block;
  1089. m->virt_end = virt_block + 1u;
  1090. m->data_block = data_dest;
  1091. m->cell = cell;
  1092. /*
  1093. * quiesce action + copy action + an extra reference held for the
  1094. * duration of this function (we may need to inc later for a
  1095. * partial zero).
  1096. */
  1097. atomic_set(&m->prepare_actions, 3);
  1098. if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
  1099. complete_mapping_preparation(m); /* already quiesced */
  1100. /*
  1101. * IO to pool_dev remaps to the pool target's data_dev.
  1102. *
  1103. * If the whole block of data is being overwritten, we can issue the
  1104. * bio immediately. Otherwise we use kcopyd to clone the data first.
  1105. */
  1106. if (io_overwrites_block(pool, bio))
  1107. remap_and_issue_overwrite(tc, bio, data_dest, m);
  1108. else {
  1109. struct dm_io_region from, to;
  1110. from.bdev = origin->bdev;
  1111. from.sector = data_origin * pool->sectors_per_block;
  1112. from.count = len;
  1113. to.bdev = tc->pool_dev->bdev;
  1114. to.sector = data_dest * pool->sectors_per_block;
  1115. to.count = len;
  1116. dm_kcopyd_copy(pool->copier, &from, 1, &to,
  1117. 0, copy_complete, m);
  1118. /*
  1119. * Do we need to zero a tail region?
  1120. */
  1121. if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
  1122. atomic_inc(&m->prepare_actions);
  1123. ll_zero(tc, m,
  1124. data_dest * pool->sectors_per_block + len,
  1125. (data_dest + 1) * pool->sectors_per_block);
  1126. }
  1127. }
  1128. complete_mapping_preparation(m); /* drop our ref */
  1129. }
  1130. static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
  1131. dm_block_t data_origin, dm_block_t data_dest,
  1132. struct dm_bio_prison_cell *cell, struct bio *bio)
  1133. {
  1134. schedule_copy(tc, virt_block, tc->pool_dev,
  1135. data_origin, data_dest, cell, bio,
  1136. tc->pool->sectors_per_block);
  1137. }
  1138. static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
  1139. dm_block_t data_block, struct dm_bio_prison_cell *cell,
  1140. struct bio *bio)
  1141. {
  1142. struct pool *pool = tc->pool;
  1143. struct dm_thin_new_mapping *m = get_next_mapping(pool);
  1144. atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
  1145. m->tc = tc;
  1146. m->virt_begin = virt_block;
  1147. m->virt_end = virt_block + 1u;
  1148. m->data_block = data_block;
  1149. m->cell = cell;
  1150. /*
  1151. * If the whole block of data is being overwritten or we are not
  1152. * zeroing pre-existing data, we can issue the bio immediately.
  1153. * Otherwise we use kcopyd to zero the data first.
  1154. */
  1155. if (pool->pf.zero_new_blocks) {
  1156. if (io_overwrites_block(pool, bio))
  1157. remap_and_issue_overwrite(tc, bio, data_block, m);
  1158. else {
  1159. ll_zero(tc, m, data_block * pool->sectors_per_block,
  1160. (data_block + 1) * pool->sectors_per_block);
  1161. }
  1162. } else
  1163. process_prepared_mapping(m);
  1164. }
  1165. static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
  1166. dm_block_t data_dest,
  1167. struct dm_bio_prison_cell *cell, struct bio *bio)
  1168. {
  1169. struct pool *pool = tc->pool;
  1170. sector_t virt_block_begin = virt_block * pool->sectors_per_block;
  1171. sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
  1172. if (virt_block_end <= tc->origin_size) {
  1173. schedule_copy(tc, virt_block, tc->origin_dev,
  1174. virt_block, data_dest, cell, bio,
  1175. pool->sectors_per_block);
  1176. } else if (virt_block_begin < tc->origin_size) {
  1177. schedule_copy(tc, virt_block, tc->origin_dev,
  1178. virt_block, data_dest, cell, bio,
  1179. tc->origin_size - virt_block_begin);
  1180. } else
  1181. schedule_zero(tc, virt_block, data_dest, cell, bio);
  1182. }
  1183. static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
  1184. static void requeue_bios(struct pool *pool);
  1185. static bool is_read_only_pool_mode(enum pool_mode mode)
  1186. {
  1187. return (mode == PM_OUT_OF_METADATA_SPACE || mode == PM_READ_ONLY);
  1188. }
  1189. static bool is_read_only(struct pool *pool)
  1190. {
  1191. return is_read_only_pool_mode(get_pool_mode(pool));
  1192. }
  1193. static void check_for_metadata_space(struct pool *pool)
  1194. {
  1195. int r;
  1196. const char *ooms_reason = NULL;
  1197. dm_block_t nr_free;
  1198. r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free);
  1199. if (r)
  1200. ooms_reason = "Could not get free metadata blocks";
  1201. else if (!nr_free)
  1202. ooms_reason = "No free metadata blocks";
  1203. if (ooms_reason && !is_read_only(pool)) {
  1204. DMERR("%s", ooms_reason);
  1205. set_pool_mode(pool, PM_OUT_OF_METADATA_SPACE);
  1206. }
  1207. }
  1208. static void check_for_data_space(struct pool *pool)
  1209. {
  1210. int r;
  1211. dm_block_t nr_free;
  1212. if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
  1213. return;
  1214. r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
  1215. if (r)
  1216. return;
  1217. if (nr_free) {
  1218. set_pool_mode(pool, PM_WRITE);
  1219. requeue_bios(pool);
  1220. }
  1221. }
  1222. /*
  1223. * A non-zero return indicates read_only or fail_io mode.
  1224. * Many callers don't care about the return value.
  1225. */
  1226. static int commit(struct pool *pool)
  1227. {
  1228. int r;
  1229. if (get_pool_mode(pool) >= PM_OUT_OF_METADATA_SPACE)
  1230. return -EINVAL;
  1231. r = dm_pool_commit_metadata(pool->pmd);
  1232. if (r)
  1233. metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
  1234. else {
  1235. check_for_metadata_space(pool);
  1236. check_for_data_space(pool);
  1237. }
  1238. return r;
  1239. }
  1240. static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
  1241. {
  1242. if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
  1243. DMWARN("%s: reached low water mark for data device: sending event.",
  1244. dm_device_name(pool->pool_md));
  1245. spin_lock_irq(&pool->lock);
  1246. pool->low_water_triggered = true;
  1247. spin_unlock_irq(&pool->lock);
  1248. dm_table_event(pool->ti->table);
  1249. }
  1250. }
  1251. static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
  1252. {
  1253. int r;
  1254. dm_block_t free_blocks;
  1255. struct pool *pool = tc->pool;
  1256. if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
  1257. return -EINVAL;
  1258. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  1259. if (r) {
  1260. metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
  1261. return r;
  1262. }
  1263. check_low_water_mark(pool, free_blocks);
  1264. if (!free_blocks) {
  1265. /*
  1266. * Try to commit to see if that will free up some
  1267. * more space.
  1268. */
  1269. r = commit(pool);
  1270. if (r)
  1271. return r;
  1272. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  1273. if (r) {
  1274. metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
  1275. return r;
  1276. }
  1277. if (!free_blocks) {
  1278. set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
  1279. return -ENOSPC;
  1280. }
  1281. }
  1282. r = dm_pool_alloc_data_block(pool->pmd, result);
  1283. if (r) {
  1284. if (r == -ENOSPC)
  1285. set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
  1286. else
  1287. metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
  1288. return r;
  1289. }
  1290. r = dm_pool_get_free_metadata_block_count(pool->pmd, &free_blocks);
  1291. if (r) {
  1292. metadata_operation_failed(pool, "dm_pool_get_free_metadata_block_count", r);
  1293. return r;
  1294. }
  1295. if (!free_blocks) {
  1296. /* Let's commit before we use up the metadata reserve. */
  1297. r = commit(pool);
  1298. if (r)
  1299. return r;
  1300. }
  1301. return 0;
  1302. }
  1303. /*
  1304. * If we have run out of space, queue bios until the device is
  1305. * resumed, presumably after having been reloaded with more space.
  1306. */
  1307. static void retry_on_resume(struct bio *bio)
  1308. {
  1309. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1310. struct thin_c *tc = h->tc;
  1311. spin_lock_irq(&tc->lock);
  1312. bio_list_add(&tc->retry_on_resume_list, bio);
  1313. spin_unlock_irq(&tc->lock);
  1314. }
  1315. static blk_status_t should_error_unserviceable_bio(struct pool *pool)
  1316. {
  1317. enum pool_mode m = get_pool_mode(pool);
  1318. switch (m) {
  1319. case PM_WRITE:
  1320. /* Shouldn't get here */
  1321. DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
  1322. return BLK_STS_IOERR;
  1323. case PM_OUT_OF_DATA_SPACE:
  1324. return pool->pf.error_if_no_space ? BLK_STS_NOSPC : 0;
  1325. case PM_OUT_OF_METADATA_SPACE:
  1326. case PM_READ_ONLY:
  1327. case PM_FAIL:
  1328. return BLK_STS_IOERR;
  1329. default:
  1330. /* Shouldn't get here */
  1331. DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
  1332. return BLK_STS_IOERR;
  1333. }
  1334. }
  1335. static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
  1336. {
  1337. blk_status_t error = should_error_unserviceable_bio(pool);
  1338. if (error) {
  1339. bio->bi_status = error;
  1340. bio_endio(bio);
  1341. } else
  1342. retry_on_resume(bio);
  1343. }
  1344. static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
  1345. {
  1346. struct bio *bio;
  1347. struct bio_list bios;
  1348. blk_status_t error;
  1349. error = should_error_unserviceable_bio(pool);
  1350. if (error) {
  1351. cell_error_with_code(pool, cell, error);
  1352. return;
  1353. }
  1354. bio_list_init(&bios);
  1355. cell_release(pool, cell, &bios);
  1356. while ((bio = bio_list_pop(&bios)))
  1357. retry_on_resume(bio);
  1358. }
  1359. static void process_discard_cell_no_passdown(struct thin_c *tc,
  1360. struct dm_bio_prison_cell *virt_cell)
  1361. {
  1362. struct pool *pool = tc->pool;
  1363. struct dm_thin_new_mapping *m = get_next_mapping(pool);
  1364. /*
  1365. * We don't need to lock the data blocks, since there's no
  1366. * passdown. We only lock data blocks for allocation and breaking sharing.
  1367. */
  1368. m->tc = tc;
  1369. m->virt_begin = virt_cell->key.block_begin;
  1370. m->virt_end = virt_cell->key.block_end;
  1371. m->cell = virt_cell;
  1372. m->bio = virt_cell->holder;
  1373. if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
  1374. pool->process_prepared_discard(m);
  1375. }
  1376. static void break_up_discard_bio(struct thin_c *tc, dm_block_t begin, dm_block_t end,
  1377. struct bio *bio)
  1378. {
  1379. struct pool *pool = tc->pool;
  1380. int r;
  1381. bool maybe_shared;
  1382. struct dm_cell_key data_key;
  1383. struct dm_bio_prison_cell *data_cell;
  1384. struct dm_thin_new_mapping *m;
  1385. dm_block_t virt_begin, virt_end, data_begin, data_end;
  1386. dm_block_t len, next_boundary;
  1387. while (begin != end) {
  1388. r = dm_thin_find_mapped_range(tc->td, begin, end, &virt_begin, &virt_end,
  1389. &data_begin, &maybe_shared);
  1390. if (r) {
  1391. /*
  1392. * Silently fail, letting any mappings we've
  1393. * created complete.
  1394. */
  1395. break;
  1396. }
  1397. data_end = data_begin + (virt_end - virt_begin);
  1398. /*
  1399. * Make sure the data region obeys the bio prison restrictions.
  1400. */
  1401. while (data_begin < data_end) {
  1402. r = ensure_next_mapping(pool);
  1403. if (r)
  1404. return; /* we did our best */
  1405. next_boundary = ((data_begin >> BIO_PRISON_MAX_RANGE_SHIFT) + 1)
  1406. << BIO_PRISON_MAX_RANGE_SHIFT;
  1407. len = min_t(sector_t, data_end - data_begin, next_boundary - data_begin);
  1408. /* This key is certainly within range given the above splitting */
  1409. (void) build_key(tc->td, PHYSICAL, data_begin, data_begin + len, &data_key);
  1410. if (bio_detain(tc->pool, &data_key, NULL, &data_cell)) {
  1411. /* contention, we'll give up with this range */
  1412. data_begin += len;
  1413. continue;
  1414. }
  1415. /*
  1416. * IO may still be going to the destination block. We must
  1417. * quiesce before we can do the removal.
  1418. */
  1419. m = get_next_mapping(pool);
  1420. m->tc = tc;
  1421. m->maybe_shared = maybe_shared;
  1422. m->virt_begin = virt_begin;
  1423. m->virt_end = virt_begin + len;
  1424. m->data_block = data_begin;
  1425. m->cell = data_cell;
  1426. m->bio = bio;
  1427. /*
  1428. * The parent bio must not complete before sub discard bios are
  1429. * chained to it (see end_discard's bio_chain)!
  1430. *
  1431. * This per-mapping bi_remaining increment is paired with
  1432. * the implicit decrement that occurs via bio_endio() in
  1433. * end_discard().
  1434. */
  1435. bio_inc_remaining(bio);
  1436. if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
  1437. pool->process_prepared_discard(m);
  1438. virt_begin += len;
  1439. data_begin += len;
  1440. }
  1441. begin = virt_end;
  1442. }
  1443. }
  1444. static void process_discard_cell_passdown(struct thin_c *tc, struct dm_bio_prison_cell *virt_cell)
  1445. {
  1446. struct bio *bio = virt_cell->holder;
  1447. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1448. /*
  1449. * The virt_cell will only get freed once the origin bio completes.
  1450. * This means it will remain locked while all the individual
  1451. * passdown bios are in flight.
  1452. */
  1453. h->cell = virt_cell;
  1454. break_up_discard_bio(tc, virt_cell->key.block_begin, virt_cell->key.block_end, bio);
  1455. /*
  1456. * We complete the bio now, knowing that the bi_remaining field
  1457. * will prevent completion until the sub range discards have
  1458. * completed.
  1459. */
  1460. bio_endio(bio);
  1461. }
  1462. static void process_discard_bio(struct thin_c *tc, struct bio *bio)
  1463. {
  1464. dm_block_t begin, end;
  1465. struct dm_cell_key virt_key;
  1466. struct dm_bio_prison_cell *virt_cell;
  1467. get_bio_block_range(tc, bio, &begin, &end);
  1468. if (begin == end) {
  1469. /*
  1470. * The discard covers less than a block.
  1471. */
  1472. bio_endio(bio);
  1473. return;
  1474. }
  1475. if (unlikely(!build_key(tc->td, VIRTUAL, begin, end, &virt_key))) {
  1476. DMERR_LIMIT("Discard doesn't respect bio prison limits");
  1477. bio_endio(bio);
  1478. return;
  1479. }
  1480. if (bio_detain(tc->pool, &virt_key, bio, &virt_cell)) {
  1481. /*
  1482. * Potential starvation issue: We're relying on the
  1483. * fs/application being well behaved, and not trying to
  1484. * send IO to a region at the same time as discarding it.
  1485. * If they do this persistently then it's possible this
  1486. * cell will never be granted.
  1487. */
  1488. return;
  1489. }
  1490. tc->pool->process_discard_cell(tc, virt_cell);
  1491. }
  1492. static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
  1493. struct dm_cell_key *key,
  1494. struct dm_thin_lookup_result *lookup_result,
  1495. struct dm_bio_prison_cell *cell)
  1496. {
  1497. int r;
  1498. dm_block_t data_block;
  1499. struct pool *pool = tc->pool;
  1500. r = alloc_data_block(tc, &data_block);
  1501. switch (r) {
  1502. case 0:
  1503. schedule_internal_copy(tc, block, lookup_result->block,
  1504. data_block, cell, bio);
  1505. break;
  1506. case -ENOSPC:
  1507. retry_bios_on_resume(pool, cell);
  1508. break;
  1509. default:
  1510. DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
  1511. __func__, r);
  1512. cell_error(pool, cell);
  1513. break;
  1514. }
  1515. }
  1516. static void __remap_and_issue_shared_cell(void *context,
  1517. struct dm_bio_prison_cell *cell)
  1518. {
  1519. struct remap_info *info = context;
  1520. struct bio *bio;
  1521. while ((bio = bio_list_pop(&cell->bios))) {
  1522. if (bio_data_dir(bio) == WRITE || op_is_flush(bio->bi_opf) ||
  1523. bio_op(bio) == REQ_OP_DISCARD)
  1524. bio_list_add(&info->defer_bios, bio);
  1525. else {
  1526. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1527. h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
  1528. inc_all_io_entry(info->tc->pool, bio);
  1529. bio_list_add(&info->issue_bios, bio);
  1530. }
  1531. }
  1532. }
  1533. static void remap_and_issue_shared_cell(struct thin_c *tc,
  1534. struct dm_bio_prison_cell *cell,
  1535. dm_block_t block)
  1536. {
  1537. struct bio *bio;
  1538. struct remap_info info;
  1539. info.tc = tc;
  1540. bio_list_init(&info.defer_bios);
  1541. bio_list_init(&info.issue_bios);
  1542. cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
  1543. &info, cell);
  1544. while ((bio = bio_list_pop(&info.defer_bios)))
  1545. thin_defer_bio(tc, bio);
  1546. while ((bio = bio_list_pop(&info.issue_bios)))
  1547. remap_and_issue(tc, bio, block);
  1548. }
  1549. static void process_shared_bio(struct thin_c *tc, struct bio *bio,
  1550. dm_block_t block,
  1551. struct dm_thin_lookup_result *lookup_result,
  1552. struct dm_bio_prison_cell *virt_cell)
  1553. {
  1554. struct dm_bio_prison_cell *data_cell;
  1555. struct pool *pool = tc->pool;
  1556. struct dm_cell_key key;
  1557. /*
  1558. * If cell is already occupied, then sharing is already in the process
  1559. * of being broken so we have nothing further to do here.
  1560. */
  1561. build_data_key(tc->td, lookup_result->block, &key);
  1562. if (bio_detain(pool, &key, bio, &data_cell)) {
  1563. cell_defer_no_holder(tc, virt_cell);
  1564. return;
  1565. }
  1566. if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
  1567. break_sharing(tc, bio, block, &key, lookup_result, data_cell);
  1568. cell_defer_no_holder(tc, virt_cell);
  1569. } else {
  1570. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1571. h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
  1572. inc_all_io_entry(pool, bio);
  1573. remap_and_issue(tc, bio, lookup_result->block);
  1574. remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
  1575. remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
  1576. }
  1577. }
  1578. static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
  1579. struct dm_bio_prison_cell *cell)
  1580. {
  1581. int r;
  1582. dm_block_t data_block;
  1583. struct pool *pool = tc->pool;
  1584. /*
  1585. * Remap empty bios (flushes) immediately, without provisioning.
  1586. */
  1587. if (!bio->bi_iter.bi_size) {
  1588. inc_all_io_entry(pool, bio);
  1589. cell_defer_no_holder(tc, cell);
  1590. remap_and_issue(tc, bio, 0);
  1591. return;
  1592. }
  1593. /*
  1594. * Fill read bios with zeroes and complete them immediately.
  1595. */
  1596. if (bio_data_dir(bio) == READ) {
  1597. zero_fill_bio(bio);
  1598. cell_defer_no_holder(tc, cell);
  1599. bio_endio(bio);
  1600. return;
  1601. }
  1602. r = alloc_data_block(tc, &data_block);
  1603. switch (r) {
  1604. case 0:
  1605. if (tc->origin_dev)
  1606. schedule_external_copy(tc, block, data_block, cell, bio);
  1607. else
  1608. schedule_zero(tc, block, data_block, cell, bio);
  1609. break;
  1610. case -ENOSPC:
  1611. retry_bios_on_resume(pool, cell);
  1612. break;
  1613. default:
  1614. DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
  1615. __func__, r);
  1616. cell_error(pool, cell);
  1617. break;
  1618. }
  1619. }
  1620. static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1621. {
  1622. int r;
  1623. struct pool *pool = tc->pool;
  1624. struct bio *bio = cell->holder;
  1625. dm_block_t block = get_bio_block(tc, bio);
  1626. struct dm_thin_lookup_result lookup_result;
  1627. if (tc->requeue_mode) {
  1628. cell_requeue(pool, cell);
  1629. return;
  1630. }
  1631. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1632. switch (r) {
  1633. case 0:
  1634. if (lookup_result.shared)
  1635. process_shared_bio(tc, bio, block, &lookup_result, cell);
  1636. else {
  1637. inc_all_io_entry(pool, bio);
  1638. remap_and_issue(tc, bio, lookup_result.block);
  1639. inc_remap_and_issue_cell(tc, cell, lookup_result.block);
  1640. }
  1641. break;
  1642. case -ENODATA:
  1643. if (bio_data_dir(bio) == READ && tc->origin_dev) {
  1644. inc_all_io_entry(pool, bio);
  1645. cell_defer_no_holder(tc, cell);
  1646. if (bio_end_sector(bio) <= tc->origin_size)
  1647. remap_to_origin_and_issue(tc, bio);
  1648. else if (bio->bi_iter.bi_sector < tc->origin_size) {
  1649. zero_fill_bio(bio);
  1650. bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
  1651. remap_to_origin_and_issue(tc, bio);
  1652. } else {
  1653. zero_fill_bio(bio);
  1654. bio_endio(bio);
  1655. }
  1656. } else
  1657. provision_block(tc, bio, block, cell);
  1658. break;
  1659. default:
  1660. DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
  1661. __func__, r);
  1662. cell_defer_no_holder(tc, cell);
  1663. bio_io_error(bio);
  1664. break;
  1665. }
  1666. }
  1667. static void process_bio(struct thin_c *tc, struct bio *bio)
  1668. {
  1669. struct pool *pool = tc->pool;
  1670. dm_block_t block = get_bio_block(tc, bio);
  1671. struct dm_bio_prison_cell *cell;
  1672. struct dm_cell_key key;
  1673. /*
  1674. * If cell is already occupied, then the block is already
  1675. * being provisioned so we have nothing further to do here.
  1676. */
  1677. build_virtual_key(tc->td, block, &key);
  1678. if (bio_detain(pool, &key, bio, &cell))
  1679. return;
  1680. process_cell(tc, cell);
  1681. }
  1682. static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
  1683. struct dm_bio_prison_cell *cell)
  1684. {
  1685. int r;
  1686. int rw = bio_data_dir(bio);
  1687. dm_block_t block = get_bio_block(tc, bio);
  1688. struct dm_thin_lookup_result lookup_result;
  1689. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1690. switch (r) {
  1691. case 0:
  1692. if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
  1693. handle_unserviceable_bio(tc->pool, bio);
  1694. if (cell)
  1695. cell_defer_no_holder(tc, cell);
  1696. } else {
  1697. inc_all_io_entry(tc->pool, bio);
  1698. remap_and_issue(tc, bio, lookup_result.block);
  1699. if (cell)
  1700. inc_remap_and_issue_cell(tc, cell, lookup_result.block);
  1701. }
  1702. break;
  1703. case -ENODATA:
  1704. if (cell)
  1705. cell_defer_no_holder(tc, cell);
  1706. if (rw != READ) {
  1707. handle_unserviceable_bio(tc->pool, bio);
  1708. break;
  1709. }
  1710. if (tc->origin_dev) {
  1711. inc_all_io_entry(tc->pool, bio);
  1712. remap_to_origin_and_issue(tc, bio);
  1713. break;
  1714. }
  1715. zero_fill_bio(bio);
  1716. bio_endio(bio);
  1717. break;
  1718. default:
  1719. DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
  1720. __func__, r);
  1721. if (cell)
  1722. cell_defer_no_holder(tc, cell);
  1723. bio_io_error(bio);
  1724. break;
  1725. }
  1726. }
  1727. static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
  1728. {
  1729. __process_bio_read_only(tc, bio, NULL);
  1730. }
  1731. static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1732. {
  1733. __process_bio_read_only(tc, cell->holder, cell);
  1734. }
  1735. static void process_bio_success(struct thin_c *tc, struct bio *bio)
  1736. {
  1737. bio_endio(bio);
  1738. }
  1739. static void process_bio_fail(struct thin_c *tc, struct bio *bio)
  1740. {
  1741. bio_io_error(bio);
  1742. }
  1743. static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1744. {
  1745. cell_success(tc->pool, cell);
  1746. }
  1747. static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1748. {
  1749. cell_error(tc->pool, cell);
  1750. }
  1751. /*
  1752. * FIXME: should we also commit due to size of transaction, measured in
  1753. * metadata blocks?
  1754. */
  1755. static int need_commit_due_to_time(struct pool *pool)
  1756. {
  1757. return !time_in_range(jiffies, pool->last_commit_jiffies,
  1758. pool->last_commit_jiffies + COMMIT_PERIOD);
  1759. }
  1760. #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
  1761. #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
  1762. static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
  1763. {
  1764. struct rb_node **rbp, *parent;
  1765. struct dm_thin_endio_hook *pbd;
  1766. sector_t bi_sector = bio->bi_iter.bi_sector;
  1767. rbp = &tc->sort_bio_list.rb_node;
  1768. parent = NULL;
  1769. while (*rbp) {
  1770. parent = *rbp;
  1771. pbd = thin_pbd(parent);
  1772. if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
  1773. rbp = &(*rbp)->rb_left;
  1774. else
  1775. rbp = &(*rbp)->rb_right;
  1776. }
  1777. pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1778. rb_link_node(&pbd->rb_node, parent, rbp);
  1779. rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
  1780. }
  1781. static void __extract_sorted_bios(struct thin_c *tc)
  1782. {
  1783. struct rb_node *node;
  1784. struct dm_thin_endio_hook *pbd;
  1785. struct bio *bio;
  1786. for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
  1787. pbd = thin_pbd(node);
  1788. bio = thin_bio(pbd);
  1789. bio_list_add(&tc->deferred_bio_list, bio);
  1790. rb_erase(&pbd->rb_node, &tc->sort_bio_list);
  1791. }
  1792. WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
  1793. }
  1794. static void __sort_thin_deferred_bios(struct thin_c *tc)
  1795. {
  1796. struct bio *bio;
  1797. struct bio_list bios;
  1798. bio_list_init(&bios);
  1799. bio_list_merge(&bios, &tc->deferred_bio_list);
  1800. bio_list_init(&tc->deferred_bio_list);
  1801. /* Sort deferred_bio_list using rb-tree */
  1802. while ((bio = bio_list_pop(&bios)))
  1803. __thin_bio_rb_add(tc, bio);
  1804. /*
  1805. * Transfer the sorted bios in sort_bio_list back to
  1806. * deferred_bio_list to allow lockless submission of
  1807. * all bios.
  1808. */
  1809. __extract_sorted_bios(tc);
  1810. }
  1811. static void process_thin_deferred_bios(struct thin_c *tc)
  1812. {
  1813. struct pool *pool = tc->pool;
  1814. struct bio *bio;
  1815. struct bio_list bios;
  1816. struct blk_plug plug;
  1817. unsigned int count = 0;
  1818. if (tc->requeue_mode) {
  1819. error_thin_bio_list(tc, &tc->deferred_bio_list,
  1820. BLK_STS_DM_REQUEUE);
  1821. return;
  1822. }
  1823. bio_list_init(&bios);
  1824. spin_lock_irq(&tc->lock);
  1825. if (bio_list_empty(&tc->deferred_bio_list)) {
  1826. spin_unlock_irq(&tc->lock);
  1827. return;
  1828. }
  1829. __sort_thin_deferred_bios(tc);
  1830. bio_list_merge(&bios, &tc->deferred_bio_list);
  1831. bio_list_init(&tc->deferred_bio_list);
  1832. spin_unlock_irq(&tc->lock);
  1833. blk_start_plug(&plug);
  1834. while ((bio = bio_list_pop(&bios))) {
  1835. /*
  1836. * If we've got no free new_mapping structs, and processing
  1837. * this bio might require one, we pause until there are some
  1838. * prepared mappings to process.
  1839. */
  1840. if (ensure_next_mapping(pool)) {
  1841. spin_lock_irq(&tc->lock);
  1842. bio_list_add(&tc->deferred_bio_list, bio);
  1843. bio_list_merge(&tc->deferred_bio_list, &bios);
  1844. spin_unlock_irq(&tc->lock);
  1845. break;
  1846. }
  1847. if (bio_op(bio) == REQ_OP_DISCARD)
  1848. pool->process_discard(tc, bio);
  1849. else
  1850. pool->process_bio(tc, bio);
  1851. if ((count++ & 127) == 0) {
  1852. throttle_work_update(&pool->throttle);
  1853. dm_pool_issue_prefetches(pool->pmd);
  1854. }
  1855. cond_resched();
  1856. }
  1857. blk_finish_plug(&plug);
  1858. }
  1859. static int cmp_cells(const void *lhs, const void *rhs)
  1860. {
  1861. struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
  1862. struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
  1863. BUG_ON(!lhs_cell->holder);
  1864. BUG_ON(!rhs_cell->holder);
  1865. if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
  1866. return -1;
  1867. if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
  1868. return 1;
  1869. return 0;
  1870. }
  1871. static unsigned int sort_cells(struct pool *pool, struct list_head *cells)
  1872. {
  1873. unsigned int count = 0;
  1874. struct dm_bio_prison_cell *cell, *tmp;
  1875. list_for_each_entry_safe(cell, tmp, cells, user_list) {
  1876. if (count >= CELL_SORT_ARRAY_SIZE)
  1877. break;
  1878. pool->cell_sort_array[count++] = cell;
  1879. list_del(&cell->user_list);
  1880. }
  1881. sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
  1882. return count;
  1883. }
  1884. static void process_thin_deferred_cells(struct thin_c *tc)
  1885. {
  1886. struct pool *pool = tc->pool;
  1887. struct list_head cells;
  1888. struct dm_bio_prison_cell *cell;
  1889. unsigned int i, j, count;
  1890. INIT_LIST_HEAD(&cells);
  1891. spin_lock_irq(&tc->lock);
  1892. list_splice_init(&tc->deferred_cells, &cells);
  1893. spin_unlock_irq(&tc->lock);
  1894. if (list_empty(&cells))
  1895. return;
  1896. do {
  1897. count = sort_cells(tc->pool, &cells);
  1898. for (i = 0; i < count; i++) {
  1899. cell = pool->cell_sort_array[i];
  1900. BUG_ON(!cell->holder);
  1901. /*
  1902. * If we've got no free new_mapping structs, and processing
  1903. * this bio might require one, we pause until there are some
  1904. * prepared mappings to process.
  1905. */
  1906. if (ensure_next_mapping(pool)) {
  1907. for (j = i; j < count; j++)
  1908. list_add(&pool->cell_sort_array[j]->user_list, &cells);
  1909. spin_lock_irq(&tc->lock);
  1910. list_splice(&cells, &tc->deferred_cells);
  1911. spin_unlock_irq(&tc->lock);
  1912. return;
  1913. }
  1914. if (bio_op(cell->holder) == REQ_OP_DISCARD)
  1915. pool->process_discard_cell(tc, cell);
  1916. else
  1917. pool->process_cell(tc, cell);
  1918. }
  1919. cond_resched();
  1920. } while (!list_empty(&cells));
  1921. }
  1922. static void thin_get(struct thin_c *tc);
  1923. static void thin_put(struct thin_c *tc);
  1924. /*
  1925. * We can't hold rcu_read_lock() around code that can block. So we
  1926. * find a thin with the rcu lock held; bump a refcount; then drop
  1927. * the lock.
  1928. */
  1929. static struct thin_c *get_first_thin(struct pool *pool)
  1930. {
  1931. struct thin_c *tc = NULL;
  1932. rcu_read_lock();
  1933. tc = list_first_or_null_rcu(&pool->active_thins, struct thin_c, list);
  1934. if (tc)
  1935. thin_get(tc);
  1936. rcu_read_unlock();
  1937. return tc;
  1938. }
  1939. static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
  1940. {
  1941. struct thin_c *old_tc = tc;
  1942. rcu_read_lock();
  1943. list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
  1944. thin_get(tc);
  1945. thin_put(old_tc);
  1946. rcu_read_unlock();
  1947. return tc;
  1948. }
  1949. thin_put(old_tc);
  1950. rcu_read_unlock();
  1951. return NULL;
  1952. }
  1953. static void process_deferred_bios(struct pool *pool)
  1954. {
  1955. struct bio *bio;
  1956. struct bio_list bios, bio_completions;
  1957. struct thin_c *tc;
  1958. tc = get_first_thin(pool);
  1959. while (tc) {
  1960. process_thin_deferred_cells(tc);
  1961. process_thin_deferred_bios(tc);
  1962. tc = get_next_thin(pool, tc);
  1963. }
  1964. /*
  1965. * If there are any deferred flush bios, we must commit the metadata
  1966. * before issuing them or signaling their completion.
  1967. */
  1968. bio_list_init(&bios);
  1969. bio_list_init(&bio_completions);
  1970. spin_lock_irq(&pool->lock);
  1971. bio_list_merge(&bios, &pool->deferred_flush_bios);
  1972. bio_list_init(&pool->deferred_flush_bios);
  1973. bio_list_merge(&bio_completions, &pool->deferred_flush_completions);
  1974. bio_list_init(&pool->deferred_flush_completions);
  1975. spin_unlock_irq(&pool->lock);
  1976. if (bio_list_empty(&bios) && bio_list_empty(&bio_completions) &&
  1977. !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
  1978. return;
  1979. if (commit(pool)) {
  1980. bio_list_merge(&bios, &bio_completions);
  1981. while ((bio = bio_list_pop(&bios)))
  1982. bio_io_error(bio);
  1983. return;
  1984. }
  1985. pool->last_commit_jiffies = jiffies;
  1986. while ((bio = bio_list_pop(&bio_completions)))
  1987. bio_endio(bio);
  1988. while ((bio = bio_list_pop(&bios))) {
  1989. /*
  1990. * The data device was flushed as part of metadata commit,
  1991. * so complete redundant flushes immediately.
  1992. */
  1993. if (bio->bi_opf & REQ_PREFLUSH)
  1994. bio_endio(bio);
  1995. else
  1996. dm_submit_bio_remap(bio, NULL);
  1997. }
  1998. }
  1999. static void do_worker(struct work_struct *ws)
  2000. {
  2001. struct pool *pool = container_of(ws, struct pool, worker);
  2002. throttle_work_start(&pool->throttle);
  2003. dm_pool_issue_prefetches(pool->pmd);
  2004. throttle_work_update(&pool->throttle);
  2005. process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
  2006. throttle_work_update(&pool->throttle);
  2007. process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
  2008. throttle_work_update(&pool->throttle);
  2009. process_prepared(pool, &pool->prepared_discards_pt2, &pool->process_prepared_discard_pt2);
  2010. throttle_work_update(&pool->throttle);
  2011. process_deferred_bios(pool);
  2012. throttle_work_complete(&pool->throttle);
  2013. }
  2014. /*
  2015. * We want to commit periodically so that not too much
  2016. * unwritten data builds up.
  2017. */
  2018. static void do_waker(struct work_struct *ws)
  2019. {
  2020. struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
  2021. wake_worker(pool);
  2022. queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
  2023. }
  2024. /*
  2025. * We're holding onto IO to allow userland time to react. After the
  2026. * timeout either the pool will have been resized (and thus back in
  2027. * PM_WRITE mode), or we degrade to PM_OUT_OF_DATA_SPACE w/ error_if_no_space.
  2028. */
  2029. static void do_no_space_timeout(struct work_struct *ws)
  2030. {
  2031. struct pool *pool = container_of(to_delayed_work(ws), struct pool,
  2032. no_space_timeout);
  2033. if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space) {
  2034. pool->pf.error_if_no_space = true;
  2035. notify_of_pool_mode_change(pool);
  2036. error_retry_list_with_code(pool, BLK_STS_NOSPC);
  2037. }
  2038. }
  2039. /*----------------------------------------------------------------*/
  2040. struct pool_work {
  2041. struct work_struct worker;
  2042. struct completion complete;
  2043. };
  2044. static struct pool_work *to_pool_work(struct work_struct *ws)
  2045. {
  2046. return container_of(ws, struct pool_work, worker);
  2047. }
  2048. static void pool_work_complete(struct pool_work *pw)
  2049. {
  2050. complete(&pw->complete);
  2051. }
  2052. static void pool_work_wait(struct pool_work *pw, struct pool *pool,
  2053. void (*fn)(struct work_struct *))
  2054. {
  2055. INIT_WORK_ONSTACK(&pw->worker, fn);
  2056. init_completion(&pw->complete);
  2057. queue_work(pool->wq, &pw->worker);
  2058. wait_for_completion(&pw->complete);
  2059. destroy_work_on_stack(&pw->worker);
  2060. }
  2061. /*----------------------------------------------------------------*/
  2062. struct noflush_work {
  2063. struct pool_work pw;
  2064. struct thin_c *tc;
  2065. };
  2066. static struct noflush_work *to_noflush(struct work_struct *ws)
  2067. {
  2068. return container_of(to_pool_work(ws), struct noflush_work, pw);
  2069. }
  2070. static void do_noflush_start(struct work_struct *ws)
  2071. {
  2072. struct noflush_work *w = to_noflush(ws);
  2073. w->tc->requeue_mode = true;
  2074. requeue_io(w->tc);
  2075. pool_work_complete(&w->pw);
  2076. }
  2077. static void do_noflush_stop(struct work_struct *ws)
  2078. {
  2079. struct noflush_work *w = to_noflush(ws);
  2080. w->tc->requeue_mode = false;
  2081. pool_work_complete(&w->pw);
  2082. }
  2083. static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
  2084. {
  2085. struct noflush_work w;
  2086. w.tc = tc;
  2087. pool_work_wait(&w.pw, tc->pool, fn);
  2088. }
  2089. /*----------------------------------------------------------------*/
  2090. static void set_discard_callbacks(struct pool *pool)
  2091. {
  2092. struct pool_c *pt = pool->ti->private;
  2093. if (pt->adjusted_pf.discard_passdown) {
  2094. pool->process_discard_cell = process_discard_cell_passdown;
  2095. pool->process_prepared_discard = process_prepared_discard_passdown_pt1;
  2096. pool->process_prepared_discard_pt2 = process_prepared_discard_passdown_pt2;
  2097. } else {
  2098. pool->process_discard_cell = process_discard_cell_no_passdown;
  2099. pool->process_prepared_discard = process_prepared_discard_no_passdown;
  2100. }
  2101. }
  2102. static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
  2103. {
  2104. struct pool_c *pt = pool->ti->private;
  2105. bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
  2106. enum pool_mode old_mode = get_pool_mode(pool);
  2107. unsigned long no_space_timeout = READ_ONCE(no_space_timeout_secs) * HZ;
  2108. /*
  2109. * Never allow the pool to transition to PM_WRITE mode if user
  2110. * intervention is required to verify metadata and data consistency.
  2111. */
  2112. if (new_mode == PM_WRITE && needs_check) {
  2113. DMERR("%s: unable to switch pool to write mode until repaired.",
  2114. dm_device_name(pool->pool_md));
  2115. if (old_mode != new_mode)
  2116. new_mode = old_mode;
  2117. else
  2118. new_mode = PM_READ_ONLY;
  2119. }
  2120. /*
  2121. * If we were in PM_FAIL mode, rollback of metadata failed. We're
  2122. * not going to recover without a thin_repair. So we never let the
  2123. * pool move out of the old mode.
  2124. */
  2125. if (old_mode == PM_FAIL)
  2126. new_mode = old_mode;
  2127. switch (new_mode) {
  2128. case PM_FAIL:
  2129. dm_pool_metadata_read_only(pool->pmd);
  2130. pool->process_bio = process_bio_fail;
  2131. pool->process_discard = process_bio_fail;
  2132. pool->process_cell = process_cell_fail;
  2133. pool->process_discard_cell = process_cell_fail;
  2134. pool->process_prepared_mapping = process_prepared_mapping_fail;
  2135. pool->process_prepared_discard = process_prepared_discard_fail;
  2136. error_retry_list(pool);
  2137. break;
  2138. case PM_OUT_OF_METADATA_SPACE:
  2139. case PM_READ_ONLY:
  2140. dm_pool_metadata_read_only(pool->pmd);
  2141. pool->process_bio = process_bio_read_only;
  2142. pool->process_discard = process_bio_success;
  2143. pool->process_cell = process_cell_read_only;
  2144. pool->process_discard_cell = process_cell_success;
  2145. pool->process_prepared_mapping = process_prepared_mapping_fail;
  2146. pool->process_prepared_discard = process_prepared_discard_success;
  2147. error_retry_list(pool);
  2148. break;
  2149. case PM_OUT_OF_DATA_SPACE:
  2150. /*
  2151. * Ideally we'd never hit this state; the low water mark
  2152. * would trigger userland to extend the pool before we
  2153. * completely run out of data space. However, many small
  2154. * IOs to unprovisioned space can consume data space at an
  2155. * alarming rate. Adjust your low water mark if you're
  2156. * frequently seeing this mode.
  2157. */
  2158. pool->out_of_data_space = true;
  2159. pool->process_bio = process_bio_read_only;
  2160. pool->process_discard = process_discard_bio;
  2161. pool->process_cell = process_cell_read_only;
  2162. pool->process_prepared_mapping = process_prepared_mapping;
  2163. set_discard_callbacks(pool);
  2164. if (!pool->pf.error_if_no_space && no_space_timeout)
  2165. queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
  2166. break;
  2167. case PM_WRITE:
  2168. if (old_mode == PM_OUT_OF_DATA_SPACE)
  2169. cancel_delayed_work_sync(&pool->no_space_timeout);
  2170. pool->out_of_data_space = false;
  2171. pool->pf.error_if_no_space = pt->requested_pf.error_if_no_space;
  2172. dm_pool_metadata_read_write(pool->pmd);
  2173. pool->process_bio = process_bio;
  2174. pool->process_discard = process_discard_bio;
  2175. pool->process_cell = process_cell;
  2176. pool->process_prepared_mapping = process_prepared_mapping;
  2177. set_discard_callbacks(pool);
  2178. break;
  2179. }
  2180. pool->pf.mode = new_mode;
  2181. /*
  2182. * The pool mode may have changed, sync it so bind_control_target()
  2183. * doesn't cause an unexpected mode transition on resume.
  2184. */
  2185. pt->adjusted_pf.mode = new_mode;
  2186. if (old_mode != new_mode)
  2187. notify_of_pool_mode_change(pool);
  2188. }
  2189. static void abort_transaction(struct pool *pool)
  2190. {
  2191. const char *dev_name = dm_device_name(pool->pool_md);
  2192. DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
  2193. if (dm_pool_abort_metadata(pool->pmd)) {
  2194. DMERR("%s: failed to abort metadata transaction", dev_name);
  2195. set_pool_mode(pool, PM_FAIL);
  2196. }
  2197. if (dm_pool_metadata_set_needs_check(pool->pmd)) {
  2198. DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
  2199. set_pool_mode(pool, PM_FAIL);
  2200. }
  2201. }
  2202. static void metadata_operation_failed(struct pool *pool, const char *op, int r)
  2203. {
  2204. DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
  2205. dm_device_name(pool->pool_md), op, r);
  2206. abort_transaction(pool);
  2207. set_pool_mode(pool, PM_READ_ONLY);
  2208. }
  2209. /*----------------------------------------------------------------*/
  2210. /*
  2211. * Mapping functions.
  2212. */
  2213. /*
  2214. * Called only while mapping a thin bio to hand it over to the workqueue.
  2215. */
  2216. static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
  2217. {
  2218. struct pool *pool = tc->pool;
  2219. spin_lock_irq(&tc->lock);
  2220. bio_list_add(&tc->deferred_bio_list, bio);
  2221. spin_unlock_irq(&tc->lock);
  2222. wake_worker(pool);
  2223. }
  2224. static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
  2225. {
  2226. struct pool *pool = tc->pool;
  2227. throttle_lock(&pool->throttle);
  2228. thin_defer_bio(tc, bio);
  2229. throttle_unlock(&pool->throttle);
  2230. }
  2231. static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  2232. {
  2233. struct pool *pool = tc->pool;
  2234. throttle_lock(&pool->throttle);
  2235. spin_lock_irq(&tc->lock);
  2236. list_add_tail(&cell->user_list, &tc->deferred_cells);
  2237. spin_unlock_irq(&tc->lock);
  2238. throttle_unlock(&pool->throttle);
  2239. wake_worker(pool);
  2240. }
  2241. static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
  2242. {
  2243. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  2244. h->tc = tc;
  2245. h->shared_read_entry = NULL;
  2246. h->all_io_entry = NULL;
  2247. h->overwrite_mapping = NULL;
  2248. h->cell = NULL;
  2249. }
  2250. /*
  2251. * Non-blocking function called from the thin target's map function.
  2252. */
  2253. static int thin_bio_map(struct dm_target *ti, struct bio *bio)
  2254. {
  2255. int r;
  2256. struct thin_c *tc = ti->private;
  2257. dm_block_t block = get_bio_block(tc, bio);
  2258. struct dm_thin_device *td = tc->td;
  2259. struct dm_thin_lookup_result result;
  2260. struct dm_bio_prison_cell *virt_cell, *data_cell;
  2261. struct dm_cell_key key;
  2262. thin_hook_bio(tc, bio);
  2263. if (tc->requeue_mode) {
  2264. bio->bi_status = BLK_STS_DM_REQUEUE;
  2265. bio_endio(bio);
  2266. return DM_MAPIO_SUBMITTED;
  2267. }
  2268. if (get_pool_mode(tc->pool) == PM_FAIL) {
  2269. bio_io_error(bio);
  2270. return DM_MAPIO_SUBMITTED;
  2271. }
  2272. if (op_is_flush(bio->bi_opf) || bio_op(bio) == REQ_OP_DISCARD) {
  2273. thin_defer_bio_with_throttle(tc, bio);
  2274. return DM_MAPIO_SUBMITTED;
  2275. }
  2276. /*
  2277. * We must hold the virtual cell before doing the lookup, otherwise
  2278. * there's a race with discard.
  2279. */
  2280. build_virtual_key(tc->td, block, &key);
  2281. if (bio_detain(tc->pool, &key, bio, &virt_cell))
  2282. return DM_MAPIO_SUBMITTED;
  2283. r = dm_thin_find_block(td, block, 0, &result);
  2284. /*
  2285. * Note that we defer readahead too.
  2286. */
  2287. switch (r) {
  2288. case 0:
  2289. if (unlikely(result.shared)) {
  2290. /*
  2291. * We have a race condition here between the
  2292. * result.shared value returned by the lookup and
  2293. * snapshot creation, which may cause new
  2294. * sharing.
  2295. *
  2296. * To avoid this always quiesce the origin before
  2297. * taking the snap. You want to do this anyway to
  2298. * ensure a consistent application view
  2299. * (i.e. lockfs).
  2300. *
  2301. * More distant ancestors are irrelevant. The
  2302. * shared flag will be set in their case.
  2303. */
  2304. thin_defer_cell(tc, virt_cell);
  2305. return DM_MAPIO_SUBMITTED;
  2306. }
  2307. build_data_key(tc->td, result.block, &key);
  2308. if (bio_detain(tc->pool, &key, bio, &data_cell)) {
  2309. cell_defer_no_holder(tc, virt_cell);
  2310. return DM_MAPIO_SUBMITTED;
  2311. }
  2312. inc_all_io_entry(tc->pool, bio);
  2313. cell_defer_no_holder(tc, data_cell);
  2314. cell_defer_no_holder(tc, virt_cell);
  2315. remap(tc, bio, result.block);
  2316. return DM_MAPIO_REMAPPED;
  2317. case -ENODATA:
  2318. case -EWOULDBLOCK:
  2319. thin_defer_cell(tc, virt_cell);
  2320. return DM_MAPIO_SUBMITTED;
  2321. default:
  2322. /*
  2323. * Must always call bio_io_error on failure.
  2324. * dm_thin_find_block can fail with -EINVAL if the
  2325. * pool is switched to fail-io mode.
  2326. */
  2327. bio_io_error(bio);
  2328. cell_defer_no_holder(tc, virt_cell);
  2329. return DM_MAPIO_SUBMITTED;
  2330. }
  2331. }
  2332. static void requeue_bios(struct pool *pool)
  2333. {
  2334. struct thin_c *tc;
  2335. rcu_read_lock();
  2336. list_for_each_entry_rcu(tc, &pool->active_thins, list) {
  2337. spin_lock_irq(&tc->lock);
  2338. bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
  2339. bio_list_init(&tc->retry_on_resume_list);
  2340. spin_unlock_irq(&tc->lock);
  2341. }
  2342. rcu_read_unlock();
  2343. }
  2344. /*
  2345. *--------------------------------------------------------------
  2346. * Binding of control targets to a pool object
  2347. *--------------------------------------------------------------
  2348. */
  2349. static bool is_factor(sector_t block_size, uint32_t n)
  2350. {
  2351. return !sector_div(block_size, n);
  2352. }
  2353. /*
  2354. * If discard_passdown was enabled verify that the data device
  2355. * supports discards. Disable discard_passdown if not.
  2356. */
  2357. static void disable_discard_passdown_if_not_supported(struct pool_c *pt)
  2358. {
  2359. struct pool *pool = pt->pool;
  2360. struct block_device *data_bdev = pt->data_dev->bdev;
  2361. struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
  2362. const char *reason = NULL;
  2363. if (!pt->adjusted_pf.discard_passdown)
  2364. return;
  2365. if (!bdev_max_discard_sectors(pt->data_dev->bdev))
  2366. reason = "discard unsupported";
  2367. else if (data_limits->max_discard_sectors < pool->sectors_per_block)
  2368. reason = "max discard sectors smaller than a block";
  2369. if (reason) {
  2370. DMWARN("Data device (%pg) %s: Disabling discard passdown.", data_bdev, reason);
  2371. pt->adjusted_pf.discard_passdown = false;
  2372. }
  2373. }
  2374. static int bind_control_target(struct pool *pool, struct dm_target *ti)
  2375. {
  2376. struct pool_c *pt = ti->private;
  2377. /*
  2378. * We want to make sure that a pool in PM_FAIL mode is never upgraded.
  2379. */
  2380. enum pool_mode old_mode = get_pool_mode(pool);
  2381. enum pool_mode new_mode = pt->adjusted_pf.mode;
  2382. /*
  2383. * Don't change the pool's mode until set_pool_mode() below.
  2384. * Otherwise the pool's process_* function pointers may
  2385. * not match the desired pool mode.
  2386. */
  2387. pt->adjusted_pf.mode = old_mode;
  2388. pool->ti = ti;
  2389. pool->pf = pt->adjusted_pf;
  2390. pool->low_water_blocks = pt->low_water_blocks;
  2391. set_pool_mode(pool, new_mode);
  2392. return 0;
  2393. }
  2394. static void unbind_control_target(struct pool *pool, struct dm_target *ti)
  2395. {
  2396. if (pool->ti == ti)
  2397. pool->ti = NULL;
  2398. }
  2399. /*
  2400. *--------------------------------------------------------------
  2401. * Pool creation
  2402. *--------------------------------------------------------------
  2403. */
  2404. /* Initialize pool features. */
  2405. static void pool_features_init(struct pool_features *pf)
  2406. {
  2407. pf->mode = PM_WRITE;
  2408. pf->zero_new_blocks = true;
  2409. pf->discard_enabled = true;
  2410. pf->discard_passdown = true;
  2411. pf->error_if_no_space = false;
  2412. }
  2413. static void __pool_destroy(struct pool *pool)
  2414. {
  2415. __pool_table_remove(pool);
  2416. vfree(pool->cell_sort_array);
  2417. if (dm_pool_metadata_close(pool->pmd) < 0)
  2418. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  2419. dm_bio_prison_destroy(pool->prison);
  2420. dm_kcopyd_client_destroy(pool->copier);
  2421. cancel_delayed_work_sync(&pool->waker);
  2422. cancel_delayed_work_sync(&pool->no_space_timeout);
  2423. if (pool->wq)
  2424. destroy_workqueue(pool->wq);
  2425. if (pool->next_mapping)
  2426. mempool_free(pool->next_mapping, &pool->mapping_pool);
  2427. mempool_exit(&pool->mapping_pool);
  2428. dm_deferred_set_destroy(pool->shared_read_ds);
  2429. dm_deferred_set_destroy(pool->all_io_ds);
  2430. kfree(pool);
  2431. }
  2432. static struct kmem_cache *_new_mapping_cache;
  2433. static struct pool *pool_create(struct mapped_device *pool_md,
  2434. struct block_device *metadata_dev,
  2435. struct block_device *data_dev,
  2436. unsigned long block_size,
  2437. int read_only, char **error)
  2438. {
  2439. int r;
  2440. void *err_p;
  2441. struct pool *pool;
  2442. struct dm_pool_metadata *pmd;
  2443. bool format_device = read_only ? false : true;
  2444. pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
  2445. if (IS_ERR(pmd)) {
  2446. *error = "Error creating metadata object";
  2447. return ERR_CAST(pmd);
  2448. }
  2449. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  2450. if (!pool) {
  2451. *error = "Error allocating memory for pool";
  2452. err_p = ERR_PTR(-ENOMEM);
  2453. goto bad_pool;
  2454. }
  2455. pool->pmd = pmd;
  2456. pool->sectors_per_block = block_size;
  2457. if (block_size & (block_size - 1))
  2458. pool->sectors_per_block_shift = -1;
  2459. else
  2460. pool->sectors_per_block_shift = __ffs(block_size);
  2461. pool->low_water_blocks = 0;
  2462. pool_features_init(&pool->pf);
  2463. pool->prison = dm_bio_prison_create();
  2464. if (!pool->prison) {
  2465. *error = "Error creating pool's bio prison";
  2466. err_p = ERR_PTR(-ENOMEM);
  2467. goto bad_prison;
  2468. }
  2469. pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  2470. if (IS_ERR(pool->copier)) {
  2471. r = PTR_ERR(pool->copier);
  2472. *error = "Error creating pool's kcopyd client";
  2473. err_p = ERR_PTR(r);
  2474. goto bad_kcopyd_client;
  2475. }
  2476. /*
  2477. * Create singlethreaded workqueue that will service all devices
  2478. * that use this metadata.
  2479. */
  2480. pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  2481. if (!pool->wq) {
  2482. *error = "Error creating pool's workqueue";
  2483. err_p = ERR_PTR(-ENOMEM);
  2484. goto bad_wq;
  2485. }
  2486. throttle_init(&pool->throttle);
  2487. INIT_WORK(&pool->worker, do_worker);
  2488. INIT_DELAYED_WORK(&pool->waker, do_waker);
  2489. INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
  2490. spin_lock_init(&pool->lock);
  2491. bio_list_init(&pool->deferred_flush_bios);
  2492. bio_list_init(&pool->deferred_flush_completions);
  2493. INIT_LIST_HEAD(&pool->prepared_mappings);
  2494. INIT_LIST_HEAD(&pool->prepared_discards);
  2495. INIT_LIST_HEAD(&pool->prepared_discards_pt2);
  2496. INIT_LIST_HEAD(&pool->active_thins);
  2497. pool->low_water_triggered = false;
  2498. pool->suspended = true;
  2499. pool->out_of_data_space = false;
  2500. pool->shared_read_ds = dm_deferred_set_create();
  2501. if (!pool->shared_read_ds) {
  2502. *error = "Error creating pool's shared read deferred set";
  2503. err_p = ERR_PTR(-ENOMEM);
  2504. goto bad_shared_read_ds;
  2505. }
  2506. pool->all_io_ds = dm_deferred_set_create();
  2507. if (!pool->all_io_ds) {
  2508. *error = "Error creating pool's all io deferred set";
  2509. err_p = ERR_PTR(-ENOMEM);
  2510. goto bad_all_io_ds;
  2511. }
  2512. pool->next_mapping = NULL;
  2513. r = mempool_init_slab_pool(&pool->mapping_pool, MAPPING_POOL_SIZE,
  2514. _new_mapping_cache);
  2515. if (r) {
  2516. *error = "Error creating pool's mapping mempool";
  2517. err_p = ERR_PTR(r);
  2518. goto bad_mapping_pool;
  2519. }
  2520. pool->cell_sort_array =
  2521. vmalloc(array_size(CELL_SORT_ARRAY_SIZE,
  2522. sizeof(*pool->cell_sort_array)));
  2523. if (!pool->cell_sort_array) {
  2524. *error = "Error allocating cell sort array";
  2525. err_p = ERR_PTR(-ENOMEM);
  2526. goto bad_sort_array;
  2527. }
  2528. pool->ref_count = 1;
  2529. pool->last_commit_jiffies = jiffies;
  2530. pool->pool_md = pool_md;
  2531. pool->md_dev = metadata_dev;
  2532. pool->data_dev = data_dev;
  2533. __pool_table_insert(pool);
  2534. return pool;
  2535. bad_sort_array:
  2536. mempool_exit(&pool->mapping_pool);
  2537. bad_mapping_pool:
  2538. dm_deferred_set_destroy(pool->all_io_ds);
  2539. bad_all_io_ds:
  2540. dm_deferred_set_destroy(pool->shared_read_ds);
  2541. bad_shared_read_ds:
  2542. destroy_workqueue(pool->wq);
  2543. bad_wq:
  2544. dm_kcopyd_client_destroy(pool->copier);
  2545. bad_kcopyd_client:
  2546. dm_bio_prison_destroy(pool->prison);
  2547. bad_prison:
  2548. kfree(pool);
  2549. bad_pool:
  2550. if (dm_pool_metadata_close(pmd))
  2551. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  2552. return err_p;
  2553. }
  2554. static void __pool_inc(struct pool *pool)
  2555. {
  2556. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  2557. pool->ref_count++;
  2558. }
  2559. static void __pool_dec(struct pool *pool)
  2560. {
  2561. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  2562. BUG_ON(!pool->ref_count);
  2563. if (!--pool->ref_count)
  2564. __pool_destroy(pool);
  2565. }
  2566. static struct pool *__pool_find(struct mapped_device *pool_md,
  2567. struct block_device *metadata_dev,
  2568. struct block_device *data_dev,
  2569. unsigned long block_size, int read_only,
  2570. char **error, int *created)
  2571. {
  2572. struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
  2573. if (pool) {
  2574. if (pool->pool_md != pool_md) {
  2575. *error = "metadata device already in use by a pool";
  2576. return ERR_PTR(-EBUSY);
  2577. }
  2578. if (pool->data_dev != data_dev) {
  2579. *error = "data device already in use by a pool";
  2580. return ERR_PTR(-EBUSY);
  2581. }
  2582. __pool_inc(pool);
  2583. } else {
  2584. pool = __pool_table_lookup(pool_md);
  2585. if (pool) {
  2586. if (pool->md_dev != metadata_dev || pool->data_dev != data_dev) {
  2587. *error = "different pool cannot replace a pool";
  2588. return ERR_PTR(-EINVAL);
  2589. }
  2590. __pool_inc(pool);
  2591. } else {
  2592. pool = pool_create(pool_md, metadata_dev, data_dev, block_size, read_only, error);
  2593. *created = 1;
  2594. }
  2595. }
  2596. return pool;
  2597. }
  2598. /*
  2599. *--------------------------------------------------------------
  2600. * Pool target methods
  2601. *--------------------------------------------------------------
  2602. */
  2603. static void pool_dtr(struct dm_target *ti)
  2604. {
  2605. struct pool_c *pt = ti->private;
  2606. mutex_lock(&dm_thin_pool_table.mutex);
  2607. unbind_control_target(pt->pool, ti);
  2608. __pool_dec(pt->pool);
  2609. dm_put_device(ti, pt->metadata_dev);
  2610. dm_put_device(ti, pt->data_dev);
  2611. kfree(pt);
  2612. mutex_unlock(&dm_thin_pool_table.mutex);
  2613. }
  2614. static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
  2615. struct dm_target *ti)
  2616. {
  2617. int r;
  2618. unsigned int argc;
  2619. const char *arg_name;
  2620. static const struct dm_arg _args[] = {
  2621. {0, 4, "Invalid number of pool feature arguments"},
  2622. };
  2623. /*
  2624. * No feature arguments supplied.
  2625. */
  2626. if (!as->argc)
  2627. return 0;
  2628. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  2629. if (r)
  2630. return -EINVAL;
  2631. while (argc && !r) {
  2632. arg_name = dm_shift_arg(as);
  2633. argc--;
  2634. if (!strcasecmp(arg_name, "skip_block_zeroing"))
  2635. pf->zero_new_blocks = false;
  2636. else if (!strcasecmp(arg_name, "ignore_discard"))
  2637. pf->discard_enabled = false;
  2638. else if (!strcasecmp(arg_name, "no_discard_passdown"))
  2639. pf->discard_passdown = false;
  2640. else if (!strcasecmp(arg_name, "read_only"))
  2641. pf->mode = PM_READ_ONLY;
  2642. else if (!strcasecmp(arg_name, "error_if_no_space"))
  2643. pf->error_if_no_space = true;
  2644. else {
  2645. ti->error = "Unrecognised pool feature requested";
  2646. r = -EINVAL;
  2647. break;
  2648. }
  2649. }
  2650. return r;
  2651. }
  2652. static void metadata_low_callback(void *context)
  2653. {
  2654. struct pool *pool = context;
  2655. DMWARN("%s: reached low water mark for metadata device: sending event.",
  2656. dm_device_name(pool->pool_md));
  2657. dm_table_event(pool->ti->table);
  2658. }
  2659. /*
  2660. * We need to flush the data device **before** committing the metadata.
  2661. *
  2662. * This ensures that the data blocks of any newly inserted mappings are
  2663. * properly written to non-volatile storage and won't be lost in case of a
  2664. * crash.
  2665. *
  2666. * Failure to do so can result in data corruption in the case of internal or
  2667. * external snapshots and in the case of newly provisioned blocks, when block
  2668. * zeroing is enabled.
  2669. */
  2670. static int metadata_pre_commit_callback(void *context)
  2671. {
  2672. struct pool *pool = context;
  2673. return blkdev_issue_flush(pool->data_dev);
  2674. }
  2675. static sector_t get_dev_size(struct block_device *bdev)
  2676. {
  2677. return bdev_nr_sectors(bdev);
  2678. }
  2679. static void warn_if_metadata_device_too_big(struct block_device *bdev)
  2680. {
  2681. sector_t metadata_dev_size = get_dev_size(bdev);
  2682. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
  2683. DMWARN("Metadata device %pg is larger than %u sectors: excess space will not be used.",
  2684. bdev, THIN_METADATA_MAX_SECTORS);
  2685. }
  2686. static sector_t get_metadata_dev_size(struct block_device *bdev)
  2687. {
  2688. sector_t metadata_dev_size = get_dev_size(bdev);
  2689. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
  2690. metadata_dev_size = THIN_METADATA_MAX_SECTORS;
  2691. return metadata_dev_size;
  2692. }
  2693. static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
  2694. {
  2695. sector_t metadata_dev_size = get_metadata_dev_size(bdev);
  2696. sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
  2697. return metadata_dev_size;
  2698. }
  2699. /*
  2700. * When a metadata threshold is crossed a dm event is triggered, and
  2701. * userland should respond by growing the metadata device. We could let
  2702. * userland set the threshold, like we do with the data threshold, but I'm
  2703. * not sure they know enough to do this well.
  2704. */
  2705. static dm_block_t calc_metadata_threshold(struct pool_c *pt)
  2706. {
  2707. /*
  2708. * 4M is ample for all ops with the possible exception of thin
  2709. * device deletion which is harmless if it fails (just retry the
  2710. * delete after you've grown the device).
  2711. */
  2712. dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
  2713. return min((dm_block_t)1024ULL /* 4M */, quarter);
  2714. }
  2715. /*
  2716. * thin-pool <metadata dev> <data dev>
  2717. * <data block size (sectors)>
  2718. * <low water mark (blocks)>
  2719. * [<#feature args> [<arg>]*]
  2720. *
  2721. * Optional feature arguments are:
  2722. * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
  2723. * ignore_discard: disable discard
  2724. * no_discard_passdown: don't pass discards down to the data device
  2725. * read_only: Don't allow any changes to be made to the pool metadata.
  2726. * error_if_no_space: error IOs, instead of queueing, if no space.
  2727. */
  2728. static int pool_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  2729. {
  2730. int r, pool_created = 0;
  2731. struct pool_c *pt;
  2732. struct pool *pool;
  2733. struct pool_features pf;
  2734. struct dm_arg_set as;
  2735. struct dm_dev *data_dev;
  2736. unsigned long block_size;
  2737. dm_block_t low_water_blocks;
  2738. struct dm_dev *metadata_dev;
  2739. blk_mode_t metadata_mode;
  2740. /*
  2741. * FIXME Remove validation from scope of lock.
  2742. */
  2743. mutex_lock(&dm_thin_pool_table.mutex);
  2744. if (argc < 4) {
  2745. ti->error = "Invalid argument count";
  2746. r = -EINVAL;
  2747. goto out_unlock;
  2748. }
  2749. as.argc = argc;
  2750. as.argv = argv;
  2751. /* make sure metadata and data are different devices */
  2752. if (!strcmp(argv[0], argv[1])) {
  2753. ti->error = "Error setting metadata or data device";
  2754. r = -EINVAL;
  2755. goto out_unlock;
  2756. }
  2757. /*
  2758. * Set default pool features.
  2759. */
  2760. pool_features_init(&pf);
  2761. dm_consume_args(&as, 4);
  2762. r = parse_pool_features(&as, &pf, ti);
  2763. if (r)
  2764. goto out_unlock;
  2765. metadata_mode = BLK_OPEN_READ |
  2766. ((pf.mode == PM_READ_ONLY) ? 0 : BLK_OPEN_WRITE);
  2767. r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
  2768. if (r) {
  2769. ti->error = "Error opening metadata block device";
  2770. goto out_unlock;
  2771. }
  2772. warn_if_metadata_device_too_big(metadata_dev->bdev);
  2773. r = dm_get_device(ti, argv[1], BLK_OPEN_READ | BLK_OPEN_WRITE, &data_dev);
  2774. if (r) {
  2775. ti->error = "Error getting data device";
  2776. goto out_metadata;
  2777. }
  2778. if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
  2779. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  2780. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  2781. block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
  2782. ti->error = "Invalid block size";
  2783. r = -EINVAL;
  2784. goto out;
  2785. }
  2786. if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
  2787. ti->error = "Invalid low water mark";
  2788. r = -EINVAL;
  2789. goto out;
  2790. }
  2791. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  2792. if (!pt) {
  2793. r = -ENOMEM;
  2794. goto out;
  2795. }
  2796. pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev, data_dev->bdev,
  2797. block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
  2798. if (IS_ERR(pool)) {
  2799. r = PTR_ERR(pool);
  2800. goto out_free_pt;
  2801. }
  2802. /*
  2803. * 'pool_created' reflects whether this is the first table load.
  2804. * Top level discard support is not allowed to be changed after
  2805. * initial load. This would require a pool reload to trigger thin
  2806. * device changes.
  2807. */
  2808. if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
  2809. ti->error = "Discard support cannot be disabled once enabled";
  2810. r = -EINVAL;
  2811. goto out_flags_changed;
  2812. }
  2813. pt->pool = pool;
  2814. pt->ti = ti;
  2815. pt->metadata_dev = metadata_dev;
  2816. pt->data_dev = data_dev;
  2817. pt->low_water_blocks = low_water_blocks;
  2818. pt->adjusted_pf = pt->requested_pf = pf;
  2819. ti->num_flush_bios = 1;
  2820. ti->limit_swap_bios = true;
  2821. /*
  2822. * Only need to enable discards if the pool should pass
  2823. * them down to the data device. The thin device's discard
  2824. * processing will cause mappings to be removed from the btree.
  2825. */
  2826. if (pf.discard_enabled && pf.discard_passdown) {
  2827. ti->num_discard_bios = 1;
  2828. /*
  2829. * Setting 'discards_supported' circumvents the normal
  2830. * stacking of discard limits (this keeps the pool and
  2831. * thin devices' discard limits consistent).
  2832. */
  2833. ti->discards_supported = true;
  2834. ti->max_discard_granularity = true;
  2835. }
  2836. ti->private = pt;
  2837. r = dm_pool_register_metadata_threshold(pt->pool->pmd,
  2838. calc_metadata_threshold(pt),
  2839. metadata_low_callback,
  2840. pool);
  2841. if (r) {
  2842. ti->error = "Error registering metadata threshold";
  2843. goto out_flags_changed;
  2844. }
  2845. dm_pool_register_pre_commit_callback(pool->pmd,
  2846. metadata_pre_commit_callback, pool);
  2847. mutex_unlock(&dm_thin_pool_table.mutex);
  2848. return 0;
  2849. out_flags_changed:
  2850. __pool_dec(pool);
  2851. out_free_pt:
  2852. kfree(pt);
  2853. out:
  2854. dm_put_device(ti, data_dev);
  2855. out_metadata:
  2856. dm_put_device(ti, metadata_dev);
  2857. out_unlock:
  2858. mutex_unlock(&dm_thin_pool_table.mutex);
  2859. return r;
  2860. }
  2861. static int pool_map(struct dm_target *ti, struct bio *bio)
  2862. {
  2863. struct pool_c *pt = ti->private;
  2864. struct pool *pool = pt->pool;
  2865. /*
  2866. * As this is a singleton target, ti->begin is always zero.
  2867. */
  2868. spin_lock_irq(&pool->lock);
  2869. bio_set_dev(bio, pt->data_dev->bdev);
  2870. spin_unlock_irq(&pool->lock);
  2871. return DM_MAPIO_REMAPPED;
  2872. }
  2873. static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
  2874. {
  2875. int r;
  2876. struct pool_c *pt = ti->private;
  2877. struct pool *pool = pt->pool;
  2878. sector_t data_size = ti->len;
  2879. dm_block_t sb_data_size;
  2880. *need_commit = false;
  2881. (void) sector_div(data_size, pool->sectors_per_block);
  2882. r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
  2883. if (r) {
  2884. DMERR("%s: failed to retrieve data device size",
  2885. dm_device_name(pool->pool_md));
  2886. return r;
  2887. }
  2888. if (data_size < sb_data_size) {
  2889. DMERR("%s: pool target (%llu blocks) too small: expected %llu",
  2890. dm_device_name(pool->pool_md),
  2891. (unsigned long long)data_size, sb_data_size);
  2892. return -EINVAL;
  2893. } else if (data_size > sb_data_size) {
  2894. if (dm_pool_metadata_needs_check(pool->pmd)) {
  2895. DMERR("%s: unable to grow the data device until repaired.",
  2896. dm_device_name(pool->pool_md));
  2897. return 0;
  2898. }
  2899. if (sb_data_size)
  2900. DMINFO("%s: growing the data device from %llu to %llu blocks",
  2901. dm_device_name(pool->pool_md),
  2902. sb_data_size, (unsigned long long)data_size);
  2903. r = dm_pool_resize_data_dev(pool->pmd, data_size);
  2904. if (r) {
  2905. metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
  2906. return r;
  2907. }
  2908. *need_commit = true;
  2909. }
  2910. return 0;
  2911. }
  2912. static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
  2913. {
  2914. int r;
  2915. struct pool_c *pt = ti->private;
  2916. struct pool *pool = pt->pool;
  2917. dm_block_t metadata_dev_size, sb_metadata_dev_size;
  2918. *need_commit = false;
  2919. metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
  2920. r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
  2921. if (r) {
  2922. DMERR("%s: failed to retrieve metadata device size",
  2923. dm_device_name(pool->pool_md));
  2924. return r;
  2925. }
  2926. if (metadata_dev_size < sb_metadata_dev_size) {
  2927. DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
  2928. dm_device_name(pool->pool_md),
  2929. metadata_dev_size, sb_metadata_dev_size);
  2930. return -EINVAL;
  2931. } else if (metadata_dev_size > sb_metadata_dev_size) {
  2932. if (dm_pool_metadata_needs_check(pool->pmd)) {
  2933. DMERR("%s: unable to grow the metadata device until repaired.",
  2934. dm_device_name(pool->pool_md));
  2935. return 0;
  2936. }
  2937. warn_if_metadata_device_too_big(pool->md_dev);
  2938. DMINFO("%s: growing the metadata device from %llu to %llu blocks",
  2939. dm_device_name(pool->pool_md),
  2940. sb_metadata_dev_size, metadata_dev_size);
  2941. if (get_pool_mode(pool) == PM_OUT_OF_METADATA_SPACE)
  2942. set_pool_mode(pool, PM_WRITE);
  2943. r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
  2944. if (r) {
  2945. metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
  2946. return r;
  2947. }
  2948. *need_commit = true;
  2949. }
  2950. return 0;
  2951. }
  2952. /*
  2953. * Retrieves the number of blocks of the data device from
  2954. * the superblock and compares it to the actual device size,
  2955. * thus resizing the data device in case it has grown.
  2956. *
  2957. * This both copes with opening preallocated data devices in the ctr
  2958. * being followed by a resume
  2959. * -and-
  2960. * calling the resume method individually after userspace has
  2961. * grown the data device in reaction to a table event.
  2962. */
  2963. static int pool_preresume(struct dm_target *ti)
  2964. {
  2965. int r;
  2966. bool need_commit1, need_commit2;
  2967. struct pool_c *pt = ti->private;
  2968. struct pool *pool = pt->pool;
  2969. /*
  2970. * Take control of the pool object.
  2971. */
  2972. r = bind_control_target(pool, ti);
  2973. if (r)
  2974. goto out;
  2975. r = maybe_resize_data_dev(ti, &need_commit1);
  2976. if (r)
  2977. goto out;
  2978. r = maybe_resize_metadata_dev(ti, &need_commit2);
  2979. if (r)
  2980. goto out;
  2981. if (need_commit1 || need_commit2)
  2982. (void) commit(pool);
  2983. out:
  2984. /*
  2985. * When a thin-pool is PM_FAIL, it cannot be rebuilt if
  2986. * bio is in deferred list. Therefore need to return 0
  2987. * to allow pool_resume() to flush IO.
  2988. */
  2989. if (r && get_pool_mode(pool) == PM_FAIL)
  2990. r = 0;
  2991. return r;
  2992. }
  2993. static void pool_suspend_active_thins(struct pool *pool)
  2994. {
  2995. struct thin_c *tc;
  2996. /* Suspend all active thin devices */
  2997. tc = get_first_thin(pool);
  2998. while (tc) {
  2999. dm_internal_suspend_noflush(tc->thin_md);
  3000. tc = get_next_thin(pool, tc);
  3001. }
  3002. }
  3003. static void pool_resume_active_thins(struct pool *pool)
  3004. {
  3005. struct thin_c *tc;
  3006. /* Resume all active thin devices */
  3007. tc = get_first_thin(pool);
  3008. while (tc) {
  3009. dm_internal_resume(tc->thin_md);
  3010. tc = get_next_thin(pool, tc);
  3011. }
  3012. }
  3013. static void pool_resume(struct dm_target *ti)
  3014. {
  3015. struct pool_c *pt = ti->private;
  3016. struct pool *pool = pt->pool;
  3017. /*
  3018. * Must requeue active_thins' bios and then resume
  3019. * active_thins _before_ clearing 'suspend' flag.
  3020. */
  3021. requeue_bios(pool);
  3022. pool_resume_active_thins(pool);
  3023. spin_lock_irq(&pool->lock);
  3024. pool->low_water_triggered = false;
  3025. pool->suspended = false;
  3026. spin_unlock_irq(&pool->lock);
  3027. do_waker(&pool->waker.work);
  3028. }
  3029. static void pool_presuspend(struct dm_target *ti)
  3030. {
  3031. struct pool_c *pt = ti->private;
  3032. struct pool *pool = pt->pool;
  3033. spin_lock_irq(&pool->lock);
  3034. pool->suspended = true;
  3035. spin_unlock_irq(&pool->lock);
  3036. pool_suspend_active_thins(pool);
  3037. }
  3038. static void pool_presuspend_undo(struct dm_target *ti)
  3039. {
  3040. struct pool_c *pt = ti->private;
  3041. struct pool *pool = pt->pool;
  3042. pool_resume_active_thins(pool);
  3043. spin_lock_irq(&pool->lock);
  3044. pool->suspended = false;
  3045. spin_unlock_irq(&pool->lock);
  3046. }
  3047. static void pool_postsuspend(struct dm_target *ti)
  3048. {
  3049. struct pool_c *pt = ti->private;
  3050. struct pool *pool = pt->pool;
  3051. cancel_delayed_work_sync(&pool->waker);
  3052. cancel_delayed_work_sync(&pool->no_space_timeout);
  3053. flush_workqueue(pool->wq);
  3054. (void) commit(pool);
  3055. }
  3056. static int check_arg_count(unsigned int argc, unsigned int args_required)
  3057. {
  3058. if (argc != args_required) {
  3059. DMWARN("Message received with %u arguments instead of %u.",
  3060. argc, args_required);
  3061. return -EINVAL;
  3062. }
  3063. return 0;
  3064. }
  3065. static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
  3066. {
  3067. if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
  3068. *dev_id <= MAX_DEV_ID)
  3069. return 0;
  3070. if (warning)
  3071. DMWARN("Message received with invalid device id: %s", arg);
  3072. return -EINVAL;
  3073. }
  3074. static int process_create_thin_mesg(unsigned int argc, char **argv, struct pool *pool)
  3075. {
  3076. dm_thin_id dev_id;
  3077. int r;
  3078. r = check_arg_count(argc, 2);
  3079. if (r)
  3080. return r;
  3081. r = read_dev_id(argv[1], &dev_id, 1);
  3082. if (r)
  3083. return r;
  3084. r = dm_pool_create_thin(pool->pmd, dev_id);
  3085. if (r) {
  3086. DMWARN("Creation of new thinly-provisioned device with id %s failed.",
  3087. argv[1]);
  3088. return r;
  3089. }
  3090. return 0;
  3091. }
  3092. static int process_create_snap_mesg(unsigned int argc, char **argv, struct pool *pool)
  3093. {
  3094. dm_thin_id dev_id;
  3095. dm_thin_id origin_dev_id;
  3096. int r;
  3097. r = check_arg_count(argc, 3);
  3098. if (r)
  3099. return r;
  3100. r = read_dev_id(argv[1], &dev_id, 1);
  3101. if (r)
  3102. return r;
  3103. r = read_dev_id(argv[2], &origin_dev_id, 1);
  3104. if (r)
  3105. return r;
  3106. r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
  3107. if (r) {
  3108. DMWARN("Creation of new snapshot %s of device %s failed.",
  3109. argv[1], argv[2]);
  3110. return r;
  3111. }
  3112. return 0;
  3113. }
  3114. static int process_delete_mesg(unsigned int argc, char **argv, struct pool *pool)
  3115. {
  3116. dm_thin_id dev_id;
  3117. int r;
  3118. r = check_arg_count(argc, 2);
  3119. if (r)
  3120. return r;
  3121. r = read_dev_id(argv[1], &dev_id, 1);
  3122. if (r)
  3123. return r;
  3124. r = dm_pool_delete_thin_device(pool->pmd, dev_id);
  3125. if (r)
  3126. DMWARN("Deletion of thin device %s failed.", argv[1]);
  3127. return r;
  3128. }
  3129. static int process_set_transaction_id_mesg(unsigned int argc, char **argv, struct pool *pool)
  3130. {
  3131. dm_thin_id old_id, new_id;
  3132. int r;
  3133. r = check_arg_count(argc, 3);
  3134. if (r)
  3135. return r;
  3136. if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
  3137. DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
  3138. return -EINVAL;
  3139. }
  3140. if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
  3141. DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
  3142. return -EINVAL;
  3143. }
  3144. r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
  3145. if (r) {
  3146. DMWARN("Failed to change transaction id from %s to %s.",
  3147. argv[1], argv[2]);
  3148. return r;
  3149. }
  3150. return 0;
  3151. }
  3152. static int process_reserve_metadata_snap_mesg(unsigned int argc, char **argv, struct pool *pool)
  3153. {
  3154. int r;
  3155. r = check_arg_count(argc, 1);
  3156. if (r)
  3157. return r;
  3158. (void) commit(pool);
  3159. r = dm_pool_reserve_metadata_snap(pool->pmd);
  3160. if (r)
  3161. DMWARN("reserve_metadata_snap message failed.");
  3162. return r;
  3163. }
  3164. static int process_release_metadata_snap_mesg(unsigned int argc, char **argv, struct pool *pool)
  3165. {
  3166. int r;
  3167. r = check_arg_count(argc, 1);
  3168. if (r)
  3169. return r;
  3170. r = dm_pool_release_metadata_snap(pool->pmd);
  3171. if (r)
  3172. DMWARN("release_metadata_snap message failed.");
  3173. return r;
  3174. }
  3175. /*
  3176. * Messages supported:
  3177. * create_thin <dev_id>
  3178. * create_snap <dev_id> <origin_id>
  3179. * delete <dev_id>
  3180. * set_transaction_id <current_trans_id> <new_trans_id>
  3181. * reserve_metadata_snap
  3182. * release_metadata_snap
  3183. */
  3184. static int pool_message(struct dm_target *ti, unsigned int argc, char **argv,
  3185. char *result, unsigned int maxlen)
  3186. {
  3187. int r = -EINVAL;
  3188. struct pool_c *pt = ti->private;
  3189. struct pool *pool = pt->pool;
  3190. if (get_pool_mode(pool) >= PM_OUT_OF_METADATA_SPACE) {
  3191. DMERR("%s: unable to service pool target messages in READ_ONLY or FAIL mode",
  3192. dm_device_name(pool->pool_md));
  3193. return -EOPNOTSUPP;
  3194. }
  3195. if (!strcasecmp(argv[0], "create_thin"))
  3196. r = process_create_thin_mesg(argc, argv, pool);
  3197. else if (!strcasecmp(argv[0], "create_snap"))
  3198. r = process_create_snap_mesg(argc, argv, pool);
  3199. else if (!strcasecmp(argv[0], "delete"))
  3200. r = process_delete_mesg(argc, argv, pool);
  3201. else if (!strcasecmp(argv[0], "set_transaction_id"))
  3202. r = process_set_transaction_id_mesg(argc, argv, pool);
  3203. else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
  3204. r = process_reserve_metadata_snap_mesg(argc, argv, pool);
  3205. else if (!strcasecmp(argv[0], "release_metadata_snap"))
  3206. r = process_release_metadata_snap_mesg(argc, argv, pool);
  3207. else
  3208. DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
  3209. if (!r)
  3210. (void) commit(pool);
  3211. return r;
  3212. }
  3213. static void emit_flags(struct pool_features *pf, char *result,
  3214. unsigned int sz, unsigned int maxlen)
  3215. {
  3216. unsigned int count = !pf->zero_new_blocks + !pf->discard_enabled +
  3217. !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
  3218. pf->error_if_no_space;
  3219. DMEMIT("%u ", count);
  3220. if (!pf->zero_new_blocks)
  3221. DMEMIT("skip_block_zeroing ");
  3222. if (!pf->discard_enabled)
  3223. DMEMIT("ignore_discard ");
  3224. if (!pf->discard_passdown)
  3225. DMEMIT("no_discard_passdown ");
  3226. if (pf->mode == PM_READ_ONLY)
  3227. DMEMIT("read_only ");
  3228. if (pf->error_if_no_space)
  3229. DMEMIT("error_if_no_space ");
  3230. }
  3231. /*
  3232. * Status line is:
  3233. * <transaction id> <used metadata sectors>/<total metadata sectors>
  3234. * <used data sectors>/<total data sectors> <held metadata root>
  3235. * <pool mode> <discard config> <no space config> <needs_check>
  3236. */
  3237. static void pool_status(struct dm_target *ti, status_type_t type,
  3238. unsigned int status_flags, char *result, unsigned int maxlen)
  3239. {
  3240. int r;
  3241. unsigned int sz = 0;
  3242. uint64_t transaction_id;
  3243. dm_block_t nr_free_blocks_data;
  3244. dm_block_t nr_free_blocks_metadata;
  3245. dm_block_t nr_blocks_data;
  3246. dm_block_t nr_blocks_metadata;
  3247. dm_block_t held_root;
  3248. enum pool_mode mode;
  3249. char buf[BDEVNAME_SIZE];
  3250. char buf2[BDEVNAME_SIZE];
  3251. struct pool_c *pt = ti->private;
  3252. struct pool *pool = pt->pool;
  3253. switch (type) {
  3254. case STATUSTYPE_INFO:
  3255. if (get_pool_mode(pool) == PM_FAIL) {
  3256. DMEMIT("Fail");
  3257. break;
  3258. }
  3259. /* Commit to ensure statistics aren't out-of-date */
  3260. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
  3261. (void) commit(pool);
  3262. r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
  3263. if (r) {
  3264. DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
  3265. dm_device_name(pool->pool_md), r);
  3266. goto err;
  3267. }
  3268. r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
  3269. if (r) {
  3270. DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
  3271. dm_device_name(pool->pool_md), r);
  3272. goto err;
  3273. }
  3274. r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
  3275. if (r) {
  3276. DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
  3277. dm_device_name(pool->pool_md), r);
  3278. goto err;
  3279. }
  3280. r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
  3281. if (r) {
  3282. DMERR("%s: dm_pool_get_free_block_count returned %d",
  3283. dm_device_name(pool->pool_md), r);
  3284. goto err;
  3285. }
  3286. r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
  3287. if (r) {
  3288. DMERR("%s: dm_pool_get_data_dev_size returned %d",
  3289. dm_device_name(pool->pool_md), r);
  3290. goto err;
  3291. }
  3292. r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
  3293. if (r) {
  3294. DMERR("%s: dm_pool_get_metadata_snap returned %d",
  3295. dm_device_name(pool->pool_md), r);
  3296. goto err;
  3297. }
  3298. DMEMIT("%llu %llu/%llu %llu/%llu ",
  3299. (unsigned long long)transaction_id,
  3300. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  3301. (unsigned long long)nr_blocks_metadata,
  3302. (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
  3303. (unsigned long long)nr_blocks_data);
  3304. if (held_root)
  3305. DMEMIT("%llu ", held_root);
  3306. else
  3307. DMEMIT("- ");
  3308. mode = get_pool_mode(pool);
  3309. if (mode == PM_OUT_OF_DATA_SPACE)
  3310. DMEMIT("out_of_data_space ");
  3311. else if (is_read_only_pool_mode(mode))
  3312. DMEMIT("ro ");
  3313. else
  3314. DMEMIT("rw ");
  3315. if (!pool->pf.discard_enabled)
  3316. DMEMIT("ignore_discard ");
  3317. else if (pool->pf.discard_passdown)
  3318. DMEMIT("discard_passdown ");
  3319. else
  3320. DMEMIT("no_discard_passdown ");
  3321. if (pool->pf.error_if_no_space)
  3322. DMEMIT("error_if_no_space ");
  3323. else
  3324. DMEMIT("queue_if_no_space ");
  3325. if (dm_pool_metadata_needs_check(pool->pmd))
  3326. DMEMIT("needs_check ");
  3327. else
  3328. DMEMIT("- ");
  3329. DMEMIT("%llu ", (unsigned long long)calc_metadata_threshold(pt));
  3330. break;
  3331. case STATUSTYPE_TABLE:
  3332. DMEMIT("%s %s %lu %llu ",
  3333. format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
  3334. format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
  3335. (unsigned long)pool->sectors_per_block,
  3336. (unsigned long long)pt->low_water_blocks);
  3337. emit_flags(&pt->requested_pf, result, sz, maxlen);
  3338. break;
  3339. case STATUSTYPE_IMA:
  3340. *result = '\0';
  3341. break;
  3342. }
  3343. return;
  3344. err:
  3345. DMEMIT("Error");
  3346. }
  3347. static int pool_iterate_devices(struct dm_target *ti,
  3348. iterate_devices_callout_fn fn, void *data)
  3349. {
  3350. struct pool_c *pt = ti->private;
  3351. return fn(ti, pt->data_dev, 0, ti->len, data);
  3352. }
  3353. static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
  3354. {
  3355. struct pool_c *pt = ti->private;
  3356. struct pool *pool = pt->pool;
  3357. sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  3358. /*
  3359. * If max_sectors is smaller than pool->sectors_per_block adjust it
  3360. * to the highest possible power-of-2 factor of pool->sectors_per_block.
  3361. * This is especially beneficial when the pool's data device is a RAID
  3362. * device that has a full stripe width that matches pool->sectors_per_block
  3363. * -- because even though partial RAID stripe-sized IOs will be issued to a
  3364. * single RAID stripe; when aggregated they will end on a full RAID stripe
  3365. * boundary.. which avoids additional partial RAID stripe writes cascading
  3366. */
  3367. if (limits->max_sectors < pool->sectors_per_block) {
  3368. while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
  3369. if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
  3370. limits->max_sectors--;
  3371. limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
  3372. }
  3373. }
  3374. /*
  3375. * If the system-determined stacked limits are compatible with the
  3376. * pool's blocksize (io_opt is a factor) do not override them.
  3377. */
  3378. if (io_opt_sectors < pool->sectors_per_block ||
  3379. !is_factor(io_opt_sectors, pool->sectors_per_block)) {
  3380. if (is_factor(pool->sectors_per_block, limits->max_sectors))
  3381. limits->io_min = limits->max_sectors << SECTOR_SHIFT;
  3382. else
  3383. limits->io_min = pool->sectors_per_block << SECTOR_SHIFT;
  3384. limits->io_opt = pool->sectors_per_block << SECTOR_SHIFT;
  3385. }
  3386. /*
  3387. * pt->adjusted_pf is a staging area for the actual features to use.
  3388. * They get transferred to the live pool in bind_control_target()
  3389. * called from pool_preresume().
  3390. */
  3391. if (pt->adjusted_pf.discard_enabled) {
  3392. disable_discard_passdown_if_not_supported(pt);
  3393. if (!pt->adjusted_pf.discard_passdown)
  3394. limits->max_hw_discard_sectors = 0;
  3395. /*
  3396. * The pool uses the same discard limits as the underlying data
  3397. * device. DM core has already set this up.
  3398. */
  3399. } else {
  3400. /*
  3401. * Must explicitly disallow stacking discard limits otherwise the
  3402. * block layer will stack them if pool's data device has support.
  3403. */
  3404. limits->discard_granularity = 0;
  3405. }
  3406. }
  3407. static struct target_type pool_target = {
  3408. .name = "thin-pool",
  3409. .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
  3410. DM_TARGET_IMMUTABLE,
  3411. .version = {1, 23, 0},
  3412. .module = THIS_MODULE,
  3413. .ctr = pool_ctr,
  3414. .dtr = pool_dtr,
  3415. .map = pool_map,
  3416. .presuspend = pool_presuspend,
  3417. .presuspend_undo = pool_presuspend_undo,
  3418. .postsuspend = pool_postsuspend,
  3419. .preresume = pool_preresume,
  3420. .resume = pool_resume,
  3421. .message = pool_message,
  3422. .status = pool_status,
  3423. .iterate_devices = pool_iterate_devices,
  3424. .io_hints = pool_io_hints,
  3425. };
  3426. /*
  3427. *--------------------------------------------------------------
  3428. * Thin target methods
  3429. *--------------------------------------------------------------
  3430. */
  3431. static void thin_get(struct thin_c *tc)
  3432. {
  3433. refcount_inc(&tc->refcount);
  3434. }
  3435. static void thin_put(struct thin_c *tc)
  3436. {
  3437. if (refcount_dec_and_test(&tc->refcount))
  3438. complete(&tc->can_destroy);
  3439. }
  3440. static void thin_dtr(struct dm_target *ti)
  3441. {
  3442. struct thin_c *tc = ti->private;
  3443. spin_lock_irq(&tc->pool->lock);
  3444. list_del_rcu(&tc->list);
  3445. spin_unlock_irq(&tc->pool->lock);
  3446. synchronize_rcu();
  3447. thin_put(tc);
  3448. wait_for_completion(&tc->can_destroy);
  3449. mutex_lock(&dm_thin_pool_table.mutex);
  3450. __pool_dec(tc->pool);
  3451. dm_pool_close_thin_device(tc->td);
  3452. dm_put_device(ti, tc->pool_dev);
  3453. if (tc->origin_dev)
  3454. dm_put_device(ti, tc->origin_dev);
  3455. kfree(tc);
  3456. mutex_unlock(&dm_thin_pool_table.mutex);
  3457. }
  3458. /*
  3459. * Thin target parameters:
  3460. *
  3461. * <pool_dev> <dev_id> [origin_dev]
  3462. *
  3463. * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
  3464. * dev_id: the internal device identifier
  3465. * origin_dev: a device external to the pool that should act as the origin
  3466. *
  3467. * If the pool device has discards disabled, they get disabled for the thin
  3468. * device as well.
  3469. */
  3470. static int thin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  3471. {
  3472. int r;
  3473. struct thin_c *tc;
  3474. struct dm_dev *pool_dev, *origin_dev;
  3475. struct mapped_device *pool_md;
  3476. mutex_lock(&dm_thin_pool_table.mutex);
  3477. if (argc != 2 && argc != 3) {
  3478. ti->error = "Invalid argument count";
  3479. r = -EINVAL;
  3480. goto out_unlock;
  3481. }
  3482. tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
  3483. if (!tc) {
  3484. ti->error = "Out of memory";
  3485. r = -ENOMEM;
  3486. goto out_unlock;
  3487. }
  3488. tc->thin_md = dm_table_get_md(ti->table);
  3489. spin_lock_init(&tc->lock);
  3490. INIT_LIST_HEAD(&tc->deferred_cells);
  3491. bio_list_init(&tc->deferred_bio_list);
  3492. bio_list_init(&tc->retry_on_resume_list);
  3493. tc->sort_bio_list = RB_ROOT;
  3494. if (argc == 3) {
  3495. if (!strcmp(argv[0], argv[2])) {
  3496. ti->error = "Error setting origin device";
  3497. r = -EINVAL;
  3498. goto bad_origin_dev;
  3499. }
  3500. r = dm_get_device(ti, argv[2], BLK_OPEN_READ, &origin_dev);
  3501. if (r) {
  3502. ti->error = "Error opening origin device";
  3503. goto bad_origin_dev;
  3504. }
  3505. tc->origin_dev = origin_dev;
  3506. }
  3507. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
  3508. if (r) {
  3509. ti->error = "Error opening pool device";
  3510. goto bad_pool_dev;
  3511. }
  3512. tc->pool_dev = pool_dev;
  3513. if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
  3514. ti->error = "Invalid device id";
  3515. r = -EINVAL;
  3516. goto bad_common;
  3517. }
  3518. pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
  3519. if (!pool_md) {
  3520. ti->error = "Couldn't get pool mapped device";
  3521. r = -EINVAL;
  3522. goto bad_common;
  3523. }
  3524. tc->pool = __pool_table_lookup(pool_md);
  3525. if (!tc->pool) {
  3526. ti->error = "Couldn't find pool object";
  3527. r = -EINVAL;
  3528. goto bad_pool_lookup;
  3529. }
  3530. __pool_inc(tc->pool);
  3531. if (get_pool_mode(tc->pool) == PM_FAIL) {
  3532. ti->error = "Couldn't open thin device, Pool is in fail mode";
  3533. r = -EINVAL;
  3534. goto bad_pool;
  3535. }
  3536. r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
  3537. if (r) {
  3538. ti->error = "Couldn't open thin internal device";
  3539. goto bad_pool;
  3540. }
  3541. r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
  3542. if (r)
  3543. goto bad;
  3544. ti->num_flush_bios = 1;
  3545. ti->limit_swap_bios = true;
  3546. ti->flush_supported = true;
  3547. ti->accounts_remapped_io = true;
  3548. ti->per_io_data_size = sizeof(struct dm_thin_endio_hook);
  3549. /* In case the pool supports discards, pass them on. */
  3550. if (tc->pool->pf.discard_enabled) {
  3551. ti->discards_supported = true;
  3552. ti->num_discard_bios = 1;
  3553. ti->max_discard_granularity = true;
  3554. }
  3555. mutex_unlock(&dm_thin_pool_table.mutex);
  3556. spin_lock_irq(&tc->pool->lock);
  3557. if (tc->pool->suspended) {
  3558. spin_unlock_irq(&tc->pool->lock);
  3559. mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
  3560. ti->error = "Unable to activate thin device while pool is suspended";
  3561. r = -EINVAL;
  3562. goto bad;
  3563. }
  3564. refcount_set(&tc->refcount, 1);
  3565. init_completion(&tc->can_destroy);
  3566. list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
  3567. spin_unlock_irq(&tc->pool->lock);
  3568. /*
  3569. * This synchronize_rcu() call is needed here otherwise we risk a
  3570. * wake_worker() call finding no bios to process (because the newly
  3571. * added tc isn't yet visible). So this reduces latency since we
  3572. * aren't then dependent on the periodic commit to wake_worker().
  3573. */
  3574. synchronize_rcu();
  3575. dm_put(pool_md);
  3576. return 0;
  3577. bad:
  3578. dm_pool_close_thin_device(tc->td);
  3579. bad_pool:
  3580. __pool_dec(tc->pool);
  3581. bad_pool_lookup:
  3582. dm_put(pool_md);
  3583. bad_common:
  3584. dm_put_device(ti, tc->pool_dev);
  3585. bad_pool_dev:
  3586. if (tc->origin_dev)
  3587. dm_put_device(ti, tc->origin_dev);
  3588. bad_origin_dev:
  3589. kfree(tc);
  3590. out_unlock:
  3591. mutex_unlock(&dm_thin_pool_table.mutex);
  3592. return r;
  3593. }
  3594. static int thin_map(struct dm_target *ti, struct bio *bio)
  3595. {
  3596. bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
  3597. return thin_bio_map(ti, bio);
  3598. }
  3599. static int thin_endio(struct dm_target *ti, struct bio *bio,
  3600. blk_status_t *err)
  3601. {
  3602. unsigned long flags;
  3603. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  3604. struct list_head work;
  3605. struct dm_thin_new_mapping *m, *tmp;
  3606. struct pool *pool = h->tc->pool;
  3607. if (h->shared_read_entry) {
  3608. INIT_LIST_HEAD(&work);
  3609. dm_deferred_entry_dec(h->shared_read_entry, &work);
  3610. spin_lock_irqsave(&pool->lock, flags);
  3611. list_for_each_entry_safe(m, tmp, &work, list) {
  3612. list_del(&m->list);
  3613. __complete_mapping_preparation(m);
  3614. }
  3615. spin_unlock_irqrestore(&pool->lock, flags);
  3616. }
  3617. if (h->all_io_entry) {
  3618. INIT_LIST_HEAD(&work);
  3619. dm_deferred_entry_dec(h->all_io_entry, &work);
  3620. if (!list_empty(&work)) {
  3621. spin_lock_irqsave(&pool->lock, flags);
  3622. list_for_each_entry_safe(m, tmp, &work, list)
  3623. list_add_tail(&m->list, &pool->prepared_discards);
  3624. spin_unlock_irqrestore(&pool->lock, flags);
  3625. wake_worker(pool);
  3626. }
  3627. }
  3628. if (h->cell)
  3629. cell_defer_no_holder(h->tc, h->cell);
  3630. return DM_ENDIO_DONE;
  3631. }
  3632. static void thin_presuspend(struct dm_target *ti)
  3633. {
  3634. struct thin_c *tc = ti->private;
  3635. if (dm_noflush_suspending(ti))
  3636. noflush_work(tc, do_noflush_start);
  3637. }
  3638. static void thin_postsuspend(struct dm_target *ti)
  3639. {
  3640. struct thin_c *tc = ti->private;
  3641. /*
  3642. * The dm_noflush_suspending flag has been cleared by now, so
  3643. * unfortunately we must always run this.
  3644. */
  3645. noflush_work(tc, do_noflush_stop);
  3646. }
  3647. static int thin_preresume(struct dm_target *ti)
  3648. {
  3649. struct thin_c *tc = ti->private;
  3650. if (tc->origin_dev)
  3651. tc->origin_size = get_dev_size(tc->origin_dev->bdev);
  3652. return 0;
  3653. }
  3654. /*
  3655. * <nr mapped sectors> <highest mapped sector>
  3656. */
  3657. static void thin_status(struct dm_target *ti, status_type_t type,
  3658. unsigned int status_flags, char *result, unsigned int maxlen)
  3659. {
  3660. int r;
  3661. ssize_t sz = 0;
  3662. dm_block_t mapped, highest;
  3663. char buf[BDEVNAME_SIZE];
  3664. struct thin_c *tc = ti->private;
  3665. if (get_pool_mode(tc->pool) == PM_FAIL) {
  3666. DMEMIT("Fail");
  3667. return;
  3668. }
  3669. if (!tc->td)
  3670. DMEMIT("-");
  3671. else {
  3672. switch (type) {
  3673. case STATUSTYPE_INFO:
  3674. r = dm_thin_get_mapped_count(tc->td, &mapped);
  3675. if (r) {
  3676. DMERR("dm_thin_get_mapped_count returned %d", r);
  3677. goto err;
  3678. }
  3679. r = dm_thin_get_highest_mapped_block(tc->td, &highest);
  3680. if (r < 0) {
  3681. DMERR("dm_thin_get_highest_mapped_block returned %d", r);
  3682. goto err;
  3683. }
  3684. DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
  3685. if (r)
  3686. DMEMIT("%llu", ((highest + 1) *
  3687. tc->pool->sectors_per_block) - 1);
  3688. else
  3689. DMEMIT("-");
  3690. break;
  3691. case STATUSTYPE_TABLE:
  3692. DMEMIT("%s %lu",
  3693. format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
  3694. (unsigned long) tc->dev_id);
  3695. if (tc->origin_dev)
  3696. DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
  3697. break;
  3698. case STATUSTYPE_IMA:
  3699. *result = '\0';
  3700. break;
  3701. }
  3702. }
  3703. return;
  3704. err:
  3705. DMEMIT("Error");
  3706. }
  3707. static int thin_iterate_devices(struct dm_target *ti,
  3708. iterate_devices_callout_fn fn, void *data)
  3709. {
  3710. sector_t blocks;
  3711. struct thin_c *tc = ti->private;
  3712. struct pool *pool = tc->pool;
  3713. /*
  3714. * We can't call dm_pool_get_data_dev_size() since that blocks. So
  3715. * we follow a more convoluted path through to the pool's target.
  3716. */
  3717. if (!pool->ti)
  3718. return 0; /* nothing is bound */
  3719. blocks = pool->ti->len;
  3720. (void) sector_div(blocks, pool->sectors_per_block);
  3721. if (blocks)
  3722. return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
  3723. return 0;
  3724. }
  3725. static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
  3726. {
  3727. struct thin_c *tc = ti->private;
  3728. struct pool *pool = tc->pool;
  3729. if (pool->pf.discard_enabled) {
  3730. limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
  3731. limits->max_hw_discard_sectors = pool->sectors_per_block * BIO_PRISON_MAX_RANGE;
  3732. }
  3733. }
  3734. static struct target_type thin_target = {
  3735. .name = "thin",
  3736. .version = {1, 23, 0},
  3737. .module = THIS_MODULE,
  3738. .ctr = thin_ctr,
  3739. .dtr = thin_dtr,
  3740. .map = thin_map,
  3741. .end_io = thin_endio,
  3742. .preresume = thin_preresume,
  3743. .presuspend = thin_presuspend,
  3744. .postsuspend = thin_postsuspend,
  3745. .status = thin_status,
  3746. .iterate_devices = thin_iterate_devices,
  3747. .io_hints = thin_io_hints,
  3748. };
  3749. /*----------------------------------------------------------------*/
  3750. static int __init dm_thin_init(void)
  3751. {
  3752. int r = -ENOMEM;
  3753. pool_table_init();
  3754. _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
  3755. if (!_new_mapping_cache)
  3756. return r;
  3757. r = dm_register_target(&thin_target);
  3758. if (r)
  3759. goto bad_new_mapping_cache;
  3760. r = dm_register_target(&pool_target);
  3761. if (r)
  3762. goto bad_thin_target;
  3763. return 0;
  3764. bad_thin_target:
  3765. dm_unregister_target(&thin_target);
  3766. bad_new_mapping_cache:
  3767. kmem_cache_destroy(_new_mapping_cache);
  3768. return r;
  3769. }
  3770. static void dm_thin_exit(void)
  3771. {
  3772. dm_unregister_target(&thin_target);
  3773. dm_unregister_target(&pool_target);
  3774. kmem_cache_destroy(_new_mapping_cache);
  3775. pool_table_exit();
  3776. }
  3777. module_init(dm_thin_init);
  3778. module_exit(dm_thin_exit);
  3779. module_param_named(no_space_timeout, no_space_timeout_secs, uint, 0644);
  3780. MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
  3781. MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
  3782. MODULE_AUTHOR("Joe Thornber <dm-devel@lists.linux.dev>");
  3783. MODULE_LICENSE("GPL");