dm-thin.c 90 KB

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