dm-thin.c 112 KB

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