dm-thin.c 112 KB

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