dm-thin.c 111 KB

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