dm-thin.c 108 KB

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