dm-thin.c 109 KB

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