dm-thin.c 107 KB

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