dm-thin.c 110 KB

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