dm-thin.c 102 KB

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