dm-thin.c 101 KB

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