dm-thin.c 109 KB

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