dm-thin.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621
  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. /*
  820. * A non-zero return indicates read_only or fail_io mode.
  821. * Many callers don't care about the return value.
  822. */
  823. static int commit(struct pool *pool)
  824. {
  825. int r;
  826. if (get_pool_mode(pool) >= PM_READ_ONLY)
  827. return -EINVAL;
  828. r = dm_pool_commit_metadata(pool->pmd);
  829. if (r)
  830. metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
  831. return r;
  832. }
  833. static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
  834. {
  835. unsigned long flags;
  836. if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
  837. DMWARN("%s: reached low water mark for data device: sending event.",
  838. dm_device_name(pool->pool_md));
  839. spin_lock_irqsave(&pool->lock, flags);
  840. pool->low_water_triggered = true;
  841. spin_unlock_irqrestore(&pool->lock, flags);
  842. dm_table_event(pool->ti->table);
  843. }
  844. }
  845. static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
  846. static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
  847. {
  848. int r;
  849. dm_block_t free_blocks;
  850. struct pool *pool = tc->pool;
  851. if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
  852. return -EINVAL;
  853. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  854. if (r) {
  855. metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
  856. return r;
  857. }
  858. check_low_water_mark(pool, free_blocks);
  859. if (!free_blocks) {
  860. /*
  861. * Try to commit to see if that will free up some
  862. * more space.
  863. */
  864. r = commit(pool);
  865. if (r)
  866. return r;
  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. if (!free_blocks) {
  873. set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
  874. return -ENOSPC;
  875. }
  876. }
  877. r = dm_pool_alloc_data_block(pool->pmd, result);
  878. if (r) {
  879. metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
  880. return r;
  881. }
  882. return 0;
  883. }
  884. /*
  885. * If we have run out of space, queue bios until the device is
  886. * resumed, presumably after having been reloaded with more space.
  887. */
  888. static void retry_on_resume(struct bio *bio)
  889. {
  890. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  891. struct thin_c *tc = h->tc;
  892. unsigned long flags;
  893. spin_lock_irqsave(&tc->lock, flags);
  894. bio_list_add(&tc->retry_on_resume_list, bio);
  895. spin_unlock_irqrestore(&tc->lock, flags);
  896. }
  897. static int should_error_unserviceable_bio(struct pool *pool)
  898. {
  899. enum pool_mode m = get_pool_mode(pool);
  900. switch (m) {
  901. case PM_WRITE:
  902. /* Shouldn't get here */
  903. DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
  904. return -EIO;
  905. case PM_OUT_OF_DATA_SPACE:
  906. return pool->pf.error_if_no_space ? -ENOSPC : 0;
  907. case PM_READ_ONLY:
  908. case PM_FAIL:
  909. return -EIO;
  910. default:
  911. /* Shouldn't get here */
  912. DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
  913. return -EIO;
  914. }
  915. }
  916. static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
  917. {
  918. int error = should_error_unserviceable_bio(pool);
  919. if (error)
  920. bio_endio(bio, error);
  921. else
  922. retry_on_resume(bio);
  923. }
  924. static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
  925. {
  926. struct bio *bio;
  927. struct bio_list bios;
  928. int error;
  929. error = should_error_unserviceable_bio(pool);
  930. if (error) {
  931. cell_error_with_code(pool, cell, error);
  932. return;
  933. }
  934. bio_list_init(&bios);
  935. cell_release(pool, cell, &bios);
  936. error = should_error_unserviceable_bio(pool);
  937. if (error)
  938. while ((bio = bio_list_pop(&bios)))
  939. bio_endio(bio, error);
  940. else
  941. while ((bio = bio_list_pop(&bios)))
  942. retry_on_resume(bio);
  943. }
  944. static void process_discard(struct thin_c *tc, struct bio *bio)
  945. {
  946. int r;
  947. unsigned long flags;
  948. struct pool *pool = tc->pool;
  949. struct dm_bio_prison_cell *cell, *cell2;
  950. struct dm_cell_key key, key2;
  951. dm_block_t block = get_bio_block(tc, bio);
  952. struct dm_thin_lookup_result lookup_result;
  953. struct dm_thin_new_mapping *m;
  954. build_virtual_key(tc->td, block, &key);
  955. if (bio_detain(tc->pool, &key, bio, &cell))
  956. return;
  957. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  958. switch (r) {
  959. case 0:
  960. /*
  961. * Check nobody is fiddling with this pool block. This can
  962. * happen if someone's in the process of breaking sharing
  963. * on this block.
  964. */
  965. build_data_key(tc->td, lookup_result.block, &key2);
  966. if (bio_detain(tc->pool, &key2, bio, &cell2)) {
  967. cell_defer_no_holder(tc, cell);
  968. break;
  969. }
  970. if (io_overlaps_block(pool, bio)) {
  971. /*
  972. * IO may still be going to the destination block. We must
  973. * quiesce before we can do the removal.
  974. */
  975. m = get_next_mapping(pool);
  976. m->tc = tc;
  977. m->pass_discard = pool->pf.discard_passdown;
  978. m->definitely_not_shared = !lookup_result.shared;
  979. m->virt_block = block;
  980. m->data_block = lookup_result.block;
  981. m->cell = cell;
  982. m->cell2 = cell2;
  983. m->bio = bio;
  984. if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list)) {
  985. spin_lock_irqsave(&pool->lock, flags);
  986. list_add_tail(&m->list, &pool->prepared_discards);
  987. spin_unlock_irqrestore(&pool->lock, flags);
  988. wake_worker(pool);
  989. }
  990. } else {
  991. inc_all_io_entry(pool, bio);
  992. cell_defer_no_holder(tc, cell);
  993. cell_defer_no_holder(tc, cell2);
  994. /*
  995. * The DM core makes sure that the discard doesn't span
  996. * a block boundary. So we submit the discard of a
  997. * partial block appropriately.
  998. */
  999. if ((!lookup_result.shared) && pool->pf.discard_passdown)
  1000. remap_and_issue(tc, bio, lookup_result.block);
  1001. else
  1002. bio_endio(bio, 0);
  1003. }
  1004. break;
  1005. case -ENODATA:
  1006. /*
  1007. * It isn't provisioned, just forget it.
  1008. */
  1009. cell_defer_no_holder(tc, cell);
  1010. bio_endio(bio, 0);
  1011. break;
  1012. default:
  1013. DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
  1014. __func__, r);
  1015. cell_defer_no_holder(tc, cell);
  1016. bio_io_error(bio);
  1017. break;
  1018. }
  1019. }
  1020. static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
  1021. struct dm_cell_key *key,
  1022. struct dm_thin_lookup_result *lookup_result,
  1023. struct dm_bio_prison_cell *cell)
  1024. {
  1025. int r;
  1026. dm_block_t data_block;
  1027. struct pool *pool = tc->pool;
  1028. r = alloc_data_block(tc, &data_block);
  1029. switch (r) {
  1030. case 0:
  1031. schedule_internal_copy(tc, block, lookup_result->block,
  1032. data_block, cell, bio);
  1033. break;
  1034. case -ENOSPC:
  1035. retry_bios_on_resume(pool, cell);
  1036. break;
  1037. default:
  1038. DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
  1039. __func__, r);
  1040. cell_error(pool, cell);
  1041. break;
  1042. }
  1043. }
  1044. static void process_shared_bio(struct thin_c *tc, struct bio *bio,
  1045. dm_block_t block,
  1046. struct dm_thin_lookup_result *lookup_result)
  1047. {
  1048. struct dm_bio_prison_cell *cell;
  1049. struct pool *pool = tc->pool;
  1050. struct dm_cell_key key;
  1051. /*
  1052. * If cell is already occupied, then sharing is already in the process
  1053. * of being broken so we have nothing further to do here.
  1054. */
  1055. build_data_key(tc->td, lookup_result->block, &key);
  1056. if (bio_detain(pool, &key, bio, &cell))
  1057. return;
  1058. if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size)
  1059. break_sharing(tc, bio, block, &key, lookup_result, cell);
  1060. else {
  1061. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1062. h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
  1063. inc_all_io_entry(pool, bio);
  1064. cell_defer_no_holder(tc, cell);
  1065. remap_and_issue(tc, bio, lookup_result->block);
  1066. }
  1067. }
  1068. static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
  1069. struct dm_bio_prison_cell *cell)
  1070. {
  1071. int r;
  1072. dm_block_t data_block;
  1073. struct pool *pool = tc->pool;
  1074. /*
  1075. * Remap empty bios (flushes) immediately, without provisioning.
  1076. */
  1077. if (!bio->bi_iter.bi_size) {
  1078. inc_all_io_entry(pool, bio);
  1079. cell_defer_no_holder(tc, cell);
  1080. remap_and_issue(tc, bio, 0);
  1081. return;
  1082. }
  1083. /*
  1084. * Fill read bios with zeroes and complete them immediately.
  1085. */
  1086. if (bio_data_dir(bio) == READ) {
  1087. zero_fill_bio(bio);
  1088. cell_defer_no_holder(tc, cell);
  1089. bio_endio(bio, 0);
  1090. return;
  1091. }
  1092. r = alloc_data_block(tc, &data_block);
  1093. switch (r) {
  1094. case 0:
  1095. if (tc->origin_dev)
  1096. schedule_external_copy(tc, block, data_block, cell, bio);
  1097. else
  1098. schedule_zero(tc, block, data_block, cell, bio);
  1099. break;
  1100. case -ENOSPC:
  1101. retry_bios_on_resume(pool, cell);
  1102. break;
  1103. default:
  1104. DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
  1105. __func__, r);
  1106. cell_error(pool, cell);
  1107. break;
  1108. }
  1109. }
  1110. static void process_bio(struct thin_c *tc, struct bio *bio)
  1111. {
  1112. int r;
  1113. struct pool *pool = tc->pool;
  1114. dm_block_t block = get_bio_block(tc, bio);
  1115. struct dm_bio_prison_cell *cell;
  1116. struct dm_cell_key key;
  1117. struct dm_thin_lookup_result lookup_result;
  1118. /*
  1119. * If cell is already occupied, then the block is already
  1120. * being provisioned so we have nothing further to do here.
  1121. */
  1122. build_virtual_key(tc->td, block, &key);
  1123. if (bio_detain(pool, &key, bio, &cell))
  1124. return;
  1125. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1126. switch (r) {
  1127. case 0:
  1128. if (lookup_result.shared) {
  1129. process_shared_bio(tc, bio, block, &lookup_result);
  1130. cell_defer_no_holder(tc, cell); /* FIXME: pass this cell into process_shared? */
  1131. } else {
  1132. inc_all_io_entry(pool, bio);
  1133. cell_defer_no_holder(tc, cell);
  1134. remap_and_issue(tc, bio, lookup_result.block);
  1135. }
  1136. break;
  1137. case -ENODATA:
  1138. if (bio_data_dir(bio) == READ && tc->origin_dev) {
  1139. inc_all_io_entry(pool, bio);
  1140. cell_defer_no_holder(tc, cell);
  1141. if (bio_end_sector(bio) <= tc->origin_size)
  1142. remap_to_origin_and_issue(tc, bio);
  1143. else if (bio->bi_iter.bi_sector < tc->origin_size) {
  1144. zero_fill_bio(bio);
  1145. bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
  1146. remap_to_origin_and_issue(tc, bio);
  1147. } else {
  1148. zero_fill_bio(bio);
  1149. bio_endio(bio, 0);
  1150. }
  1151. } else
  1152. provision_block(tc, bio, block, cell);
  1153. break;
  1154. default:
  1155. DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
  1156. __func__, r);
  1157. cell_defer_no_holder(tc, cell);
  1158. bio_io_error(bio);
  1159. break;
  1160. }
  1161. }
  1162. static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
  1163. {
  1164. int r;
  1165. int rw = bio_data_dir(bio);
  1166. dm_block_t block = get_bio_block(tc, bio);
  1167. struct dm_thin_lookup_result lookup_result;
  1168. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1169. switch (r) {
  1170. case 0:
  1171. if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size)
  1172. handle_unserviceable_bio(tc->pool, bio);
  1173. else {
  1174. inc_all_io_entry(tc->pool, bio);
  1175. remap_and_issue(tc, bio, lookup_result.block);
  1176. }
  1177. break;
  1178. case -ENODATA:
  1179. if (rw != READ) {
  1180. handle_unserviceable_bio(tc->pool, bio);
  1181. break;
  1182. }
  1183. if (tc->origin_dev) {
  1184. inc_all_io_entry(tc->pool, bio);
  1185. remap_to_origin_and_issue(tc, bio);
  1186. break;
  1187. }
  1188. zero_fill_bio(bio);
  1189. bio_endio(bio, 0);
  1190. break;
  1191. default:
  1192. DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
  1193. __func__, r);
  1194. bio_io_error(bio);
  1195. break;
  1196. }
  1197. }
  1198. static void process_bio_success(struct thin_c *tc, struct bio *bio)
  1199. {
  1200. bio_endio(bio, 0);
  1201. }
  1202. static void process_bio_fail(struct thin_c *tc, struct bio *bio)
  1203. {
  1204. bio_io_error(bio);
  1205. }
  1206. /*
  1207. * FIXME: should we also commit due to size of transaction, measured in
  1208. * metadata blocks?
  1209. */
  1210. static int need_commit_due_to_time(struct pool *pool)
  1211. {
  1212. return jiffies < pool->last_commit_jiffies ||
  1213. jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
  1214. }
  1215. #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
  1216. #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
  1217. static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
  1218. {
  1219. struct rb_node **rbp, *parent;
  1220. struct dm_thin_endio_hook *pbd;
  1221. sector_t bi_sector = bio->bi_iter.bi_sector;
  1222. rbp = &tc->sort_bio_list.rb_node;
  1223. parent = NULL;
  1224. while (*rbp) {
  1225. parent = *rbp;
  1226. pbd = thin_pbd(parent);
  1227. if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
  1228. rbp = &(*rbp)->rb_left;
  1229. else
  1230. rbp = &(*rbp)->rb_right;
  1231. }
  1232. pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1233. rb_link_node(&pbd->rb_node, parent, rbp);
  1234. rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
  1235. }
  1236. static void __extract_sorted_bios(struct thin_c *tc)
  1237. {
  1238. struct rb_node *node;
  1239. struct dm_thin_endio_hook *pbd;
  1240. struct bio *bio;
  1241. for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
  1242. pbd = thin_pbd(node);
  1243. bio = thin_bio(pbd);
  1244. bio_list_add(&tc->deferred_bio_list, bio);
  1245. rb_erase(&pbd->rb_node, &tc->sort_bio_list);
  1246. }
  1247. WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
  1248. }
  1249. static void __sort_thin_deferred_bios(struct thin_c *tc)
  1250. {
  1251. struct bio *bio;
  1252. struct bio_list bios;
  1253. bio_list_init(&bios);
  1254. bio_list_merge(&bios, &tc->deferred_bio_list);
  1255. bio_list_init(&tc->deferred_bio_list);
  1256. /* Sort deferred_bio_list using rb-tree */
  1257. while ((bio = bio_list_pop(&bios)))
  1258. __thin_bio_rb_add(tc, bio);
  1259. /*
  1260. * Transfer the sorted bios in sort_bio_list back to
  1261. * deferred_bio_list to allow lockless submission of
  1262. * all bios.
  1263. */
  1264. __extract_sorted_bios(tc);
  1265. }
  1266. static void process_thin_deferred_bios(struct thin_c *tc)
  1267. {
  1268. struct pool *pool = tc->pool;
  1269. unsigned long flags;
  1270. struct bio *bio;
  1271. struct bio_list bios;
  1272. struct blk_plug plug;
  1273. if (tc->requeue_mode) {
  1274. requeue_bio_list(tc, &tc->deferred_bio_list);
  1275. return;
  1276. }
  1277. bio_list_init(&bios);
  1278. spin_lock_irqsave(&tc->lock, flags);
  1279. if (bio_list_empty(&tc->deferred_bio_list)) {
  1280. spin_unlock_irqrestore(&tc->lock, flags);
  1281. return;
  1282. }
  1283. __sort_thin_deferred_bios(tc);
  1284. bio_list_merge(&bios, &tc->deferred_bio_list);
  1285. bio_list_init(&tc->deferred_bio_list);
  1286. spin_unlock_irqrestore(&tc->lock, flags);
  1287. blk_start_plug(&plug);
  1288. while ((bio = bio_list_pop(&bios))) {
  1289. /*
  1290. * If we've got no free new_mapping structs, and processing
  1291. * this bio might require one, we pause until there are some
  1292. * prepared mappings to process.
  1293. */
  1294. if (ensure_next_mapping(pool)) {
  1295. spin_lock_irqsave(&tc->lock, flags);
  1296. bio_list_add(&tc->deferred_bio_list, bio);
  1297. bio_list_merge(&tc->deferred_bio_list, &bios);
  1298. spin_unlock_irqrestore(&tc->lock, flags);
  1299. break;
  1300. }
  1301. if (bio->bi_rw & REQ_DISCARD)
  1302. pool->process_discard(tc, bio);
  1303. else
  1304. pool->process_bio(tc, bio);
  1305. }
  1306. blk_finish_plug(&plug);
  1307. }
  1308. static void thin_get(struct thin_c *tc);
  1309. static void thin_put(struct thin_c *tc);
  1310. /*
  1311. * We can't hold rcu_read_lock() around code that can block. So we
  1312. * find a thin with the rcu lock held; bump a refcount; then drop
  1313. * the lock.
  1314. */
  1315. static struct thin_c *get_first_thin(struct pool *pool)
  1316. {
  1317. struct thin_c *tc = NULL;
  1318. rcu_read_lock();
  1319. if (!list_empty(&pool->active_thins)) {
  1320. tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
  1321. thin_get(tc);
  1322. }
  1323. rcu_read_unlock();
  1324. return tc;
  1325. }
  1326. static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
  1327. {
  1328. struct thin_c *old_tc = tc;
  1329. rcu_read_lock();
  1330. list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
  1331. thin_get(tc);
  1332. thin_put(old_tc);
  1333. rcu_read_unlock();
  1334. return tc;
  1335. }
  1336. thin_put(old_tc);
  1337. rcu_read_unlock();
  1338. return NULL;
  1339. }
  1340. static void process_deferred_bios(struct pool *pool)
  1341. {
  1342. unsigned long flags;
  1343. struct bio *bio;
  1344. struct bio_list bios;
  1345. struct thin_c *tc;
  1346. tc = get_first_thin(pool);
  1347. while (tc) {
  1348. process_thin_deferred_bios(tc);
  1349. tc = get_next_thin(pool, tc);
  1350. }
  1351. /*
  1352. * If there are any deferred flush bios, we must commit
  1353. * the metadata before issuing them.
  1354. */
  1355. bio_list_init(&bios);
  1356. spin_lock_irqsave(&pool->lock, flags);
  1357. bio_list_merge(&bios, &pool->deferred_flush_bios);
  1358. bio_list_init(&pool->deferred_flush_bios);
  1359. spin_unlock_irqrestore(&pool->lock, flags);
  1360. if (bio_list_empty(&bios) &&
  1361. !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
  1362. return;
  1363. if (commit(pool)) {
  1364. while ((bio = bio_list_pop(&bios)))
  1365. bio_io_error(bio);
  1366. return;
  1367. }
  1368. pool->last_commit_jiffies = jiffies;
  1369. while ((bio = bio_list_pop(&bios)))
  1370. generic_make_request(bio);
  1371. }
  1372. static void do_worker(struct work_struct *ws)
  1373. {
  1374. struct pool *pool = container_of(ws, struct pool, worker);
  1375. process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
  1376. process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
  1377. process_deferred_bios(pool);
  1378. }
  1379. /*
  1380. * We want to commit periodically so that not too much
  1381. * unwritten data builds up.
  1382. */
  1383. static void do_waker(struct work_struct *ws)
  1384. {
  1385. struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
  1386. wake_worker(pool);
  1387. queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
  1388. }
  1389. /*
  1390. * We're holding onto IO to allow userland time to react. After the
  1391. * timeout either the pool will have been resized (and thus back in
  1392. * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
  1393. */
  1394. static void do_no_space_timeout(struct work_struct *ws)
  1395. {
  1396. struct pool *pool = container_of(to_delayed_work(ws), struct pool,
  1397. no_space_timeout);
  1398. if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
  1399. set_pool_mode(pool, PM_READ_ONLY);
  1400. }
  1401. /*----------------------------------------------------------------*/
  1402. struct pool_work {
  1403. struct work_struct worker;
  1404. struct completion complete;
  1405. };
  1406. static struct pool_work *to_pool_work(struct work_struct *ws)
  1407. {
  1408. return container_of(ws, struct pool_work, worker);
  1409. }
  1410. static void pool_work_complete(struct pool_work *pw)
  1411. {
  1412. complete(&pw->complete);
  1413. }
  1414. static void pool_work_wait(struct pool_work *pw, struct pool *pool,
  1415. void (*fn)(struct work_struct *))
  1416. {
  1417. INIT_WORK_ONSTACK(&pw->worker, fn);
  1418. init_completion(&pw->complete);
  1419. queue_work(pool->wq, &pw->worker);
  1420. wait_for_completion(&pw->complete);
  1421. }
  1422. /*----------------------------------------------------------------*/
  1423. struct noflush_work {
  1424. struct pool_work pw;
  1425. struct thin_c *tc;
  1426. };
  1427. static struct noflush_work *to_noflush(struct work_struct *ws)
  1428. {
  1429. return container_of(to_pool_work(ws), struct noflush_work, pw);
  1430. }
  1431. static void do_noflush_start(struct work_struct *ws)
  1432. {
  1433. struct noflush_work *w = to_noflush(ws);
  1434. w->tc->requeue_mode = true;
  1435. requeue_io(w->tc);
  1436. pool_work_complete(&w->pw);
  1437. }
  1438. static void do_noflush_stop(struct work_struct *ws)
  1439. {
  1440. struct noflush_work *w = to_noflush(ws);
  1441. w->tc->requeue_mode = false;
  1442. pool_work_complete(&w->pw);
  1443. }
  1444. static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
  1445. {
  1446. struct noflush_work w;
  1447. w.tc = tc;
  1448. pool_work_wait(&w.pw, tc->pool, fn);
  1449. }
  1450. /*----------------------------------------------------------------*/
  1451. static enum pool_mode get_pool_mode(struct pool *pool)
  1452. {
  1453. return pool->pf.mode;
  1454. }
  1455. static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
  1456. {
  1457. dm_table_event(pool->ti->table);
  1458. DMINFO("%s: switching pool to %s mode",
  1459. dm_device_name(pool->pool_md), new_mode);
  1460. }
  1461. static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
  1462. {
  1463. struct pool_c *pt = pool->ti->private;
  1464. bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
  1465. enum pool_mode old_mode = get_pool_mode(pool);
  1466. unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
  1467. /*
  1468. * Never allow the pool to transition to PM_WRITE mode if user
  1469. * intervention is required to verify metadata and data consistency.
  1470. */
  1471. if (new_mode == PM_WRITE && needs_check) {
  1472. DMERR("%s: unable to switch pool to write mode until repaired.",
  1473. dm_device_name(pool->pool_md));
  1474. if (old_mode != new_mode)
  1475. new_mode = old_mode;
  1476. else
  1477. new_mode = PM_READ_ONLY;
  1478. }
  1479. /*
  1480. * If we were in PM_FAIL mode, rollback of metadata failed. We're
  1481. * not going to recover without a thin_repair. So we never let the
  1482. * pool move out of the old mode.
  1483. */
  1484. if (old_mode == PM_FAIL)
  1485. new_mode = old_mode;
  1486. switch (new_mode) {
  1487. case PM_FAIL:
  1488. if (old_mode != new_mode)
  1489. notify_of_pool_mode_change(pool, "failure");
  1490. dm_pool_metadata_read_only(pool->pmd);
  1491. pool->process_bio = process_bio_fail;
  1492. pool->process_discard = process_bio_fail;
  1493. pool->process_prepared_mapping = process_prepared_mapping_fail;
  1494. pool->process_prepared_discard = process_prepared_discard_fail;
  1495. error_retry_list(pool);
  1496. break;
  1497. case PM_READ_ONLY:
  1498. if (old_mode != new_mode)
  1499. notify_of_pool_mode_change(pool, "read-only");
  1500. dm_pool_metadata_read_only(pool->pmd);
  1501. pool->process_bio = process_bio_read_only;
  1502. pool->process_discard = process_bio_success;
  1503. pool->process_prepared_mapping = process_prepared_mapping_fail;
  1504. pool->process_prepared_discard = process_prepared_discard_passdown;
  1505. error_retry_list(pool);
  1506. break;
  1507. case PM_OUT_OF_DATA_SPACE:
  1508. /*
  1509. * Ideally we'd never hit this state; the low water mark
  1510. * would trigger userland to extend the pool before we
  1511. * completely run out of data space. However, many small
  1512. * IOs to unprovisioned space can consume data space at an
  1513. * alarming rate. Adjust your low water mark if you're
  1514. * frequently seeing this mode.
  1515. */
  1516. if (old_mode != new_mode)
  1517. notify_of_pool_mode_change(pool, "out-of-data-space");
  1518. pool->process_bio = process_bio_read_only;
  1519. pool->process_discard = process_discard;
  1520. pool->process_prepared_mapping = process_prepared_mapping;
  1521. pool->process_prepared_discard = process_prepared_discard_passdown;
  1522. if (!pool->pf.error_if_no_space && no_space_timeout)
  1523. queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
  1524. break;
  1525. case PM_WRITE:
  1526. if (old_mode != new_mode)
  1527. notify_of_pool_mode_change(pool, "write");
  1528. dm_pool_metadata_read_write(pool->pmd);
  1529. pool->process_bio = process_bio;
  1530. pool->process_discard = process_discard;
  1531. pool->process_prepared_mapping = process_prepared_mapping;
  1532. pool->process_prepared_discard = process_prepared_discard;
  1533. break;
  1534. }
  1535. pool->pf.mode = new_mode;
  1536. /*
  1537. * The pool mode may have changed, sync it so bind_control_target()
  1538. * doesn't cause an unexpected mode transition on resume.
  1539. */
  1540. pt->adjusted_pf.mode = new_mode;
  1541. }
  1542. static void abort_transaction(struct pool *pool)
  1543. {
  1544. const char *dev_name = dm_device_name(pool->pool_md);
  1545. DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
  1546. if (dm_pool_abort_metadata(pool->pmd)) {
  1547. DMERR("%s: failed to abort metadata transaction", dev_name);
  1548. set_pool_mode(pool, PM_FAIL);
  1549. }
  1550. if (dm_pool_metadata_set_needs_check(pool->pmd)) {
  1551. DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
  1552. set_pool_mode(pool, PM_FAIL);
  1553. }
  1554. }
  1555. static void metadata_operation_failed(struct pool *pool, const char *op, int r)
  1556. {
  1557. DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
  1558. dm_device_name(pool->pool_md), op, r);
  1559. abort_transaction(pool);
  1560. set_pool_mode(pool, PM_READ_ONLY);
  1561. }
  1562. /*----------------------------------------------------------------*/
  1563. /*
  1564. * Mapping functions.
  1565. */
  1566. /*
  1567. * Called only while mapping a thin bio to hand it over to the workqueue.
  1568. */
  1569. static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
  1570. {
  1571. unsigned long flags;
  1572. struct pool *pool = tc->pool;
  1573. spin_lock_irqsave(&tc->lock, flags);
  1574. bio_list_add(&tc->deferred_bio_list, bio);
  1575. spin_unlock_irqrestore(&tc->lock, flags);
  1576. wake_worker(pool);
  1577. }
  1578. static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
  1579. {
  1580. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1581. h->tc = tc;
  1582. h->shared_read_entry = NULL;
  1583. h->all_io_entry = NULL;
  1584. h->overwrite_mapping = NULL;
  1585. }
  1586. /*
  1587. * Non-blocking function called from the thin target's map function.
  1588. */
  1589. static int thin_bio_map(struct dm_target *ti, struct bio *bio)
  1590. {
  1591. int r;
  1592. struct thin_c *tc = ti->private;
  1593. dm_block_t block = get_bio_block(tc, bio);
  1594. struct dm_thin_device *td = tc->td;
  1595. struct dm_thin_lookup_result result;
  1596. struct dm_bio_prison_cell cell1, cell2;
  1597. struct dm_bio_prison_cell *cell_result;
  1598. struct dm_cell_key key;
  1599. thin_hook_bio(tc, bio);
  1600. if (tc->requeue_mode) {
  1601. bio_endio(bio, DM_ENDIO_REQUEUE);
  1602. return DM_MAPIO_SUBMITTED;
  1603. }
  1604. if (get_pool_mode(tc->pool) == PM_FAIL) {
  1605. bio_io_error(bio);
  1606. return DM_MAPIO_SUBMITTED;
  1607. }
  1608. if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
  1609. thin_defer_bio(tc, bio);
  1610. return DM_MAPIO_SUBMITTED;
  1611. }
  1612. r = dm_thin_find_block(td, block, 0, &result);
  1613. /*
  1614. * Note that we defer readahead too.
  1615. */
  1616. switch (r) {
  1617. case 0:
  1618. if (unlikely(result.shared)) {
  1619. /*
  1620. * We have a race condition here between the
  1621. * result.shared value returned by the lookup and
  1622. * snapshot creation, which may cause new
  1623. * sharing.
  1624. *
  1625. * To avoid this always quiesce the origin before
  1626. * taking the snap. You want to do this anyway to
  1627. * ensure a consistent application view
  1628. * (i.e. lockfs).
  1629. *
  1630. * More distant ancestors are irrelevant. The
  1631. * shared flag will be set in their case.
  1632. */
  1633. thin_defer_bio(tc, bio);
  1634. return DM_MAPIO_SUBMITTED;
  1635. }
  1636. build_virtual_key(tc->td, block, &key);
  1637. if (dm_bio_detain(tc->pool->prison, &key, bio, &cell1, &cell_result))
  1638. return DM_MAPIO_SUBMITTED;
  1639. build_data_key(tc->td, result.block, &key);
  1640. if (dm_bio_detain(tc->pool->prison, &key, bio, &cell2, &cell_result)) {
  1641. cell_defer_no_holder_no_free(tc, &cell1);
  1642. return DM_MAPIO_SUBMITTED;
  1643. }
  1644. inc_all_io_entry(tc->pool, bio);
  1645. cell_defer_no_holder_no_free(tc, &cell2);
  1646. cell_defer_no_holder_no_free(tc, &cell1);
  1647. remap(tc, bio, result.block);
  1648. return DM_MAPIO_REMAPPED;
  1649. case -ENODATA:
  1650. if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
  1651. /*
  1652. * This block isn't provisioned, and we have no way
  1653. * of doing so.
  1654. */
  1655. handle_unserviceable_bio(tc->pool, bio);
  1656. return DM_MAPIO_SUBMITTED;
  1657. }
  1658. /* fall through */
  1659. case -EWOULDBLOCK:
  1660. /*
  1661. * In future, the failed dm_thin_find_block above could
  1662. * provide the hint to load the metadata into cache.
  1663. */
  1664. thin_defer_bio(tc, bio);
  1665. return DM_MAPIO_SUBMITTED;
  1666. default:
  1667. /*
  1668. * Must always call bio_io_error on failure.
  1669. * dm_thin_find_block can fail with -EINVAL if the
  1670. * pool is switched to fail-io mode.
  1671. */
  1672. bio_io_error(bio);
  1673. return DM_MAPIO_SUBMITTED;
  1674. }
  1675. }
  1676. static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1677. {
  1678. struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
  1679. struct request_queue *q;
  1680. if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
  1681. return 1;
  1682. q = bdev_get_queue(pt->data_dev->bdev);
  1683. return bdi_congested(&q->backing_dev_info, bdi_bits);
  1684. }
  1685. static void requeue_bios(struct pool *pool)
  1686. {
  1687. unsigned long flags;
  1688. struct thin_c *tc;
  1689. rcu_read_lock();
  1690. list_for_each_entry_rcu(tc, &pool->active_thins, list) {
  1691. spin_lock_irqsave(&tc->lock, flags);
  1692. bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
  1693. bio_list_init(&tc->retry_on_resume_list);
  1694. spin_unlock_irqrestore(&tc->lock, flags);
  1695. }
  1696. rcu_read_unlock();
  1697. }
  1698. /*----------------------------------------------------------------
  1699. * Binding of control targets to a pool object
  1700. *--------------------------------------------------------------*/
  1701. static bool data_dev_supports_discard(struct pool_c *pt)
  1702. {
  1703. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  1704. return q && blk_queue_discard(q);
  1705. }
  1706. static bool is_factor(sector_t block_size, uint32_t n)
  1707. {
  1708. return !sector_div(block_size, n);
  1709. }
  1710. /*
  1711. * If discard_passdown was enabled verify that the data device
  1712. * supports discards. Disable discard_passdown if not.
  1713. */
  1714. static void disable_passdown_if_not_supported(struct pool_c *pt)
  1715. {
  1716. struct pool *pool = pt->pool;
  1717. struct block_device *data_bdev = pt->data_dev->bdev;
  1718. struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
  1719. sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
  1720. const char *reason = NULL;
  1721. char buf[BDEVNAME_SIZE];
  1722. if (!pt->adjusted_pf.discard_passdown)
  1723. return;
  1724. if (!data_dev_supports_discard(pt))
  1725. reason = "discard unsupported";
  1726. else if (data_limits->max_discard_sectors < pool->sectors_per_block)
  1727. reason = "max discard sectors smaller than a block";
  1728. else if (data_limits->discard_granularity > block_size)
  1729. reason = "discard granularity larger than a block";
  1730. else if (!is_factor(block_size, data_limits->discard_granularity))
  1731. reason = "discard granularity not a factor of block size";
  1732. if (reason) {
  1733. DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
  1734. pt->adjusted_pf.discard_passdown = false;
  1735. }
  1736. }
  1737. static int bind_control_target(struct pool *pool, struct dm_target *ti)
  1738. {
  1739. struct pool_c *pt = ti->private;
  1740. /*
  1741. * We want to make sure that a pool in PM_FAIL mode is never upgraded.
  1742. */
  1743. enum pool_mode old_mode = get_pool_mode(pool);
  1744. enum pool_mode new_mode = pt->adjusted_pf.mode;
  1745. /*
  1746. * Don't change the pool's mode until set_pool_mode() below.
  1747. * Otherwise the pool's process_* function pointers may
  1748. * not match the desired pool mode.
  1749. */
  1750. pt->adjusted_pf.mode = old_mode;
  1751. pool->ti = ti;
  1752. pool->pf = pt->adjusted_pf;
  1753. pool->low_water_blocks = pt->low_water_blocks;
  1754. set_pool_mode(pool, new_mode);
  1755. return 0;
  1756. }
  1757. static void unbind_control_target(struct pool *pool, struct dm_target *ti)
  1758. {
  1759. if (pool->ti == ti)
  1760. pool->ti = NULL;
  1761. }
  1762. /*----------------------------------------------------------------
  1763. * Pool creation
  1764. *--------------------------------------------------------------*/
  1765. /* Initialize pool features. */
  1766. static void pool_features_init(struct pool_features *pf)
  1767. {
  1768. pf->mode = PM_WRITE;
  1769. pf->zero_new_blocks = true;
  1770. pf->discard_enabled = true;
  1771. pf->discard_passdown = true;
  1772. pf->error_if_no_space = false;
  1773. }
  1774. static void __pool_destroy(struct pool *pool)
  1775. {
  1776. __pool_table_remove(pool);
  1777. if (dm_pool_metadata_close(pool->pmd) < 0)
  1778. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  1779. dm_bio_prison_destroy(pool->prison);
  1780. dm_kcopyd_client_destroy(pool->copier);
  1781. if (pool->wq)
  1782. destroy_workqueue(pool->wq);
  1783. if (pool->next_mapping)
  1784. mempool_free(pool->next_mapping, pool->mapping_pool);
  1785. mempool_destroy(pool->mapping_pool);
  1786. dm_deferred_set_destroy(pool->shared_read_ds);
  1787. dm_deferred_set_destroy(pool->all_io_ds);
  1788. kfree(pool);
  1789. }
  1790. static struct kmem_cache *_new_mapping_cache;
  1791. static struct pool *pool_create(struct mapped_device *pool_md,
  1792. struct block_device *metadata_dev,
  1793. unsigned long block_size,
  1794. int read_only, char **error)
  1795. {
  1796. int r;
  1797. void *err_p;
  1798. struct pool *pool;
  1799. struct dm_pool_metadata *pmd;
  1800. bool format_device = read_only ? false : true;
  1801. pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
  1802. if (IS_ERR(pmd)) {
  1803. *error = "Error creating metadata object";
  1804. return (struct pool *)pmd;
  1805. }
  1806. pool = kmalloc(sizeof(*pool), GFP_KERNEL);
  1807. if (!pool) {
  1808. *error = "Error allocating memory for pool";
  1809. err_p = ERR_PTR(-ENOMEM);
  1810. goto bad_pool;
  1811. }
  1812. pool->pmd = pmd;
  1813. pool->sectors_per_block = block_size;
  1814. if (block_size & (block_size - 1))
  1815. pool->sectors_per_block_shift = -1;
  1816. else
  1817. pool->sectors_per_block_shift = __ffs(block_size);
  1818. pool->low_water_blocks = 0;
  1819. pool_features_init(&pool->pf);
  1820. pool->prison = dm_bio_prison_create(PRISON_CELLS);
  1821. if (!pool->prison) {
  1822. *error = "Error creating pool's bio prison";
  1823. err_p = ERR_PTR(-ENOMEM);
  1824. goto bad_prison;
  1825. }
  1826. pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  1827. if (IS_ERR(pool->copier)) {
  1828. r = PTR_ERR(pool->copier);
  1829. *error = "Error creating pool's kcopyd client";
  1830. err_p = ERR_PTR(r);
  1831. goto bad_kcopyd_client;
  1832. }
  1833. /*
  1834. * Create singlethreaded workqueue that will service all devices
  1835. * that use this metadata.
  1836. */
  1837. pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  1838. if (!pool->wq) {
  1839. *error = "Error creating pool's workqueue";
  1840. err_p = ERR_PTR(-ENOMEM);
  1841. goto bad_wq;
  1842. }
  1843. INIT_WORK(&pool->worker, do_worker);
  1844. INIT_DELAYED_WORK(&pool->waker, do_waker);
  1845. INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
  1846. spin_lock_init(&pool->lock);
  1847. bio_list_init(&pool->deferred_flush_bios);
  1848. INIT_LIST_HEAD(&pool->prepared_mappings);
  1849. INIT_LIST_HEAD(&pool->prepared_discards);
  1850. INIT_LIST_HEAD(&pool->active_thins);
  1851. pool->low_water_triggered = false;
  1852. pool->shared_read_ds = dm_deferred_set_create();
  1853. if (!pool->shared_read_ds) {
  1854. *error = "Error creating pool's shared read deferred set";
  1855. err_p = ERR_PTR(-ENOMEM);
  1856. goto bad_shared_read_ds;
  1857. }
  1858. pool->all_io_ds = dm_deferred_set_create();
  1859. if (!pool->all_io_ds) {
  1860. *error = "Error creating pool's all io deferred set";
  1861. err_p = ERR_PTR(-ENOMEM);
  1862. goto bad_all_io_ds;
  1863. }
  1864. pool->next_mapping = NULL;
  1865. pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
  1866. _new_mapping_cache);
  1867. if (!pool->mapping_pool) {
  1868. *error = "Error creating pool's mapping mempool";
  1869. err_p = ERR_PTR(-ENOMEM);
  1870. goto bad_mapping_pool;
  1871. }
  1872. pool->ref_count = 1;
  1873. pool->last_commit_jiffies = jiffies;
  1874. pool->pool_md = pool_md;
  1875. pool->md_dev = metadata_dev;
  1876. __pool_table_insert(pool);
  1877. return pool;
  1878. bad_mapping_pool:
  1879. dm_deferred_set_destroy(pool->all_io_ds);
  1880. bad_all_io_ds:
  1881. dm_deferred_set_destroy(pool->shared_read_ds);
  1882. bad_shared_read_ds:
  1883. destroy_workqueue(pool->wq);
  1884. bad_wq:
  1885. dm_kcopyd_client_destroy(pool->copier);
  1886. bad_kcopyd_client:
  1887. dm_bio_prison_destroy(pool->prison);
  1888. bad_prison:
  1889. kfree(pool);
  1890. bad_pool:
  1891. if (dm_pool_metadata_close(pmd))
  1892. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  1893. return err_p;
  1894. }
  1895. static void __pool_inc(struct pool *pool)
  1896. {
  1897. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  1898. pool->ref_count++;
  1899. }
  1900. static void __pool_dec(struct pool *pool)
  1901. {
  1902. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  1903. BUG_ON(!pool->ref_count);
  1904. if (!--pool->ref_count)
  1905. __pool_destroy(pool);
  1906. }
  1907. static struct pool *__pool_find(struct mapped_device *pool_md,
  1908. struct block_device *metadata_dev,
  1909. unsigned long block_size, int read_only,
  1910. char **error, int *created)
  1911. {
  1912. struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
  1913. if (pool) {
  1914. if (pool->pool_md != pool_md) {
  1915. *error = "metadata device already in use by a pool";
  1916. return ERR_PTR(-EBUSY);
  1917. }
  1918. __pool_inc(pool);
  1919. } else {
  1920. pool = __pool_table_lookup(pool_md);
  1921. if (pool) {
  1922. if (pool->md_dev != metadata_dev) {
  1923. *error = "different pool cannot replace a pool";
  1924. return ERR_PTR(-EINVAL);
  1925. }
  1926. __pool_inc(pool);
  1927. } else {
  1928. pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
  1929. *created = 1;
  1930. }
  1931. }
  1932. return pool;
  1933. }
  1934. /*----------------------------------------------------------------
  1935. * Pool target methods
  1936. *--------------------------------------------------------------*/
  1937. static void pool_dtr(struct dm_target *ti)
  1938. {
  1939. struct pool_c *pt = ti->private;
  1940. mutex_lock(&dm_thin_pool_table.mutex);
  1941. unbind_control_target(pt->pool, ti);
  1942. __pool_dec(pt->pool);
  1943. dm_put_device(ti, pt->metadata_dev);
  1944. dm_put_device(ti, pt->data_dev);
  1945. kfree(pt);
  1946. mutex_unlock(&dm_thin_pool_table.mutex);
  1947. }
  1948. static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
  1949. struct dm_target *ti)
  1950. {
  1951. int r;
  1952. unsigned argc;
  1953. const char *arg_name;
  1954. static struct dm_arg _args[] = {
  1955. {0, 4, "Invalid number of pool feature arguments"},
  1956. };
  1957. /*
  1958. * No feature arguments supplied.
  1959. */
  1960. if (!as->argc)
  1961. return 0;
  1962. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  1963. if (r)
  1964. return -EINVAL;
  1965. while (argc && !r) {
  1966. arg_name = dm_shift_arg(as);
  1967. argc--;
  1968. if (!strcasecmp(arg_name, "skip_block_zeroing"))
  1969. pf->zero_new_blocks = false;
  1970. else if (!strcasecmp(arg_name, "ignore_discard"))
  1971. pf->discard_enabled = false;
  1972. else if (!strcasecmp(arg_name, "no_discard_passdown"))
  1973. pf->discard_passdown = false;
  1974. else if (!strcasecmp(arg_name, "read_only"))
  1975. pf->mode = PM_READ_ONLY;
  1976. else if (!strcasecmp(arg_name, "error_if_no_space"))
  1977. pf->error_if_no_space = true;
  1978. else {
  1979. ti->error = "Unrecognised pool feature requested";
  1980. r = -EINVAL;
  1981. break;
  1982. }
  1983. }
  1984. return r;
  1985. }
  1986. static void metadata_low_callback(void *context)
  1987. {
  1988. struct pool *pool = context;
  1989. DMWARN("%s: reached low water mark for metadata device: sending event.",
  1990. dm_device_name(pool->pool_md));
  1991. dm_table_event(pool->ti->table);
  1992. }
  1993. static sector_t get_dev_size(struct block_device *bdev)
  1994. {
  1995. return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  1996. }
  1997. static void warn_if_metadata_device_too_big(struct block_device *bdev)
  1998. {
  1999. sector_t metadata_dev_size = get_dev_size(bdev);
  2000. char buffer[BDEVNAME_SIZE];
  2001. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
  2002. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  2003. bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
  2004. }
  2005. static sector_t get_metadata_dev_size(struct block_device *bdev)
  2006. {
  2007. sector_t metadata_dev_size = get_dev_size(bdev);
  2008. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
  2009. metadata_dev_size = THIN_METADATA_MAX_SECTORS;
  2010. return metadata_dev_size;
  2011. }
  2012. static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
  2013. {
  2014. sector_t metadata_dev_size = get_metadata_dev_size(bdev);
  2015. sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
  2016. return metadata_dev_size;
  2017. }
  2018. /*
  2019. * When a metadata threshold is crossed a dm event is triggered, and
  2020. * userland should respond by growing the metadata device. We could let
  2021. * userland set the threshold, like we do with the data threshold, but I'm
  2022. * not sure they know enough to do this well.
  2023. */
  2024. static dm_block_t calc_metadata_threshold(struct pool_c *pt)
  2025. {
  2026. /*
  2027. * 4M is ample for all ops with the possible exception of thin
  2028. * device deletion which is harmless if it fails (just retry the
  2029. * delete after you've grown the device).
  2030. */
  2031. dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
  2032. return min((dm_block_t)1024ULL /* 4M */, quarter);
  2033. }
  2034. /*
  2035. * thin-pool <metadata dev> <data dev>
  2036. * <data block size (sectors)>
  2037. * <low water mark (blocks)>
  2038. * [<#feature args> [<arg>]*]
  2039. *
  2040. * Optional feature arguments are:
  2041. * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
  2042. * ignore_discard: disable discard
  2043. * no_discard_passdown: don't pass discards down to the data device
  2044. * read_only: Don't allow any changes to be made to the pool metadata.
  2045. * error_if_no_space: error IOs, instead of queueing, if no space.
  2046. */
  2047. static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2048. {
  2049. int r, pool_created = 0;
  2050. struct pool_c *pt;
  2051. struct pool *pool;
  2052. struct pool_features pf;
  2053. struct dm_arg_set as;
  2054. struct dm_dev *data_dev;
  2055. unsigned long block_size;
  2056. dm_block_t low_water_blocks;
  2057. struct dm_dev *metadata_dev;
  2058. fmode_t metadata_mode;
  2059. /*
  2060. * FIXME Remove validation from scope of lock.
  2061. */
  2062. mutex_lock(&dm_thin_pool_table.mutex);
  2063. if (argc < 4) {
  2064. ti->error = "Invalid argument count";
  2065. r = -EINVAL;
  2066. goto out_unlock;
  2067. }
  2068. as.argc = argc;
  2069. as.argv = argv;
  2070. /*
  2071. * Set default pool features.
  2072. */
  2073. pool_features_init(&pf);
  2074. dm_consume_args(&as, 4);
  2075. r = parse_pool_features(&as, &pf, ti);
  2076. if (r)
  2077. goto out_unlock;
  2078. metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
  2079. r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
  2080. if (r) {
  2081. ti->error = "Error opening metadata block device";
  2082. goto out_unlock;
  2083. }
  2084. warn_if_metadata_device_too_big(metadata_dev->bdev);
  2085. r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
  2086. if (r) {
  2087. ti->error = "Error getting data device";
  2088. goto out_metadata;
  2089. }
  2090. if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
  2091. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  2092. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  2093. block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
  2094. ti->error = "Invalid block size";
  2095. r = -EINVAL;
  2096. goto out;
  2097. }
  2098. if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
  2099. ti->error = "Invalid low water mark";
  2100. r = -EINVAL;
  2101. goto out;
  2102. }
  2103. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  2104. if (!pt) {
  2105. r = -ENOMEM;
  2106. goto out;
  2107. }
  2108. pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
  2109. block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
  2110. if (IS_ERR(pool)) {
  2111. r = PTR_ERR(pool);
  2112. goto out_free_pt;
  2113. }
  2114. /*
  2115. * 'pool_created' reflects whether this is the first table load.
  2116. * Top level discard support is not allowed to be changed after
  2117. * initial load. This would require a pool reload to trigger thin
  2118. * device changes.
  2119. */
  2120. if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
  2121. ti->error = "Discard support cannot be disabled once enabled";
  2122. r = -EINVAL;
  2123. goto out_flags_changed;
  2124. }
  2125. pt->pool = pool;
  2126. pt->ti = ti;
  2127. pt->metadata_dev = metadata_dev;
  2128. pt->data_dev = data_dev;
  2129. pt->low_water_blocks = low_water_blocks;
  2130. pt->adjusted_pf = pt->requested_pf = pf;
  2131. ti->num_flush_bios = 1;
  2132. /*
  2133. * Only need to enable discards if the pool should pass
  2134. * them down to the data device. The thin device's discard
  2135. * processing will cause mappings to be removed from the btree.
  2136. */
  2137. ti->discard_zeroes_data_unsupported = true;
  2138. if (pf.discard_enabled && pf.discard_passdown) {
  2139. ti->num_discard_bios = 1;
  2140. /*
  2141. * Setting 'discards_supported' circumvents the normal
  2142. * stacking of discard limits (this keeps the pool and
  2143. * thin devices' discard limits consistent).
  2144. */
  2145. ti->discards_supported = true;
  2146. }
  2147. ti->private = pt;
  2148. r = dm_pool_register_metadata_threshold(pt->pool->pmd,
  2149. calc_metadata_threshold(pt),
  2150. metadata_low_callback,
  2151. pool);
  2152. if (r)
  2153. goto out_free_pt;
  2154. pt->callbacks.congested_fn = pool_is_congested;
  2155. dm_table_add_target_callbacks(ti->table, &pt->callbacks);
  2156. mutex_unlock(&dm_thin_pool_table.mutex);
  2157. return 0;
  2158. out_flags_changed:
  2159. __pool_dec(pool);
  2160. out_free_pt:
  2161. kfree(pt);
  2162. out:
  2163. dm_put_device(ti, data_dev);
  2164. out_metadata:
  2165. dm_put_device(ti, metadata_dev);
  2166. out_unlock:
  2167. mutex_unlock(&dm_thin_pool_table.mutex);
  2168. return r;
  2169. }
  2170. static int pool_map(struct dm_target *ti, struct bio *bio)
  2171. {
  2172. int r;
  2173. struct pool_c *pt = ti->private;
  2174. struct pool *pool = pt->pool;
  2175. unsigned long flags;
  2176. /*
  2177. * As this is a singleton target, ti->begin is always zero.
  2178. */
  2179. spin_lock_irqsave(&pool->lock, flags);
  2180. bio->bi_bdev = pt->data_dev->bdev;
  2181. r = DM_MAPIO_REMAPPED;
  2182. spin_unlock_irqrestore(&pool->lock, flags);
  2183. return r;
  2184. }
  2185. static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
  2186. {
  2187. int r;
  2188. struct pool_c *pt = ti->private;
  2189. struct pool *pool = pt->pool;
  2190. sector_t data_size = ti->len;
  2191. dm_block_t sb_data_size;
  2192. *need_commit = false;
  2193. (void) sector_div(data_size, pool->sectors_per_block);
  2194. r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
  2195. if (r) {
  2196. DMERR("%s: failed to retrieve data device size",
  2197. dm_device_name(pool->pool_md));
  2198. return r;
  2199. }
  2200. if (data_size < sb_data_size) {
  2201. DMERR("%s: pool target (%llu blocks) too small: expected %llu",
  2202. dm_device_name(pool->pool_md),
  2203. (unsigned long long)data_size, sb_data_size);
  2204. return -EINVAL;
  2205. } else if (data_size > sb_data_size) {
  2206. if (dm_pool_metadata_needs_check(pool->pmd)) {
  2207. DMERR("%s: unable to grow the data device until repaired.",
  2208. dm_device_name(pool->pool_md));
  2209. return 0;
  2210. }
  2211. if (sb_data_size)
  2212. DMINFO("%s: growing the data device from %llu to %llu blocks",
  2213. dm_device_name(pool->pool_md),
  2214. sb_data_size, (unsigned long long)data_size);
  2215. r = dm_pool_resize_data_dev(pool->pmd, data_size);
  2216. if (r) {
  2217. metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
  2218. return r;
  2219. }
  2220. *need_commit = true;
  2221. }
  2222. return 0;
  2223. }
  2224. static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
  2225. {
  2226. int r;
  2227. struct pool_c *pt = ti->private;
  2228. struct pool *pool = pt->pool;
  2229. dm_block_t metadata_dev_size, sb_metadata_dev_size;
  2230. *need_commit = false;
  2231. metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
  2232. r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
  2233. if (r) {
  2234. DMERR("%s: failed to retrieve metadata device size",
  2235. dm_device_name(pool->pool_md));
  2236. return r;
  2237. }
  2238. if (metadata_dev_size < sb_metadata_dev_size) {
  2239. DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
  2240. dm_device_name(pool->pool_md),
  2241. metadata_dev_size, sb_metadata_dev_size);
  2242. return -EINVAL;
  2243. } else if (metadata_dev_size > sb_metadata_dev_size) {
  2244. if (dm_pool_metadata_needs_check(pool->pmd)) {
  2245. DMERR("%s: unable to grow the metadata device until repaired.",
  2246. dm_device_name(pool->pool_md));
  2247. return 0;
  2248. }
  2249. warn_if_metadata_device_too_big(pool->md_dev);
  2250. DMINFO("%s: growing the metadata device from %llu to %llu blocks",
  2251. dm_device_name(pool->pool_md),
  2252. sb_metadata_dev_size, metadata_dev_size);
  2253. r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
  2254. if (r) {
  2255. metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
  2256. return r;
  2257. }
  2258. *need_commit = true;
  2259. }
  2260. return 0;
  2261. }
  2262. /*
  2263. * Retrieves the number of blocks of the data device from
  2264. * the superblock and compares it to the actual device size,
  2265. * thus resizing the data device in case it has grown.
  2266. *
  2267. * This both copes with opening preallocated data devices in the ctr
  2268. * being followed by a resume
  2269. * -and-
  2270. * calling the resume method individually after userspace has
  2271. * grown the data device in reaction to a table event.
  2272. */
  2273. static int pool_preresume(struct dm_target *ti)
  2274. {
  2275. int r;
  2276. bool need_commit1, need_commit2;
  2277. struct pool_c *pt = ti->private;
  2278. struct pool *pool = pt->pool;
  2279. /*
  2280. * Take control of the pool object.
  2281. */
  2282. r = bind_control_target(pool, ti);
  2283. if (r)
  2284. return r;
  2285. r = maybe_resize_data_dev(ti, &need_commit1);
  2286. if (r)
  2287. return r;
  2288. r = maybe_resize_metadata_dev(ti, &need_commit2);
  2289. if (r)
  2290. return r;
  2291. if (need_commit1 || need_commit2)
  2292. (void) commit(pool);
  2293. return 0;
  2294. }
  2295. static void pool_resume(struct dm_target *ti)
  2296. {
  2297. struct pool_c *pt = ti->private;
  2298. struct pool *pool = pt->pool;
  2299. unsigned long flags;
  2300. spin_lock_irqsave(&pool->lock, flags);
  2301. pool->low_water_triggered = false;
  2302. spin_unlock_irqrestore(&pool->lock, flags);
  2303. requeue_bios(pool);
  2304. do_waker(&pool->waker.work);
  2305. }
  2306. static void pool_postsuspend(struct dm_target *ti)
  2307. {
  2308. struct pool_c *pt = ti->private;
  2309. struct pool *pool = pt->pool;
  2310. cancel_delayed_work(&pool->waker);
  2311. cancel_delayed_work(&pool->no_space_timeout);
  2312. flush_workqueue(pool->wq);
  2313. (void) commit(pool);
  2314. }
  2315. static int check_arg_count(unsigned argc, unsigned args_required)
  2316. {
  2317. if (argc != args_required) {
  2318. DMWARN("Message received with %u arguments instead of %u.",
  2319. argc, args_required);
  2320. return -EINVAL;
  2321. }
  2322. return 0;
  2323. }
  2324. static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
  2325. {
  2326. if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
  2327. *dev_id <= MAX_DEV_ID)
  2328. return 0;
  2329. if (warning)
  2330. DMWARN("Message received with invalid device id: %s", arg);
  2331. return -EINVAL;
  2332. }
  2333. static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
  2334. {
  2335. dm_thin_id dev_id;
  2336. int r;
  2337. r = check_arg_count(argc, 2);
  2338. if (r)
  2339. return r;
  2340. r = read_dev_id(argv[1], &dev_id, 1);
  2341. if (r)
  2342. return r;
  2343. r = dm_pool_create_thin(pool->pmd, dev_id);
  2344. if (r) {
  2345. DMWARN("Creation of new thinly-provisioned device with id %s failed.",
  2346. argv[1]);
  2347. return r;
  2348. }
  2349. return 0;
  2350. }
  2351. static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  2352. {
  2353. dm_thin_id dev_id;
  2354. dm_thin_id origin_dev_id;
  2355. int r;
  2356. r = check_arg_count(argc, 3);
  2357. if (r)
  2358. return r;
  2359. r = read_dev_id(argv[1], &dev_id, 1);
  2360. if (r)
  2361. return r;
  2362. r = read_dev_id(argv[2], &origin_dev_id, 1);
  2363. if (r)
  2364. return r;
  2365. r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
  2366. if (r) {
  2367. DMWARN("Creation of new snapshot %s of device %s failed.",
  2368. argv[1], argv[2]);
  2369. return r;
  2370. }
  2371. return 0;
  2372. }
  2373. static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
  2374. {
  2375. dm_thin_id dev_id;
  2376. int r;
  2377. r = check_arg_count(argc, 2);
  2378. if (r)
  2379. return r;
  2380. r = read_dev_id(argv[1], &dev_id, 1);
  2381. if (r)
  2382. return r;
  2383. r = dm_pool_delete_thin_device(pool->pmd, dev_id);
  2384. if (r)
  2385. DMWARN("Deletion of thin device %s failed.", argv[1]);
  2386. return r;
  2387. }
  2388. static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
  2389. {
  2390. dm_thin_id old_id, new_id;
  2391. int r;
  2392. r = check_arg_count(argc, 3);
  2393. if (r)
  2394. return r;
  2395. if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
  2396. DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
  2397. return -EINVAL;
  2398. }
  2399. if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
  2400. DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
  2401. return -EINVAL;
  2402. }
  2403. r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
  2404. if (r) {
  2405. DMWARN("Failed to change transaction id from %s to %s.",
  2406. argv[1], argv[2]);
  2407. return r;
  2408. }
  2409. return 0;
  2410. }
  2411. static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  2412. {
  2413. int r;
  2414. r = check_arg_count(argc, 1);
  2415. if (r)
  2416. return r;
  2417. (void) commit(pool);
  2418. r = dm_pool_reserve_metadata_snap(pool->pmd);
  2419. if (r)
  2420. DMWARN("reserve_metadata_snap message failed.");
  2421. return r;
  2422. }
  2423. static int process_release_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. r = dm_pool_release_metadata_snap(pool->pmd);
  2430. if (r)
  2431. DMWARN("release_metadata_snap message failed.");
  2432. return r;
  2433. }
  2434. /*
  2435. * Messages supported:
  2436. * create_thin <dev_id>
  2437. * create_snap <dev_id> <origin_id>
  2438. * delete <dev_id>
  2439. * trim <dev_id> <new_size_in_sectors>
  2440. * set_transaction_id <current_trans_id> <new_trans_id>
  2441. * reserve_metadata_snap
  2442. * release_metadata_snap
  2443. */
  2444. static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
  2445. {
  2446. int r = -EINVAL;
  2447. struct pool_c *pt = ti->private;
  2448. struct pool *pool = pt->pool;
  2449. if (!strcasecmp(argv[0], "create_thin"))
  2450. r = process_create_thin_mesg(argc, argv, pool);
  2451. else if (!strcasecmp(argv[0], "create_snap"))
  2452. r = process_create_snap_mesg(argc, argv, pool);
  2453. else if (!strcasecmp(argv[0], "delete"))
  2454. r = process_delete_mesg(argc, argv, pool);
  2455. else if (!strcasecmp(argv[0], "set_transaction_id"))
  2456. r = process_set_transaction_id_mesg(argc, argv, pool);
  2457. else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
  2458. r = process_reserve_metadata_snap_mesg(argc, argv, pool);
  2459. else if (!strcasecmp(argv[0], "release_metadata_snap"))
  2460. r = process_release_metadata_snap_mesg(argc, argv, pool);
  2461. else
  2462. DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
  2463. if (!r)
  2464. (void) commit(pool);
  2465. return r;
  2466. }
  2467. static void emit_flags(struct pool_features *pf, char *result,
  2468. unsigned sz, unsigned maxlen)
  2469. {
  2470. unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
  2471. !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
  2472. pf->error_if_no_space;
  2473. DMEMIT("%u ", count);
  2474. if (!pf->zero_new_blocks)
  2475. DMEMIT("skip_block_zeroing ");
  2476. if (!pf->discard_enabled)
  2477. DMEMIT("ignore_discard ");
  2478. if (!pf->discard_passdown)
  2479. DMEMIT("no_discard_passdown ");
  2480. if (pf->mode == PM_READ_ONLY)
  2481. DMEMIT("read_only ");
  2482. if (pf->error_if_no_space)
  2483. DMEMIT("error_if_no_space ");
  2484. }
  2485. /*
  2486. * Status line is:
  2487. * <transaction id> <used metadata sectors>/<total metadata sectors>
  2488. * <used data sectors>/<total data sectors> <held metadata root>
  2489. */
  2490. static void pool_status(struct dm_target *ti, status_type_t type,
  2491. unsigned status_flags, char *result, unsigned maxlen)
  2492. {
  2493. int r;
  2494. unsigned sz = 0;
  2495. uint64_t transaction_id;
  2496. dm_block_t nr_free_blocks_data;
  2497. dm_block_t nr_free_blocks_metadata;
  2498. dm_block_t nr_blocks_data;
  2499. dm_block_t nr_blocks_metadata;
  2500. dm_block_t held_root;
  2501. char buf[BDEVNAME_SIZE];
  2502. char buf2[BDEVNAME_SIZE];
  2503. struct pool_c *pt = ti->private;
  2504. struct pool *pool = pt->pool;
  2505. switch (type) {
  2506. case STATUSTYPE_INFO:
  2507. if (get_pool_mode(pool) == PM_FAIL) {
  2508. DMEMIT("Fail");
  2509. break;
  2510. }
  2511. /* Commit to ensure statistics aren't out-of-date */
  2512. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
  2513. (void) commit(pool);
  2514. r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
  2515. if (r) {
  2516. DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
  2517. dm_device_name(pool->pool_md), r);
  2518. goto err;
  2519. }
  2520. r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
  2521. if (r) {
  2522. DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
  2523. dm_device_name(pool->pool_md), r);
  2524. goto err;
  2525. }
  2526. r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
  2527. if (r) {
  2528. DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
  2529. dm_device_name(pool->pool_md), r);
  2530. goto err;
  2531. }
  2532. r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
  2533. if (r) {
  2534. DMERR("%s: dm_pool_get_free_block_count returned %d",
  2535. dm_device_name(pool->pool_md), r);
  2536. goto err;
  2537. }
  2538. r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
  2539. if (r) {
  2540. DMERR("%s: dm_pool_get_data_dev_size returned %d",
  2541. dm_device_name(pool->pool_md), r);
  2542. goto err;
  2543. }
  2544. r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
  2545. if (r) {
  2546. DMERR("%s: dm_pool_get_metadata_snap returned %d",
  2547. dm_device_name(pool->pool_md), r);
  2548. goto err;
  2549. }
  2550. DMEMIT("%llu %llu/%llu %llu/%llu ",
  2551. (unsigned long long)transaction_id,
  2552. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  2553. (unsigned long long)nr_blocks_metadata,
  2554. (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
  2555. (unsigned long long)nr_blocks_data);
  2556. if (held_root)
  2557. DMEMIT("%llu ", held_root);
  2558. else
  2559. DMEMIT("- ");
  2560. if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
  2561. DMEMIT("out_of_data_space ");
  2562. else if (pool->pf.mode == PM_READ_ONLY)
  2563. DMEMIT("ro ");
  2564. else
  2565. DMEMIT("rw ");
  2566. if (!pool->pf.discard_enabled)
  2567. DMEMIT("ignore_discard ");
  2568. else if (pool->pf.discard_passdown)
  2569. DMEMIT("discard_passdown ");
  2570. else
  2571. DMEMIT("no_discard_passdown ");
  2572. if (pool->pf.error_if_no_space)
  2573. DMEMIT("error_if_no_space ");
  2574. else
  2575. DMEMIT("queue_if_no_space ");
  2576. break;
  2577. case STATUSTYPE_TABLE:
  2578. DMEMIT("%s %s %lu %llu ",
  2579. format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
  2580. format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
  2581. (unsigned long)pool->sectors_per_block,
  2582. (unsigned long long)pt->low_water_blocks);
  2583. emit_flags(&pt->requested_pf, result, sz, maxlen);
  2584. break;
  2585. }
  2586. return;
  2587. err:
  2588. DMEMIT("Error");
  2589. }
  2590. static int pool_iterate_devices(struct dm_target *ti,
  2591. iterate_devices_callout_fn fn, void *data)
  2592. {
  2593. struct pool_c *pt = ti->private;
  2594. return fn(ti, pt->data_dev, 0, ti->len, data);
  2595. }
  2596. static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  2597. struct bio_vec *biovec, int max_size)
  2598. {
  2599. struct pool_c *pt = ti->private;
  2600. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  2601. if (!q->merge_bvec_fn)
  2602. return max_size;
  2603. bvm->bi_bdev = pt->data_dev->bdev;
  2604. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  2605. }
  2606. static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
  2607. {
  2608. struct pool *pool = pt->pool;
  2609. struct queue_limits *data_limits;
  2610. limits->max_discard_sectors = pool->sectors_per_block;
  2611. /*
  2612. * discard_granularity is just a hint, and not enforced.
  2613. */
  2614. if (pt->adjusted_pf.discard_passdown) {
  2615. data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
  2616. limits->discard_granularity = max(data_limits->discard_granularity,
  2617. pool->sectors_per_block << SECTOR_SHIFT);
  2618. } else
  2619. limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
  2620. }
  2621. static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2622. {
  2623. struct pool_c *pt = ti->private;
  2624. struct pool *pool = pt->pool;
  2625. uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  2626. /*
  2627. * If the system-determined stacked limits are compatible with the
  2628. * pool's blocksize (io_opt is a factor) do not override them.
  2629. */
  2630. if (io_opt_sectors < pool->sectors_per_block ||
  2631. do_div(io_opt_sectors, pool->sectors_per_block)) {
  2632. blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
  2633. blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
  2634. }
  2635. /*
  2636. * pt->adjusted_pf is a staging area for the actual features to use.
  2637. * They get transferred to the live pool in bind_control_target()
  2638. * called from pool_preresume().
  2639. */
  2640. if (!pt->adjusted_pf.discard_enabled) {
  2641. /*
  2642. * Must explicitly disallow stacking discard limits otherwise the
  2643. * block layer will stack them if pool's data device has support.
  2644. * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
  2645. * user to see that, so make sure to set all discard limits to 0.
  2646. */
  2647. limits->discard_granularity = 0;
  2648. return;
  2649. }
  2650. disable_passdown_if_not_supported(pt);
  2651. set_discard_limits(pt, limits);
  2652. }
  2653. static struct target_type pool_target = {
  2654. .name = "thin-pool",
  2655. .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
  2656. DM_TARGET_IMMUTABLE,
  2657. .version = {1, 13, 0},
  2658. .module = THIS_MODULE,
  2659. .ctr = pool_ctr,
  2660. .dtr = pool_dtr,
  2661. .map = pool_map,
  2662. .postsuspend = pool_postsuspend,
  2663. .preresume = pool_preresume,
  2664. .resume = pool_resume,
  2665. .message = pool_message,
  2666. .status = pool_status,
  2667. .merge = pool_merge,
  2668. .iterate_devices = pool_iterate_devices,
  2669. .io_hints = pool_io_hints,
  2670. };
  2671. /*----------------------------------------------------------------
  2672. * Thin target methods
  2673. *--------------------------------------------------------------*/
  2674. static void thin_get(struct thin_c *tc)
  2675. {
  2676. atomic_inc(&tc->refcount);
  2677. }
  2678. static void thin_put(struct thin_c *tc)
  2679. {
  2680. if (atomic_dec_and_test(&tc->refcount))
  2681. complete(&tc->can_destroy);
  2682. }
  2683. static void thin_dtr(struct dm_target *ti)
  2684. {
  2685. struct thin_c *tc = ti->private;
  2686. unsigned long flags;
  2687. thin_put(tc);
  2688. wait_for_completion(&tc->can_destroy);
  2689. spin_lock_irqsave(&tc->pool->lock, flags);
  2690. list_del_rcu(&tc->list);
  2691. spin_unlock_irqrestore(&tc->pool->lock, flags);
  2692. synchronize_rcu();
  2693. mutex_lock(&dm_thin_pool_table.mutex);
  2694. __pool_dec(tc->pool);
  2695. dm_pool_close_thin_device(tc->td);
  2696. dm_put_device(ti, tc->pool_dev);
  2697. if (tc->origin_dev)
  2698. dm_put_device(ti, tc->origin_dev);
  2699. kfree(tc);
  2700. mutex_unlock(&dm_thin_pool_table.mutex);
  2701. }
  2702. /*
  2703. * Thin target parameters:
  2704. *
  2705. * <pool_dev> <dev_id> [origin_dev]
  2706. *
  2707. * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
  2708. * dev_id: the internal device identifier
  2709. * origin_dev: a device external to the pool that should act as the origin
  2710. *
  2711. * If the pool device has discards disabled, they get disabled for the thin
  2712. * device as well.
  2713. */
  2714. static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2715. {
  2716. int r;
  2717. struct thin_c *tc;
  2718. struct dm_dev *pool_dev, *origin_dev;
  2719. struct mapped_device *pool_md;
  2720. unsigned long flags;
  2721. mutex_lock(&dm_thin_pool_table.mutex);
  2722. if (argc != 2 && argc != 3) {
  2723. ti->error = "Invalid argument count";
  2724. r = -EINVAL;
  2725. goto out_unlock;
  2726. }
  2727. tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
  2728. if (!tc) {
  2729. ti->error = "Out of memory";
  2730. r = -ENOMEM;
  2731. goto out_unlock;
  2732. }
  2733. spin_lock_init(&tc->lock);
  2734. bio_list_init(&tc->deferred_bio_list);
  2735. bio_list_init(&tc->retry_on_resume_list);
  2736. tc->sort_bio_list = RB_ROOT;
  2737. if (argc == 3) {
  2738. r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
  2739. if (r) {
  2740. ti->error = "Error opening origin device";
  2741. goto bad_origin_dev;
  2742. }
  2743. tc->origin_dev = origin_dev;
  2744. }
  2745. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
  2746. if (r) {
  2747. ti->error = "Error opening pool device";
  2748. goto bad_pool_dev;
  2749. }
  2750. tc->pool_dev = pool_dev;
  2751. if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
  2752. ti->error = "Invalid device id";
  2753. r = -EINVAL;
  2754. goto bad_common;
  2755. }
  2756. pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
  2757. if (!pool_md) {
  2758. ti->error = "Couldn't get pool mapped device";
  2759. r = -EINVAL;
  2760. goto bad_common;
  2761. }
  2762. tc->pool = __pool_table_lookup(pool_md);
  2763. if (!tc->pool) {
  2764. ti->error = "Couldn't find pool object";
  2765. r = -EINVAL;
  2766. goto bad_pool_lookup;
  2767. }
  2768. __pool_inc(tc->pool);
  2769. if (get_pool_mode(tc->pool) == PM_FAIL) {
  2770. ti->error = "Couldn't open thin device, Pool is in fail mode";
  2771. r = -EINVAL;
  2772. goto bad_thin_open;
  2773. }
  2774. r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
  2775. if (r) {
  2776. ti->error = "Couldn't open thin internal device";
  2777. goto bad_thin_open;
  2778. }
  2779. r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
  2780. if (r)
  2781. goto bad_target_max_io_len;
  2782. ti->num_flush_bios = 1;
  2783. ti->flush_supported = true;
  2784. ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
  2785. /* In case the pool supports discards, pass them on. */
  2786. ti->discard_zeroes_data_unsupported = true;
  2787. if (tc->pool->pf.discard_enabled) {
  2788. ti->discards_supported = true;
  2789. ti->num_discard_bios = 1;
  2790. /* Discard bios must be split on a block boundary */
  2791. ti->split_discard_bios = true;
  2792. }
  2793. dm_put(pool_md);
  2794. mutex_unlock(&dm_thin_pool_table.mutex);
  2795. atomic_set(&tc->refcount, 1);
  2796. init_completion(&tc->can_destroy);
  2797. spin_lock_irqsave(&tc->pool->lock, flags);
  2798. list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
  2799. spin_unlock_irqrestore(&tc->pool->lock, flags);
  2800. /*
  2801. * This synchronize_rcu() call is needed here otherwise we risk a
  2802. * wake_worker() call finding no bios to process (because the newly
  2803. * added tc isn't yet visible). So this reduces latency since we
  2804. * aren't then dependent on the periodic commit to wake_worker().
  2805. */
  2806. synchronize_rcu();
  2807. return 0;
  2808. bad_target_max_io_len:
  2809. dm_pool_close_thin_device(tc->td);
  2810. bad_thin_open:
  2811. __pool_dec(tc->pool);
  2812. bad_pool_lookup:
  2813. dm_put(pool_md);
  2814. bad_common:
  2815. dm_put_device(ti, tc->pool_dev);
  2816. bad_pool_dev:
  2817. if (tc->origin_dev)
  2818. dm_put_device(ti, tc->origin_dev);
  2819. bad_origin_dev:
  2820. kfree(tc);
  2821. out_unlock:
  2822. mutex_unlock(&dm_thin_pool_table.mutex);
  2823. return r;
  2824. }
  2825. static int thin_map(struct dm_target *ti, struct bio *bio)
  2826. {
  2827. bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
  2828. return thin_bio_map(ti, bio);
  2829. }
  2830. static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
  2831. {
  2832. unsigned long flags;
  2833. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  2834. struct list_head work;
  2835. struct dm_thin_new_mapping *m, *tmp;
  2836. struct pool *pool = h->tc->pool;
  2837. if (h->shared_read_entry) {
  2838. INIT_LIST_HEAD(&work);
  2839. dm_deferred_entry_dec(h->shared_read_entry, &work);
  2840. spin_lock_irqsave(&pool->lock, flags);
  2841. list_for_each_entry_safe(m, tmp, &work, list) {
  2842. list_del(&m->list);
  2843. __complete_mapping_preparation(m);
  2844. }
  2845. spin_unlock_irqrestore(&pool->lock, flags);
  2846. }
  2847. if (h->all_io_entry) {
  2848. INIT_LIST_HEAD(&work);
  2849. dm_deferred_entry_dec(h->all_io_entry, &work);
  2850. if (!list_empty(&work)) {
  2851. spin_lock_irqsave(&pool->lock, flags);
  2852. list_for_each_entry_safe(m, tmp, &work, list)
  2853. list_add_tail(&m->list, &pool->prepared_discards);
  2854. spin_unlock_irqrestore(&pool->lock, flags);
  2855. wake_worker(pool);
  2856. }
  2857. }
  2858. return 0;
  2859. }
  2860. static void thin_presuspend(struct dm_target *ti)
  2861. {
  2862. struct thin_c *tc = ti->private;
  2863. if (dm_noflush_suspending(ti))
  2864. noflush_work(tc, do_noflush_start);
  2865. }
  2866. static void thin_postsuspend(struct dm_target *ti)
  2867. {
  2868. struct thin_c *tc = ti->private;
  2869. /*
  2870. * The dm_noflush_suspending flag has been cleared by now, so
  2871. * unfortunately we must always run this.
  2872. */
  2873. noflush_work(tc, do_noflush_stop);
  2874. }
  2875. static int thin_preresume(struct dm_target *ti)
  2876. {
  2877. struct thin_c *tc = ti->private;
  2878. if (tc->origin_dev)
  2879. tc->origin_size = get_dev_size(tc->origin_dev->bdev);
  2880. return 0;
  2881. }
  2882. /*
  2883. * <nr mapped sectors> <highest mapped sector>
  2884. */
  2885. static void thin_status(struct dm_target *ti, status_type_t type,
  2886. unsigned status_flags, char *result, unsigned maxlen)
  2887. {
  2888. int r;
  2889. ssize_t sz = 0;
  2890. dm_block_t mapped, highest;
  2891. char buf[BDEVNAME_SIZE];
  2892. struct thin_c *tc = ti->private;
  2893. if (get_pool_mode(tc->pool) == PM_FAIL) {
  2894. DMEMIT("Fail");
  2895. return;
  2896. }
  2897. if (!tc->td)
  2898. DMEMIT("-");
  2899. else {
  2900. switch (type) {
  2901. case STATUSTYPE_INFO:
  2902. r = dm_thin_get_mapped_count(tc->td, &mapped);
  2903. if (r) {
  2904. DMERR("dm_thin_get_mapped_count returned %d", r);
  2905. goto err;
  2906. }
  2907. r = dm_thin_get_highest_mapped_block(tc->td, &highest);
  2908. if (r < 0) {
  2909. DMERR("dm_thin_get_highest_mapped_block returned %d", r);
  2910. goto err;
  2911. }
  2912. DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
  2913. if (r)
  2914. DMEMIT("%llu", ((highest + 1) *
  2915. tc->pool->sectors_per_block) - 1);
  2916. else
  2917. DMEMIT("-");
  2918. break;
  2919. case STATUSTYPE_TABLE:
  2920. DMEMIT("%s %lu",
  2921. format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
  2922. (unsigned long) tc->dev_id);
  2923. if (tc->origin_dev)
  2924. DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
  2925. break;
  2926. }
  2927. }
  2928. return;
  2929. err:
  2930. DMEMIT("Error");
  2931. }
  2932. static int thin_iterate_devices(struct dm_target *ti,
  2933. iterate_devices_callout_fn fn, void *data)
  2934. {
  2935. sector_t blocks;
  2936. struct thin_c *tc = ti->private;
  2937. struct pool *pool = tc->pool;
  2938. /*
  2939. * We can't call dm_pool_get_data_dev_size() since that blocks. So
  2940. * we follow a more convoluted path through to the pool's target.
  2941. */
  2942. if (!pool->ti)
  2943. return 0; /* nothing is bound */
  2944. blocks = pool->ti->len;
  2945. (void) sector_div(blocks, pool->sectors_per_block);
  2946. if (blocks)
  2947. return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
  2948. return 0;
  2949. }
  2950. static struct target_type thin_target = {
  2951. .name = "thin",
  2952. .version = {1, 13, 0},
  2953. .module = THIS_MODULE,
  2954. .ctr = thin_ctr,
  2955. .dtr = thin_dtr,
  2956. .map = thin_map,
  2957. .end_io = thin_endio,
  2958. .preresume = thin_preresume,
  2959. .presuspend = thin_presuspend,
  2960. .postsuspend = thin_postsuspend,
  2961. .status = thin_status,
  2962. .iterate_devices = thin_iterate_devices,
  2963. };
  2964. /*----------------------------------------------------------------*/
  2965. static int __init dm_thin_init(void)
  2966. {
  2967. int r;
  2968. pool_table_init();
  2969. r = dm_register_target(&thin_target);
  2970. if (r)
  2971. return r;
  2972. r = dm_register_target(&pool_target);
  2973. if (r)
  2974. goto bad_pool_target;
  2975. r = -ENOMEM;
  2976. _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
  2977. if (!_new_mapping_cache)
  2978. goto bad_new_mapping_cache;
  2979. return 0;
  2980. bad_new_mapping_cache:
  2981. dm_unregister_target(&pool_target);
  2982. bad_pool_target:
  2983. dm_unregister_target(&thin_target);
  2984. return r;
  2985. }
  2986. static void dm_thin_exit(void)
  2987. {
  2988. dm_unregister_target(&thin_target);
  2989. dm_unregister_target(&pool_target);
  2990. kmem_cache_destroy(_new_mapping_cache);
  2991. }
  2992. module_init(dm_thin_init);
  2993. module_exit(dm_thin_exit);
  2994. module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
  2995. MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
  2996. MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
  2997. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2998. MODULE_LICENSE("GPL");