dm-thin.c 88 KB

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