dm-thin.c 74 KB

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