dm-thin.c 107 KB

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