dm-thin.c 112 KB

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