dm-thin.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856
  1. /*
  2. * Copyright (C) 2011 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 10240
  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. struct pool_features {
  416. unsigned zero_new_blocks:1;
  417. unsigned discard_enabled:1;
  418. unsigned discard_passdown:1;
  419. };
  420. struct pool {
  421. struct list_head list;
  422. struct dm_target *ti; /* Only set if a pool target is bound */
  423. struct mapped_device *pool_md;
  424. struct block_device *md_dev;
  425. struct dm_pool_metadata *pmd;
  426. uint32_t sectors_per_block;
  427. unsigned block_shift;
  428. dm_block_t offset_mask;
  429. dm_block_t low_water_blocks;
  430. struct pool_features pf;
  431. unsigned low_water_triggered:1; /* A dm event has been sent */
  432. unsigned no_free_space:1; /* A -ENOSPC warning has been issued */
  433. struct bio_prison *prison;
  434. struct dm_kcopyd_client *copier;
  435. struct workqueue_struct *wq;
  436. struct work_struct worker;
  437. struct delayed_work waker;
  438. unsigned ref_count;
  439. unsigned long last_commit_jiffies;
  440. spinlock_t lock;
  441. struct bio_list deferred_bios;
  442. struct bio_list deferred_flush_bios;
  443. struct list_head prepared_mappings;
  444. struct list_head prepared_discards;
  445. struct bio_list retry_on_resume_list;
  446. struct deferred_set shared_read_ds;
  447. struct deferred_set all_io_ds;
  448. struct dm_thin_new_mapping *next_mapping;
  449. mempool_t *mapping_pool;
  450. mempool_t *endio_hook_pool;
  451. };
  452. /*
  453. * Target context for a pool.
  454. */
  455. struct pool_c {
  456. struct dm_target *ti;
  457. struct pool *pool;
  458. struct dm_dev *data_dev;
  459. struct dm_dev *metadata_dev;
  460. struct dm_target_callbacks callbacks;
  461. dm_block_t low_water_blocks;
  462. struct pool_features pf;
  463. };
  464. /*
  465. * Target context for a thin.
  466. */
  467. struct thin_c {
  468. struct dm_dev *pool_dev;
  469. struct dm_dev *origin_dev;
  470. dm_thin_id dev_id;
  471. struct pool *pool;
  472. struct dm_thin_device *td;
  473. };
  474. /*----------------------------------------------------------------*/
  475. /*
  476. * A global list of pools that uses a struct mapped_device as a key.
  477. */
  478. static struct dm_thin_pool_table {
  479. struct mutex mutex;
  480. struct list_head pools;
  481. } dm_thin_pool_table;
  482. static void pool_table_init(void)
  483. {
  484. mutex_init(&dm_thin_pool_table.mutex);
  485. INIT_LIST_HEAD(&dm_thin_pool_table.pools);
  486. }
  487. static void __pool_table_insert(struct pool *pool)
  488. {
  489. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  490. list_add(&pool->list, &dm_thin_pool_table.pools);
  491. }
  492. static void __pool_table_remove(struct pool *pool)
  493. {
  494. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  495. list_del(&pool->list);
  496. }
  497. static struct pool *__pool_table_lookup(struct mapped_device *md)
  498. {
  499. struct pool *pool = NULL, *tmp;
  500. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  501. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  502. if (tmp->pool_md == md) {
  503. pool = tmp;
  504. break;
  505. }
  506. }
  507. return pool;
  508. }
  509. static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
  510. {
  511. struct pool *pool = NULL, *tmp;
  512. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  513. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  514. if (tmp->md_dev == md_dev) {
  515. pool = tmp;
  516. break;
  517. }
  518. }
  519. return pool;
  520. }
  521. /*----------------------------------------------------------------*/
  522. struct dm_thin_endio_hook {
  523. struct thin_c *tc;
  524. struct deferred_entry *shared_read_entry;
  525. struct deferred_entry *all_io_entry;
  526. struct dm_thin_new_mapping *overwrite_mapping;
  527. };
  528. static void __requeue_bio_list(struct thin_c *tc, struct bio_list *master)
  529. {
  530. struct bio *bio;
  531. struct bio_list bios;
  532. bio_list_init(&bios);
  533. bio_list_merge(&bios, master);
  534. bio_list_init(master);
  535. while ((bio = bio_list_pop(&bios))) {
  536. struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
  537. if (h->tc == tc)
  538. bio_endio(bio, DM_ENDIO_REQUEUE);
  539. else
  540. bio_list_add(master, bio);
  541. }
  542. }
  543. static void requeue_io(struct thin_c *tc)
  544. {
  545. struct pool *pool = tc->pool;
  546. unsigned long flags;
  547. spin_lock_irqsave(&pool->lock, flags);
  548. __requeue_bio_list(tc, &pool->deferred_bios);
  549. __requeue_bio_list(tc, &pool->retry_on_resume_list);
  550. spin_unlock_irqrestore(&pool->lock, flags);
  551. }
  552. /*
  553. * This section of code contains the logic for processing a thin device's IO.
  554. * Much of the code depends on pool object resources (lists, workqueues, etc)
  555. * but most is exclusively called from the thin target rather than the thin-pool
  556. * target.
  557. */
  558. static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
  559. {
  560. return bio->bi_sector >> tc->pool->block_shift;
  561. }
  562. static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
  563. {
  564. struct pool *pool = tc->pool;
  565. bio->bi_bdev = tc->pool_dev->bdev;
  566. bio->bi_sector = (block << pool->block_shift) +
  567. (bio->bi_sector & pool->offset_mask);
  568. }
  569. static void remap_to_origin(struct thin_c *tc, struct bio *bio)
  570. {
  571. bio->bi_bdev = tc->origin_dev->bdev;
  572. }
  573. static void issue(struct thin_c *tc, struct bio *bio)
  574. {
  575. struct pool *pool = tc->pool;
  576. unsigned long flags;
  577. /*
  578. * Batch together any FUA/FLUSH bios we find and then issue
  579. * a single commit for them in process_deferred_bios().
  580. */
  581. if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
  582. spin_lock_irqsave(&pool->lock, flags);
  583. bio_list_add(&pool->deferred_flush_bios, bio);
  584. spin_unlock_irqrestore(&pool->lock, flags);
  585. } else
  586. generic_make_request(bio);
  587. }
  588. static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
  589. {
  590. remap_to_origin(tc, bio);
  591. issue(tc, bio);
  592. }
  593. static void remap_and_issue(struct thin_c *tc, struct bio *bio,
  594. dm_block_t block)
  595. {
  596. remap(tc, bio, block);
  597. issue(tc, bio);
  598. }
  599. /*
  600. * wake_worker() is used when new work is queued and when pool_resume is
  601. * ready to continue deferred IO processing.
  602. */
  603. static void wake_worker(struct pool *pool)
  604. {
  605. queue_work(pool->wq, &pool->worker);
  606. }
  607. /*----------------------------------------------------------------*/
  608. /*
  609. * Bio endio functions.
  610. */
  611. struct dm_thin_new_mapping {
  612. struct list_head list;
  613. unsigned quiesced:1;
  614. unsigned prepared:1;
  615. unsigned pass_discard:1;
  616. struct thin_c *tc;
  617. dm_block_t virt_block;
  618. dm_block_t data_block;
  619. struct dm_bio_prison_cell *cell, *cell2;
  620. int err;
  621. /*
  622. * If the bio covers the whole area of a block then we can avoid
  623. * zeroing or copying. Instead this bio is hooked. The bio will
  624. * still be in the cell, so care has to be taken to avoid issuing
  625. * the bio twice.
  626. */
  627. struct bio *bio;
  628. bio_end_io_t *saved_bi_end_io;
  629. };
  630. static void __maybe_add_mapping(struct dm_thin_new_mapping *m)
  631. {
  632. struct pool *pool = m->tc->pool;
  633. if (m->quiesced && m->prepared) {
  634. list_add(&m->list, &pool->prepared_mappings);
  635. wake_worker(pool);
  636. }
  637. }
  638. static void copy_complete(int read_err, unsigned long write_err, void *context)
  639. {
  640. unsigned long flags;
  641. struct dm_thin_new_mapping *m = context;
  642. struct pool *pool = m->tc->pool;
  643. m->err = read_err || write_err ? -EIO : 0;
  644. spin_lock_irqsave(&pool->lock, flags);
  645. m->prepared = 1;
  646. __maybe_add_mapping(m);
  647. spin_unlock_irqrestore(&pool->lock, flags);
  648. }
  649. static void overwrite_endio(struct bio *bio, int err)
  650. {
  651. unsigned long flags;
  652. struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
  653. struct dm_thin_new_mapping *m = h->overwrite_mapping;
  654. struct pool *pool = m->tc->pool;
  655. m->err = err;
  656. spin_lock_irqsave(&pool->lock, flags);
  657. m->prepared = 1;
  658. __maybe_add_mapping(m);
  659. spin_unlock_irqrestore(&pool->lock, flags);
  660. }
  661. /*----------------------------------------------------------------*/
  662. /*
  663. * Workqueue.
  664. */
  665. /*
  666. * Prepared mapping jobs.
  667. */
  668. /*
  669. * This sends the bios in the cell back to the deferred_bios list.
  670. */
  671. static void cell_defer(struct thin_c *tc, struct dm_bio_prison_cell *cell,
  672. dm_block_t data_block)
  673. {
  674. struct pool *pool = tc->pool;
  675. unsigned long flags;
  676. spin_lock_irqsave(&pool->lock, flags);
  677. cell_release(cell, &pool->deferred_bios);
  678. spin_unlock_irqrestore(&tc->pool->lock, flags);
  679. wake_worker(pool);
  680. }
  681. /*
  682. * Same as cell_defer above, except it omits one particular detainee,
  683. * a write bio that covers the block and has already been processed.
  684. */
  685. static void cell_defer_except(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  686. {
  687. struct bio_list bios;
  688. struct pool *pool = tc->pool;
  689. unsigned long flags;
  690. bio_list_init(&bios);
  691. spin_lock_irqsave(&pool->lock, flags);
  692. cell_release_no_holder(cell, &pool->deferred_bios);
  693. spin_unlock_irqrestore(&pool->lock, flags);
  694. wake_worker(pool);
  695. }
  696. static void process_prepared_mapping(struct dm_thin_new_mapping *m)
  697. {
  698. struct thin_c *tc = m->tc;
  699. struct bio *bio;
  700. int r;
  701. bio = m->bio;
  702. if (bio)
  703. bio->bi_end_io = m->saved_bi_end_io;
  704. if (m->err) {
  705. cell_error(m->cell);
  706. return;
  707. }
  708. /*
  709. * Commit the prepared block into the mapping btree.
  710. * Any I/O for this block arriving after this point will get
  711. * remapped to it directly.
  712. */
  713. r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
  714. if (r) {
  715. DMERR("dm_thin_insert_block() failed");
  716. cell_error(m->cell);
  717. return;
  718. }
  719. /*
  720. * Release any bios held while the block was being provisioned.
  721. * If we are processing a write bio that completely covers the block,
  722. * we already processed it so can ignore it now when processing
  723. * the bios in the cell.
  724. */
  725. if (bio) {
  726. cell_defer_except(tc, m->cell);
  727. bio_endio(bio, 0);
  728. } else
  729. cell_defer(tc, m->cell, m->data_block);
  730. list_del(&m->list);
  731. mempool_free(m, tc->pool->mapping_pool);
  732. }
  733. static void process_prepared_discard(struct dm_thin_new_mapping *m)
  734. {
  735. int r;
  736. struct thin_c *tc = m->tc;
  737. r = dm_thin_remove_block(tc->td, m->virt_block);
  738. if (r)
  739. DMERR("dm_thin_remove_block() failed");
  740. /*
  741. * Pass the discard down to the underlying device?
  742. */
  743. if (m->pass_discard)
  744. remap_and_issue(tc, m->bio, m->data_block);
  745. else
  746. bio_endio(m->bio, 0);
  747. cell_defer_except(tc, m->cell);
  748. cell_defer_except(tc, m->cell2);
  749. mempool_free(m, tc->pool->mapping_pool);
  750. }
  751. static void process_prepared(struct pool *pool, struct list_head *head,
  752. void (*fn)(struct dm_thin_new_mapping *))
  753. {
  754. unsigned long flags;
  755. struct list_head maps;
  756. struct dm_thin_new_mapping *m, *tmp;
  757. INIT_LIST_HEAD(&maps);
  758. spin_lock_irqsave(&pool->lock, flags);
  759. list_splice_init(head, &maps);
  760. spin_unlock_irqrestore(&pool->lock, flags);
  761. list_for_each_entry_safe(m, tmp, &maps, list)
  762. fn(m);
  763. }
  764. /*
  765. * Deferred bio jobs.
  766. */
  767. static int io_overlaps_block(struct pool *pool, struct bio *bio)
  768. {
  769. return !(bio->bi_sector & pool->offset_mask) &&
  770. (bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT));
  771. }
  772. static int io_overwrites_block(struct pool *pool, struct bio *bio)
  773. {
  774. return (bio_data_dir(bio) == WRITE) &&
  775. io_overlaps_block(pool, bio);
  776. }
  777. static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
  778. bio_end_io_t *fn)
  779. {
  780. *save = bio->bi_end_io;
  781. bio->bi_end_io = fn;
  782. }
  783. static int ensure_next_mapping(struct pool *pool)
  784. {
  785. if (pool->next_mapping)
  786. return 0;
  787. pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
  788. return pool->next_mapping ? 0 : -ENOMEM;
  789. }
  790. static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
  791. {
  792. struct dm_thin_new_mapping *r = pool->next_mapping;
  793. BUG_ON(!pool->next_mapping);
  794. pool->next_mapping = NULL;
  795. return r;
  796. }
  797. static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
  798. struct dm_dev *origin, dm_block_t data_origin,
  799. dm_block_t data_dest,
  800. struct dm_bio_prison_cell *cell, struct bio *bio)
  801. {
  802. int r;
  803. struct pool *pool = tc->pool;
  804. struct dm_thin_new_mapping *m = get_next_mapping(pool);
  805. INIT_LIST_HEAD(&m->list);
  806. m->quiesced = 0;
  807. m->prepared = 0;
  808. m->tc = tc;
  809. m->virt_block = virt_block;
  810. m->data_block = data_dest;
  811. m->cell = cell;
  812. m->err = 0;
  813. m->bio = NULL;
  814. if (!ds_add_work(&pool->shared_read_ds, &m->list))
  815. m->quiesced = 1;
  816. /*
  817. * IO to pool_dev remaps to the pool target's data_dev.
  818. *
  819. * If the whole block of data is being overwritten, we can issue the
  820. * bio immediately. Otherwise we use kcopyd to clone the data first.
  821. */
  822. if (io_overwrites_block(pool, bio)) {
  823. struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
  824. h->overwrite_mapping = m;
  825. m->bio = bio;
  826. save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
  827. remap_and_issue(tc, bio, data_dest);
  828. } else {
  829. struct dm_io_region from, to;
  830. from.bdev = origin->bdev;
  831. from.sector = data_origin * pool->sectors_per_block;
  832. from.count = pool->sectors_per_block;
  833. to.bdev = tc->pool_dev->bdev;
  834. to.sector = data_dest * pool->sectors_per_block;
  835. to.count = pool->sectors_per_block;
  836. r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
  837. 0, copy_complete, m);
  838. if (r < 0) {
  839. mempool_free(m, pool->mapping_pool);
  840. DMERR("dm_kcopyd_copy() failed");
  841. cell_error(cell);
  842. }
  843. }
  844. }
  845. static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
  846. dm_block_t data_origin, dm_block_t data_dest,
  847. struct dm_bio_prison_cell *cell, struct bio *bio)
  848. {
  849. schedule_copy(tc, virt_block, tc->pool_dev,
  850. data_origin, data_dest, cell, bio);
  851. }
  852. static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
  853. dm_block_t data_dest,
  854. struct dm_bio_prison_cell *cell, struct bio *bio)
  855. {
  856. schedule_copy(tc, virt_block, tc->origin_dev,
  857. virt_block, data_dest, cell, bio);
  858. }
  859. static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
  860. dm_block_t data_block, struct dm_bio_prison_cell *cell,
  861. struct bio *bio)
  862. {
  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 = 1;
  867. m->prepared = 0;
  868. m->tc = tc;
  869. m->virt_block = virt_block;
  870. m->data_block = data_block;
  871. m->cell = cell;
  872. m->err = 0;
  873. m->bio = NULL;
  874. /*
  875. * If the whole block of data is being overwritten or we are not
  876. * zeroing pre-existing data, we can issue the bio immediately.
  877. * Otherwise we use kcopyd to zero the data first.
  878. */
  879. if (!pool->pf.zero_new_blocks)
  880. process_prepared_mapping(m);
  881. else if (io_overwrites_block(pool, bio)) {
  882. struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
  883. h->overwrite_mapping = m;
  884. m->bio = bio;
  885. save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
  886. remap_and_issue(tc, bio, data_block);
  887. } else {
  888. int r;
  889. struct dm_io_region to;
  890. to.bdev = tc->pool_dev->bdev;
  891. to.sector = data_block * pool->sectors_per_block;
  892. to.count = pool->sectors_per_block;
  893. r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
  894. if (r < 0) {
  895. mempool_free(m, pool->mapping_pool);
  896. DMERR("dm_kcopyd_zero() failed");
  897. cell_error(cell);
  898. }
  899. }
  900. }
  901. static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
  902. {
  903. int r;
  904. dm_block_t free_blocks;
  905. unsigned long flags;
  906. struct pool *pool = tc->pool;
  907. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  908. if (r)
  909. return r;
  910. if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
  911. DMWARN("%s: reached low water mark, sending event.",
  912. dm_device_name(pool->pool_md));
  913. spin_lock_irqsave(&pool->lock, flags);
  914. pool->low_water_triggered = 1;
  915. spin_unlock_irqrestore(&pool->lock, flags);
  916. dm_table_event(pool->ti->table);
  917. }
  918. if (!free_blocks) {
  919. if (pool->no_free_space)
  920. return -ENOSPC;
  921. else {
  922. /*
  923. * Try to commit to see if that will free up some
  924. * more space.
  925. */
  926. r = dm_pool_commit_metadata(pool->pmd);
  927. if (r) {
  928. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  929. __func__, r);
  930. return r;
  931. }
  932. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  933. if (r)
  934. return r;
  935. /*
  936. * If we still have no space we set a flag to avoid
  937. * doing all this checking and return -ENOSPC.
  938. */
  939. if (!free_blocks) {
  940. DMWARN("%s: no free space available.",
  941. dm_device_name(pool->pool_md));
  942. spin_lock_irqsave(&pool->lock, flags);
  943. pool->no_free_space = 1;
  944. spin_unlock_irqrestore(&pool->lock, flags);
  945. return -ENOSPC;
  946. }
  947. }
  948. }
  949. r = dm_pool_alloc_data_block(pool->pmd, result);
  950. if (r)
  951. return r;
  952. return 0;
  953. }
  954. /*
  955. * If we have run out of space, queue bios until the device is
  956. * resumed, presumably after having been reloaded with more space.
  957. */
  958. static void retry_on_resume(struct bio *bio)
  959. {
  960. struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
  961. struct thin_c *tc = h->tc;
  962. struct pool *pool = tc->pool;
  963. unsigned long flags;
  964. spin_lock_irqsave(&pool->lock, flags);
  965. bio_list_add(&pool->retry_on_resume_list, bio);
  966. spin_unlock_irqrestore(&pool->lock, flags);
  967. }
  968. static void no_space(struct dm_bio_prison_cell *cell)
  969. {
  970. struct bio *bio;
  971. struct bio_list bios;
  972. bio_list_init(&bios);
  973. cell_release(cell, &bios);
  974. while ((bio = bio_list_pop(&bios)))
  975. retry_on_resume(bio);
  976. }
  977. static void process_discard(struct thin_c *tc, struct bio *bio)
  978. {
  979. int r;
  980. unsigned long flags;
  981. struct pool *pool = tc->pool;
  982. struct dm_bio_prison_cell *cell, *cell2;
  983. struct cell_key key, key2;
  984. dm_block_t block = get_bio_block(tc, bio);
  985. struct dm_thin_lookup_result lookup_result;
  986. struct dm_thin_new_mapping *m;
  987. build_virtual_key(tc->td, block, &key);
  988. if (bio_detain(tc->pool->prison, &key, bio, &cell))
  989. return;
  990. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  991. switch (r) {
  992. case 0:
  993. /*
  994. * Check nobody is fiddling with this pool block. This can
  995. * happen if someone's in the process of breaking sharing
  996. * on this block.
  997. */
  998. build_data_key(tc->td, lookup_result.block, &key2);
  999. if (bio_detain(tc->pool->prison, &key2, bio, &cell2)) {
  1000. cell_release_singleton(cell, bio);
  1001. break;
  1002. }
  1003. if (io_overlaps_block(pool, bio)) {
  1004. /*
  1005. * IO may still be going to the destination block. We must
  1006. * quiesce before we can do the removal.
  1007. */
  1008. m = get_next_mapping(pool);
  1009. m->tc = tc;
  1010. m->pass_discard = (!lookup_result.shared) & pool->pf.discard_passdown;
  1011. m->virt_block = block;
  1012. m->data_block = lookup_result.block;
  1013. m->cell = cell;
  1014. m->cell2 = cell2;
  1015. m->err = 0;
  1016. m->bio = bio;
  1017. if (!ds_add_work(&pool->all_io_ds, &m->list)) {
  1018. spin_lock_irqsave(&pool->lock, flags);
  1019. list_add(&m->list, &pool->prepared_discards);
  1020. spin_unlock_irqrestore(&pool->lock, flags);
  1021. wake_worker(pool);
  1022. }
  1023. } else {
  1024. /*
  1025. * This path is hit if people are ignoring
  1026. * limits->discard_granularity. It ignores any
  1027. * part of the discard that is in a subsequent
  1028. * block.
  1029. */
  1030. sector_t offset = bio->bi_sector - (block << pool->block_shift);
  1031. unsigned remaining = (pool->sectors_per_block - offset) << 9;
  1032. bio->bi_size = min(bio->bi_size, remaining);
  1033. cell_release_singleton(cell, bio);
  1034. cell_release_singleton(cell2, bio);
  1035. remap_and_issue(tc, bio, lookup_result.block);
  1036. }
  1037. break;
  1038. case -ENODATA:
  1039. /*
  1040. * It isn't provisioned, just forget it.
  1041. */
  1042. cell_release_singleton(cell, bio);
  1043. bio_endio(bio, 0);
  1044. break;
  1045. default:
  1046. DMERR("discard: find block unexpectedly returned %d", r);
  1047. cell_release_singleton(cell, bio);
  1048. bio_io_error(bio);
  1049. break;
  1050. }
  1051. }
  1052. static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
  1053. struct cell_key *key,
  1054. struct dm_thin_lookup_result *lookup_result,
  1055. struct dm_bio_prison_cell *cell)
  1056. {
  1057. int r;
  1058. dm_block_t data_block;
  1059. r = alloc_data_block(tc, &data_block);
  1060. switch (r) {
  1061. case 0:
  1062. schedule_internal_copy(tc, block, lookup_result->block,
  1063. data_block, cell, bio);
  1064. break;
  1065. case -ENOSPC:
  1066. no_space(cell);
  1067. break;
  1068. default:
  1069. DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
  1070. cell_error(cell);
  1071. break;
  1072. }
  1073. }
  1074. static void process_shared_bio(struct thin_c *tc, struct bio *bio,
  1075. dm_block_t block,
  1076. struct dm_thin_lookup_result *lookup_result)
  1077. {
  1078. struct dm_bio_prison_cell *cell;
  1079. struct pool *pool = tc->pool;
  1080. struct cell_key key;
  1081. /*
  1082. * If cell is already occupied, then sharing is already in the process
  1083. * of being broken so we have nothing further to do here.
  1084. */
  1085. build_data_key(tc->td, lookup_result->block, &key);
  1086. if (bio_detain(pool->prison, &key, bio, &cell))
  1087. return;
  1088. if (bio_data_dir(bio) == WRITE)
  1089. break_sharing(tc, bio, block, &key, lookup_result, cell);
  1090. else {
  1091. struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
  1092. h->shared_read_entry = ds_inc(&pool->shared_read_ds);
  1093. cell_release_singleton(cell, bio);
  1094. remap_and_issue(tc, bio, lookup_result->block);
  1095. }
  1096. }
  1097. static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
  1098. struct dm_bio_prison_cell *cell)
  1099. {
  1100. int r;
  1101. dm_block_t data_block;
  1102. /*
  1103. * Remap empty bios (flushes) immediately, without provisioning.
  1104. */
  1105. if (!bio->bi_size) {
  1106. cell_release_singleton(cell, bio);
  1107. remap_and_issue(tc, bio, 0);
  1108. return;
  1109. }
  1110. /*
  1111. * Fill read bios with zeroes and complete them immediately.
  1112. */
  1113. if (bio_data_dir(bio) == READ) {
  1114. zero_fill_bio(bio);
  1115. cell_release_singleton(cell, bio);
  1116. bio_endio(bio, 0);
  1117. return;
  1118. }
  1119. r = alloc_data_block(tc, &data_block);
  1120. switch (r) {
  1121. case 0:
  1122. if (tc->origin_dev)
  1123. schedule_external_copy(tc, block, data_block, cell, bio);
  1124. else
  1125. schedule_zero(tc, block, data_block, cell, bio);
  1126. break;
  1127. case -ENOSPC:
  1128. no_space(cell);
  1129. break;
  1130. default:
  1131. DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
  1132. cell_error(cell);
  1133. break;
  1134. }
  1135. }
  1136. static void process_bio(struct thin_c *tc, struct bio *bio)
  1137. {
  1138. int r;
  1139. dm_block_t block = get_bio_block(tc, bio);
  1140. struct dm_bio_prison_cell *cell;
  1141. struct cell_key key;
  1142. struct dm_thin_lookup_result lookup_result;
  1143. /*
  1144. * If cell is already occupied, then the block is already
  1145. * being provisioned so we have nothing further to do here.
  1146. */
  1147. build_virtual_key(tc->td, block, &key);
  1148. if (bio_detain(tc->pool->prison, &key, bio, &cell))
  1149. return;
  1150. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1151. switch (r) {
  1152. case 0:
  1153. /*
  1154. * We can release this cell now. This thread is the only
  1155. * one that puts bios into a cell, and we know there were
  1156. * no preceding bios.
  1157. */
  1158. /*
  1159. * TODO: this will probably have to change when discard goes
  1160. * back in.
  1161. */
  1162. cell_release_singleton(cell, bio);
  1163. if (lookup_result.shared)
  1164. process_shared_bio(tc, bio, block, &lookup_result);
  1165. else
  1166. remap_and_issue(tc, bio, lookup_result.block);
  1167. break;
  1168. case -ENODATA:
  1169. if (bio_data_dir(bio) == READ && tc->origin_dev) {
  1170. cell_release_singleton(cell, bio);
  1171. remap_to_origin_and_issue(tc, bio);
  1172. } else
  1173. provision_block(tc, bio, block, cell);
  1174. break;
  1175. default:
  1176. DMERR("dm_thin_find_block() failed, error = %d", r);
  1177. cell_release_singleton(cell, bio);
  1178. bio_io_error(bio);
  1179. break;
  1180. }
  1181. }
  1182. static int need_commit_due_to_time(struct pool *pool)
  1183. {
  1184. return jiffies < pool->last_commit_jiffies ||
  1185. jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
  1186. }
  1187. static void process_deferred_bios(struct pool *pool)
  1188. {
  1189. unsigned long flags;
  1190. struct bio *bio;
  1191. struct bio_list bios;
  1192. int r;
  1193. bio_list_init(&bios);
  1194. spin_lock_irqsave(&pool->lock, flags);
  1195. bio_list_merge(&bios, &pool->deferred_bios);
  1196. bio_list_init(&pool->deferred_bios);
  1197. spin_unlock_irqrestore(&pool->lock, flags);
  1198. while ((bio = bio_list_pop(&bios))) {
  1199. struct dm_thin_endio_hook *h = dm_get_mapinfo(bio)->ptr;
  1200. struct thin_c *tc = h->tc;
  1201. /*
  1202. * If we've got no free new_mapping structs, and processing
  1203. * this bio might require one, we pause until there are some
  1204. * prepared mappings to process.
  1205. */
  1206. if (ensure_next_mapping(pool)) {
  1207. spin_lock_irqsave(&pool->lock, flags);
  1208. bio_list_merge(&pool->deferred_bios, &bios);
  1209. spin_unlock_irqrestore(&pool->lock, flags);
  1210. break;
  1211. }
  1212. if (bio->bi_rw & REQ_DISCARD)
  1213. process_discard(tc, bio);
  1214. else
  1215. process_bio(tc, bio);
  1216. }
  1217. /*
  1218. * If there are any deferred flush bios, we must commit
  1219. * the metadata before issuing them.
  1220. */
  1221. bio_list_init(&bios);
  1222. spin_lock_irqsave(&pool->lock, flags);
  1223. bio_list_merge(&bios, &pool->deferred_flush_bios);
  1224. bio_list_init(&pool->deferred_flush_bios);
  1225. spin_unlock_irqrestore(&pool->lock, flags);
  1226. if (bio_list_empty(&bios) && !need_commit_due_to_time(pool))
  1227. return;
  1228. r = dm_pool_commit_metadata(pool->pmd);
  1229. if (r) {
  1230. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  1231. __func__, r);
  1232. while ((bio = bio_list_pop(&bios)))
  1233. bio_io_error(bio);
  1234. return;
  1235. }
  1236. pool->last_commit_jiffies = jiffies;
  1237. while ((bio = bio_list_pop(&bios)))
  1238. generic_make_request(bio);
  1239. }
  1240. static void do_worker(struct work_struct *ws)
  1241. {
  1242. struct pool *pool = container_of(ws, struct pool, worker);
  1243. process_prepared(pool, &pool->prepared_mappings, process_prepared_mapping);
  1244. process_prepared(pool, &pool->prepared_discards, process_prepared_discard);
  1245. process_deferred_bios(pool);
  1246. }
  1247. /*
  1248. * We want to commit periodically so that not too much
  1249. * unwritten data builds up.
  1250. */
  1251. static void do_waker(struct work_struct *ws)
  1252. {
  1253. struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
  1254. wake_worker(pool);
  1255. queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
  1256. }
  1257. /*----------------------------------------------------------------*/
  1258. /*
  1259. * Mapping functions.
  1260. */
  1261. /*
  1262. * Called only while mapping a thin bio to hand it over to the workqueue.
  1263. */
  1264. static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
  1265. {
  1266. unsigned long flags;
  1267. struct pool *pool = tc->pool;
  1268. spin_lock_irqsave(&pool->lock, flags);
  1269. bio_list_add(&pool->deferred_bios, bio);
  1270. spin_unlock_irqrestore(&pool->lock, flags);
  1271. wake_worker(pool);
  1272. }
  1273. static struct dm_thin_endio_hook *thin_hook_bio(struct thin_c *tc, struct bio *bio)
  1274. {
  1275. struct pool *pool = tc->pool;
  1276. struct dm_thin_endio_hook *h = mempool_alloc(pool->endio_hook_pool, GFP_NOIO);
  1277. h->tc = tc;
  1278. h->shared_read_entry = NULL;
  1279. h->all_io_entry = bio->bi_rw & REQ_DISCARD ? NULL : ds_inc(&pool->all_io_ds);
  1280. h->overwrite_mapping = NULL;
  1281. return h;
  1282. }
  1283. /*
  1284. * Non-blocking function called from the thin target's map function.
  1285. */
  1286. static int thin_bio_map(struct dm_target *ti, struct bio *bio,
  1287. union map_info *map_context)
  1288. {
  1289. int r;
  1290. struct thin_c *tc = ti->private;
  1291. dm_block_t block = get_bio_block(tc, bio);
  1292. struct dm_thin_device *td = tc->td;
  1293. struct dm_thin_lookup_result result;
  1294. map_context->ptr = thin_hook_bio(tc, bio);
  1295. if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
  1296. thin_defer_bio(tc, bio);
  1297. return DM_MAPIO_SUBMITTED;
  1298. }
  1299. r = dm_thin_find_block(td, block, 0, &result);
  1300. /*
  1301. * Note that we defer readahead too.
  1302. */
  1303. switch (r) {
  1304. case 0:
  1305. if (unlikely(result.shared)) {
  1306. /*
  1307. * We have a race condition here between the
  1308. * result.shared value returned by the lookup and
  1309. * snapshot creation, which may cause new
  1310. * sharing.
  1311. *
  1312. * To avoid this always quiesce the origin before
  1313. * taking the snap. You want to do this anyway to
  1314. * ensure a consistent application view
  1315. * (i.e. lockfs).
  1316. *
  1317. * More distant ancestors are irrelevant. The
  1318. * shared flag will be set in their case.
  1319. */
  1320. thin_defer_bio(tc, bio);
  1321. r = DM_MAPIO_SUBMITTED;
  1322. } else {
  1323. remap(tc, bio, result.block);
  1324. r = DM_MAPIO_REMAPPED;
  1325. }
  1326. break;
  1327. case -ENODATA:
  1328. /*
  1329. * In future, the failed dm_thin_find_block above could
  1330. * provide the hint to load the metadata into cache.
  1331. */
  1332. case -EWOULDBLOCK:
  1333. thin_defer_bio(tc, bio);
  1334. r = DM_MAPIO_SUBMITTED;
  1335. break;
  1336. }
  1337. return r;
  1338. }
  1339. static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1340. {
  1341. int r;
  1342. unsigned long flags;
  1343. struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
  1344. spin_lock_irqsave(&pt->pool->lock, flags);
  1345. r = !bio_list_empty(&pt->pool->retry_on_resume_list);
  1346. spin_unlock_irqrestore(&pt->pool->lock, flags);
  1347. if (!r) {
  1348. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  1349. r = bdi_congested(&q->backing_dev_info, bdi_bits);
  1350. }
  1351. return r;
  1352. }
  1353. static void __requeue_bios(struct pool *pool)
  1354. {
  1355. bio_list_merge(&pool->deferred_bios, &pool->retry_on_resume_list);
  1356. bio_list_init(&pool->retry_on_resume_list);
  1357. }
  1358. /*----------------------------------------------------------------
  1359. * Binding of control targets to a pool object
  1360. *--------------------------------------------------------------*/
  1361. static int bind_control_target(struct pool *pool, struct dm_target *ti)
  1362. {
  1363. struct pool_c *pt = ti->private;
  1364. pool->ti = ti;
  1365. pool->low_water_blocks = pt->low_water_blocks;
  1366. pool->pf = pt->pf;
  1367. /*
  1368. * If discard_passdown was enabled verify that the data device
  1369. * supports discards. Disable discard_passdown if not; otherwise
  1370. * -EOPNOTSUPP will be returned.
  1371. */
  1372. if (pt->pf.discard_passdown) {
  1373. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  1374. if (!q || !blk_queue_discard(q)) {
  1375. char buf[BDEVNAME_SIZE];
  1376. DMWARN("Discard unsupported by data device (%s): Disabling discard passdown.",
  1377. bdevname(pt->data_dev->bdev, buf));
  1378. pool->pf.discard_passdown = 0;
  1379. }
  1380. }
  1381. return 0;
  1382. }
  1383. static void unbind_control_target(struct pool *pool, struct dm_target *ti)
  1384. {
  1385. if (pool->ti == ti)
  1386. pool->ti = NULL;
  1387. }
  1388. /*----------------------------------------------------------------
  1389. * Pool creation
  1390. *--------------------------------------------------------------*/
  1391. /* Initialize pool features. */
  1392. static void pool_features_init(struct pool_features *pf)
  1393. {
  1394. pf->zero_new_blocks = 1;
  1395. pf->discard_enabled = 1;
  1396. pf->discard_passdown = 1;
  1397. }
  1398. static void __pool_destroy(struct pool *pool)
  1399. {
  1400. __pool_table_remove(pool);
  1401. if (dm_pool_metadata_close(pool->pmd) < 0)
  1402. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  1403. prison_destroy(pool->prison);
  1404. dm_kcopyd_client_destroy(pool->copier);
  1405. if (pool->wq)
  1406. destroy_workqueue(pool->wq);
  1407. if (pool->next_mapping)
  1408. mempool_free(pool->next_mapping, pool->mapping_pool);
  1409. mempool_destroy(pool->mapping_pool);
  1410. mempool_destroy(pool->endio_hook_pool);
  1411. kfree(pool);
  1412. }
  1413. static struct kmem_cache *_new_mapping_cache;
  1414. static struct kmem_cache *_endio_hook_cache;
  1415. static struct pool *pool_create(struct mapped_device *pool_md,
  1416. struct block_device *metadata_dev,
  1417. unsigned long block_size, char **error)
  1418. {
  1419. int r;
  1420. void *err_p;
  1421. struct pool *pool;
  1422. struct dm_pool_metadata *pmd;
  1423. pmd = dm_pool_metadata_open(metadata_dev, block_size);
  1424. if (IS_ERR(pmd)) {
  1425. *error = "Error creating metadata object";
  1426. return (struct pool *)pmd;
  1427. }
  1428. pool = kmalloc(sizeof(*pool), GFP_KERNEL);
  1429. if (!pool) {
  1430. *error = "Error allocating memory for pool";
  1431. err_p = ERR_PTR(-ENOMEM);
  1432. goto bad_pool;
  1433. }
  1434. pool->pmd = pmd;
  1435. pool->sectors_per_block = block_size;
  1436. pool->block_shift = ffs(block_size) - 1;
  1437. pool->offset_mask = block_size - 1;
  1438. pool->low_water_blocks = 0;
  1439. pool_features_init(&pool->pf);
  1440. pool->prison = prison_create(PRISON_CELLS);
  1441. if (!pool->prison) {
  1442. *error = "Error creating pool's bio prison";
  1443. err_p = ERR_PTR(-ENOMEM);
  1444. goto bad_prison;
  1445. }
  1446. pool->copier = dm_kcopyd_client_create();
  1447. if (IS_ERR(pool->copier)) {
  1448. r = PTR_ERR(pool->copier);
  1449. *error = "Error creating pool's kcopyd client";
  1450. err_p = ERR_PTR(r);
  1451. goto bad_kcopyd_client;
  1452. }
  1453. /*
  1454. * Create singlethreaded workqueue that will service all devices
  1455. * that use this metadata.
  1456. */
  1457. pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  1458. if (!pool->wq) {
  1459. *error = "Error creating pool's workqueue";
  1460. err_p = ERR_PTR(-ENOMEM);
  1461. goto bad_wq;
  1462. }
  1463. INIT_WORK(&pool->worker, do_worker);
  1464. INIT_DELAYED_WORK(&pool->waker, do_waker);
  1465. spin_lock_init(&pool->lock);
  1466. bio_list_init(&pool->deferred_bios);
  1467. bio_list_init(&pool->deferred_flush_bios);
  1468. INIT_LIST_HEAD(&pool->prepared_mappings);
  1469. INIT_LIST_HEAD(&pool->prepared_discards);
  1470. pool->low_water_triggered = 0;
  1471. pool->no_free_space = 0;
  1472. bio_list_init(&pool->retry_on_resume_list);
  1473. ds_init(&pool->shared_read_ds);
  1474. ds_init(&pool->all_io_ds);
  1475. pool->next_mapping = NULL;
  1476. pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
  1477. _new_mapping_cache);
  1478. if (!pool->mapping_pool) {
  1479. *error = "Error creating pool's mapping mempool";
  1480. err_p = ERR_PTR(-ENOMEM);
  1481. goto bad_mapping_pool;
  1482. }
  1483. pool->endio_hook_pool = mempool_create_slab_pool(ENDIO_HOOK_POOL_SIZE,
  1484. _endio_hook_cache);
  1485. if (!pool->endio_hook_pool) {
  1486. *error = "Error creating pool's endio_hook mempool";
  1487. err_p = ERR_PTR(-ENOMEM);
  1488. goto bad_endio_hook_pool;
  1489. }
  1490. pool->ref_count = 1;
  1491. pool->last_commit_jiffies = jiffies;
  1492. pool->pool_md = pool_md;
  1493. pool->md_dev = metadata_dev;
  1494. __pool_table_insert(pool);
  1495. return pool;
  1496. bad_endio_hook_pool:
  1497. mempool_destroy(pool->mapping_pool);
  1498. bad_mapping_pool:
  1499. destroy_workqueue(pool->wq);
  1500. bad_wq:
  1501. dm_kcopyd_client_destroy(pool->copier);
  1502. bad_kcopyd_client:
  1503. prison_destroy(pool->prison);
  1504. bad_prison:
  1505. kfree(pool);
  1506. bad_pool:
  1507. if (dm_pool_metadata_close(pmd))
  1508. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  1509. return err_p;
  1510. }
  1511. static void __pool_inc(struct pool *pool)
  1512. {
  1513. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  1514. pool->ref_count++;
  1515. }
  1516. static void __pool_dec(struct pool *pool)
  1517. {
  1518. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  1519. BUG_ON(!pool->ref_count);
  1520. if (!--pool->ref_count)
  1521. __pool_destroy(pool);
  1522. }
  1523. static struct pool *__pool_find(struct mapped_device *pool_md,
  1524. struct block_device *metadata_dev,
  1525. unsigned long block_size, char **error,
  1526. int *created)
  1527. {
  1528. struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
  1529. if (pool) {
  1530. if (pool->pool_md != pool_md)
  1531. return ERR_PTR(-EBUSY);
  1532. __pool_inc(pool);
  1533. } else {
  1534. pool = __pool_table_lookup(pool_md);
  1535. if (pool) {
  1536. if (pool->md_dev != metadata_dev)
  1537. return ERR_PTR(-EINVAL);
  1538. __pool_inc(pool);
  1539. } else {
  1540. pool = pool_create(pool_md, metadata_dev, block_size, error);
  1541. *created = 1;
  1542. }
  1543. }
  1544. return pool;
  1545. }
  1546. /*----------------------------------------------------------------
  1547. * Pool target methods
  1548. *--------------------------------------------------------------*/
  1549. static void pool_dtr(struct dm_target *ti)
  1550. {
  1551. struct pool_c *pt = ti->private;
  1552. mutex_lock(&dm_thin_pool_table.mutex);
  1553. unbind_control_target(pt->pool, ti);
  1554. __pool_dec(pt->pool);
  1555. dm_put_device(ti, pt->metadata_dev);
  1556. dm_put_device(ti, pt->data_dev);
  1557. kfree(pt);
  1558. mutex_unlock(&dm_thin_pool_table.mutex);
  1559. }
  1560. static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
  1561. struct dm_target *ti)
  1562. {
  1563. int r;
  1564. unsigned argc;
  1565. const char *arg_name;
  1566. static struct dm_arg _args[] = {
  1567. {0, 3, "Invalid number of pool feature arguments"},
  1568. };
  1569. /*
  1570. * No feature arguments supplied.
  1571. */
  1572. if (!as->argc)
  1573. return 0;
  1574. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  1575. if (r)
  1576. return -EINVAL;
  1577. while (argc && !r) {
  1578. arg_name = dm_shift_arg(as);
  1579. argc--;
  1580. if (!strcasecmp(arg_name, "skip_block_zeroing")) {
  1581. pf->zero_new_blocks = 0;
  1582. continue;
  1583. } else if (!strcasecmp(arg_name, "ignore_discard")) {
  1584. pf->discard_enabled = 0;
  1585. continue;
  1586. } else if (!strcasecmp(arg_name, "no_discard_passdown")) {
  1587. pf->discard_passdown = 0;
  1588. continue;
  1589. }
  1590. ti->error = "Unrecognised pool feature requested";
  1591. r = -EINVAL;
  1592. }
  1593. return r;
  1594. }
  1595. /*
  1596. * thin-pool <metadata dev> <data dev>
  1597. * <data block size (sectors)>
  1598. * <low water mark (blocks)>
  1599. * [<#feature args> [<arg>]*]
  1600. *
  1601. * Optional feature arguments are:
  1602. * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
  1603. * ignore_discard: disable discard
  1604. * no_discard_passdown: don't pass discards down to the data device
  1605. */
  1606. static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
  1607. {
  1608. int r, pool_created = 0;
  1609. struct pool_c *pt;
  1610. struct pool *pool;
  1611. struct pool_features pf;
  1612. struct dm_arg_set as;
  1613. struct dm_dev *data_dev;
  1614. unsigned long block_size;
  1615. dm_block_t low_water_blocks;
  1616. struct dm_dev *metadata_dev;
  1617. sector_t metadata_dev_size;
  1618. char b[BDEVNAME_SIZE];
  1619. /*
  1620. * FIXME Remove validation from scope of lock.
  1621. */
  1622. mutex_lock(&dm_thin_pool_table.mutex);
  1623. if (argc < 4) {
  1624. ti->error = "Invalid argument count";
  1625. r = -EINVAL;
  1626. goto out_unlock;
  1627. }
  1628. as.argc = argc;
  1629. as.argv = argv;
  1630. r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &metadata_dev);
  1631. if (r) {
  1632. ti->error = "Error opening metadata block device";
  1633. goto out_unlock;
  1634. }
  1635. metadata_dev_size = i_size_read(metadata_dev->bdev->bd_inode) >> SECTOR_SHIFT;
  1636. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
  1637. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  1638. bdevname(metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
  1639. r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
  1640. if (r) {
  1641. ti->error = "Error getting data device";
  1642. goto out_metadata;
  1643. }
  1644. if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
  1645. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  1646. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  1647. !is_power_of_2(block_size)) {
  1648. ti->error = "Invalid block size";
  1649. r = -EINVAL;
  1650. goto out;
  1651. }
  1652. if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
  1653. ti->error = "Invalid low water mark";
  1654. r = -EINVAL;
  1655. goto out;
  1656. }
  1657. /*
  1658. * Set default pool features.
  1659. */
  1660. pool_features_init(&pf);
  1661. dm_consume_args(&as, 4);
  1662. r = parse_pool_features(&as, &pf, ti);
  1663. if (r)
  1664. goto out;
  1665. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  1666. if (!pt) {
  1667. r = -ENOMEM;
  1668. goto out;
  1669. }
  1670. pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
  1671. block_size, &ti->error, &pool_created);
  1672. if (IS_ERR(pool)) {
  1673. r = PTR_ERR(pool);
  1674. goto out_free_pt;
  1675. }
  1676. /*
  1677. * 'pool_created' reflects whether this is the first table load.
  1678. * Top level discard support is not allowed to be changed after
  1679. * initial load. This would require a pool reload to trigger thin
  1680. * device changes.
  1681. */
  1682. if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
  1683. ti->error = "Discard support cannot be disabled once enabled";
  1684. r = -EINVAL;
  1685. goto out_flags_changed;
  1686. }
  1687. pt->pool = pool;
  1688. pt->ti = ti;
  1689. pt->metadata_dev = metadata_dev;
  1690. pt->data_dev = data_dev;
  1691. pt->low_water_blocks = low_water_blocks;
  1692. pt->pf = pf;
  1693. ti->num_flush_requests = 1;
  1694. /*
  1695. * Only need to enable discards if the pool should pass
  1696. * them down to the data device. The thin device's discard
  1697. * processing will cause mappings to be removed from the btree.
  1698. */
  1699. if (pf.discard_enabled && pf.discard_passdown) {
  1700. ti->num_discard_requests = 1;
  1701. /*
  1702. * Setting 'discards_supported' circumvents the normal
  1703. * stacking of discard limits (this keeps the pool and
  1704. * thin devices' discard limits consistent).
  1705. */
  1706. ti->discards_supported = 1;
  1707. }
  1708. ti->private = pt;
  1709. pt->callbacks.congested_fn = pool_is_congested;
  1710. dm_table_add_target_callbacks(ti->table, &pt->callbacks);
  1711. mutex_unlock(&dm_thin_pool_table.mutex);
  1712. return 0;
  1713. out_flags_changed:
  1714. __pool_dec(pool);
  1715. out_free_pt:
  1716. kfree(pt);
  1717. out:
  1718. dm_put_device(ti, data_dev);
  1719. out_metadata:
  1720. dm_put_device(ti, metadata_dev);
  1721. out_unlock:
  1722. mutex_unlock(&dm_thin_pool_table.mutex);
  1723. return r;
  1724. }
  1725. static int pool_map(struct dm_target *ti, struct bio *bio,
  1726. union map_info *map_context)
  1727. {
  1728. int r;
  1729. struct pool_c *pt = ti->private;
  1730. struct pool *pool = pt->pool;
  1731. unsigned long flags;
  1732. /*
  1733. * As this is a singleton target, ti->begin is always zero.
  1734. */
  1735. spin_lock_irqsave(&pool->lock, flags);
  1736. bio->bi_bdev = pt->data_dev->bdev;
  1737. r = DM_MAPIO_REMAPPED;
  1738. spin_unlock_irqrestore(&pool->lock, flags);
  1739. return r;
  1740. }
  1741. /*
  1742. * Retrieves the number of blocks of the data device from
  1743. * the superblock and compares it to the actual device size,
  1744. * thus resizing the data device in case it has grown.
  1745. *
  1746. * This both copes with opening preallocated data devices in the ctr
  1747. * being followed by a resume
  1748. * -and-
  1749. * calling the resume method individually after userspace has
  1750. * grown the data device in reaction to a table event.
  1751. */
  1752. static int pool_preresume(struct dm_target *ti)
  1753. {
  1754. int r;
  1755. struct pool_c *pt = ti->private;
  1756. struct pool *pool = pt->pool;
  1757. dm_block_t data_size, sb_data_size;
  1758. /*
  1759. * Take control of the pool object.
  1760. */
  1761. r = bind_control_target(pool, ti);
  1762. if (r)
  1763. return r;
  1764. data_size = ti->len >> pool->block_shift;
  1765. r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
  1766. if (r) {
  1767. DMERR("failed to retrieve data device size");
  1768. return r;
  1769. }
  1770. if (data_size < sb_data_size) {
  1771. DMERR("pool target too small, is %llu blocks (expected %llu)",
  1772. data_size, sb_data_size);
  1773. return -EINVAL;
  1774. } else if (data_size > sb_data_size) {
  1775. r = dm_pool_resize_data_dev(pool->pmd, data_size);
  1776. if (r) {
  1777. DMERR("failed to resize data device");
  1778. return r;
  1779. }
  1780. r = dm_pool_commit_metadata(pool->pmd);
  1781. if (r) {
  1782. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  1783. __func__, r);
  1784. return r;
  1785. }
  1786. }
  1787. return 0;
  1788. }
  1789. static void pool_resume(struct dm_target *ti)
  1790. {
  1791. struct pool_c *pt = ti->private;
  1792. struct pool *pool = pt->pool;
  1793. unsigned long flags;
  1794. spin_lock_irqsave(&pool->lock, flags);
  1795. pool->low_water_triggered = 0;
  1796. pool->no_free_space = 0;
  1797. __requeue_bios(pool);
  1798. spin_unlock_irqrestore(&pool->lock, flags);
  1799. do_waker(&pool->waker.work);
  1800. }
  1801. static void pool_postsuspend(struct dm_target *ti)
  1802. {
  1803. int r;
  1804. struct pool_c *pt = ti->private;
  1805. struct pool *pool = pt->pool;
  1806. cancel_delayed_work(&pool->waker);
  1807. flush_workqueue(pool->wq);
  1808. r = dm_pool_commit_metadata(pool->pmd);
  1809. if (r < 0) {
  1810. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  1811. __func__, r);
  1812. /* FIXME: invalidate device? error the next FUA or FLUSH bio ?*/
  1813. }
  1814. }
  1815. static int check_arg_count(unsigned argc, unsigned args_required)
  1816. {
  1817. if (argc != args_required) {
  1818. DMWARN("Message received with %u arguments instead of %u.",
  1819. argc, args_required);
  1820. return -EINVAL;
  1821. }
  1822. return 0;
  1823. }
  1824. static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
  1825. {
  1826. if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
  1827. *dev_id <= MAX_DEV_ID)
  1828. return 0;
  1829. if (warning)
  1830. DMWARN("Message received with invalid device id: %s", arg);
  1831. return -EINVAL;
  1832. }
  1833. static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
  1834. {
  1835. dm_thin_id dev_id;
  1836. int r;
  1837. r = check_arg_count(argc, 2);
  1838. if (r)
  1839. return r;
  1840. r = read_dev_id(argv[1], &dev_id, 1);
  1841. if (r)
  1842. return r;
  1843. r = dm_pool_create_thin(pool->pmd, dev_id);
  1844. if (r) {
  1845. DMWARN("Creation of new thinly-provisioned device with id %s failed.",
  1846. argv[1]);
  1847. return r;
  1848. }
  1849. return 0;
  1850. }
  1851. static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  1852. {
  1853. dm_thin_id dev_id;
  1854. dm_thin_id origin_dev_id;
  1855. int r;
  1856. r = check_arg_count(argc, 3);
  1857. if (r)
  1858. return r;
  1859. r = read_dev_id(argv[1], &dev_id, 1);
  1860. if (r)
  1861. return r;
  1862. r = read_dev_id(argv[2], &origin_dev_id, 1);
  1863. if (r)
  1864. return r;
  1865. r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
  1866. if (r) {
  1867. DMWARN("Creation of new snapshot %s of device %s failed.",
  1868. argv[1], argv[2]);
  1869. return r;
  1870. }
  1871. return 0;
  1872. }
  1873. static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
  1874. {
  1875. dm_thin_id dev_id;
  1876. int r;
  1877. r = check_arg_count(argc, 2);
  1878. if (r)
  1879. return r;
  1880. r = read_dev_id(argv[1], &dev_id, 1);
  1881. if (r)
  1882. return r;
  1883. r = dm_pool_delete_thin_device(pool->pmd, dev_id);
  1884. if (r)
  1885. DMWARN("Deletion of thin device %s failed.", argv[1]);
  1886. return r;
  1887. }
  1888. static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
  1889. {
  1890. dm_thin_id old_id, new_id;
  1891. int r;
  1892. r = check_arg_count(argc, 3);
  1893. if (r)
  1894. return r;
  1895. if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
  1896. DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
  1897. return -EINVAL;
  1898. }
  1899. if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
  1900. DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
  1901. return -EINVAL;
  1902. }
  1903. r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
  1904. if (r) {
  1905. DMWARN("Failed to change transaction id from %s to %s.",
  1906. argv[1], argv[2]);
  1907. return r;
  1908. }
  1909. return 0;
  1910. }
  1911. static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  1912. {
  1913. int r;
  1914. r = check_arg_count(argc, 1);
  1915. if (r)
  1916. return r;
  1917. r = dm_pool_commit_metadata(pool->pmd);
  1918. if (r) {
  1919. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  1920. __func__, r);
  1921. return r;
  1922. }
  1923. r = dm_pool_reserve_metadata_snap(pool->pmd);
  1924. if (r)
  1925. DMWARN("reserve_metadata_snap message failed.");
  1926. return r;
  1927. }
  1928. static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  1929. {
  1930. int r;
  1931. r = check_arg_count(argc, 1);
  1932. if (r)
  1933. return r;
  1934. r = dm_pool_release_metadata_snap(pool->pmd);
  1935. if (r)
  1936. DMWARN("release_metadata_snap message failed.");
  1937. return r;
  1938. }
  1939. /*
  1940. * Messages supported:
  1941. * create_thin <dev_id>
  1942. * create_snap <dev_id> <origin_id>
  1943. * delete <dev_id>
  1944. * trim <dev_id> <new_size_in_sectors>
  1945. * set_transaction_id <current_trans_id> <new_trans_id>
  1946. * reserve_metadata_snap
  1947. * release_metadata_snap
  1948. */
  1949. static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
  1950. {
  1951. int r = -EINVAL;
  1952. struct pool_c *pt = ti->private;
  1953. struct pool *pool = pt->pool;
  1954. if (!strcasecmp(argv[0], "create_thin"))
  1955. r = process_create_thin_mesg(argc, argv, pool);
  1956. else if (!strcasecmp(argv[0], "create_snap"))
  1957. r = process_create_snap_mesg(argc, argv, pool);
  1958. else if (!strcasecmp(argv[0], "delete"))
  1959. r = process_delete_mesg(argc, argv, pool);
  1960. else if (!strcasecmp(argv[0], "set_transaction_id"))
  1961. r = process_set_transaction_id_mesg(argc, argv, pool);
  1962. else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
  1963. r = process_reserve_metadata_snap_mesg(argc, argv, pool);
  1964. else if (!strcasecmp(argv[0], "release_metadata_snap"))
  1965. r = process_release_metadata_snap_mesg(argc, argv, pool);
  1966. else
  1967. DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
  1968. if (!r) {
  1969. r = dm_pool_commit_metadata(pool->pmd);
  1970. if (r)
  1971. DMERR("%s message: dm_pool_commit_metadata() failed, error = %d",
  1972. argv[0], r);
  1973. }
  1974. return r;
  1975. }
  1976. /*
  1977. * Status line is:
  1978. * <transaction id> <used metadata sectors>/<total metadata sectors>
  1979. * <used data sectors>/<total data sectors> <held metadata root>
  1980. */
  1981. static int pool_status(struct dm_target *ti, status_type_t type,
  1982. char *result, unsigned maxlen)
  1983. {
  1984. int r, count;
  1985. unsigned sz = 0;
  1986. uint64_t transaction_id;
  1987. dm_block_t nr_free_blocks_data;
  1988. dm_block_t nr_free_blocks_metadata;
  1989. dm_block_t nr_blocks_data;
  1990. dm_block_t nr_blocks_metadata;
  1991. dm_block_t held_root;
  1992. char buf[BDEVNAME_SIZE];
  1993. char buf2[BDEVNAME_SIZE];
  1994. struct pool_c *pt = ti->private;
  1995. struct pool *pool = pt->pool;
  1996. switch (type) {
  1997. case STATUSTYPE_INFO:
  1998. r = dm_pool_get_metadata_transaction_id(pool->pmd,
  1999. &transaction_id);
  2000. if (r)
  2001. return r;
  2002. r = dm_pool_get_free_metadata_block_count(pool->pmd,
  2003. &nr_free_blocks_metadata);
  2004. if (r)
  2005. return r;
  2006. r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
  2007. if (r)
  2008. return r;
  2009. r = dm_pool_get_free_block_count(pool->pmd,
  2010. &nr_free_blocks_data);
  2011. if (r)
  2012. return r;
  2013. r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
  2014. if (r)
  2015. return r;
  2016. r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
  2017. if (r)
  2018. return r;
  2019. DMEMIT("%llu %llu/%llu %llu/%llu ",
  2020. (unsigned long long)transaction_id,
  2021. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  2022. (unsigned long long)nr_blocks_metadata,
  2023. (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
  2024. (unsigned long long)nr_blocks_data);
  2025. if (held_root)
  2026. DMEMIT("%llu", held_root);
  2027. else
  2028. DMEMIT("-");
  2029. break;
  2030. case STATUSTYPE_TABLE:
  2031. DMEMIT("%s %s %lu %llu ",
  2032. format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
  2033. format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
  2034. (unsigned long)pool->sectors_per_block,
  2035. (unsigned long long)pt->low_water_blocks);
  2036. count = !pool->pf.zero_new_blocks + !pool->pf.discard_enabled +
  2037. !pt->pf.discard_passdown;
  2038. DMEMIT("%u ", count);
  2039. if (!pool->pf.zero_new_blocks)
  2040. DMEMIT("skip_block_zeroing ");
  2041. if (!pool->pf.discard_enabled)
  2042. DMEMIT("ignore_discard ");
  2043. if (!pt->pf.discard_passdown)
  2044. DMEMIT("no_discard_passdown ");
  2045. break;
  2046. }
  2047. return 0;
  2048. }
  2049. static int pool_iterate_devices(struct dm_target *ti,
  2050. iterate_devices_callout_fn fn, void *data)
  2051. {
  2052. struct pool_c *pt = ti->private;
  2053. return fn(ti, pt->data_dev, 0, ti->len, data);
  2054. }
  2055. static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  2056. struct bio_vec *biovec, int max_size)
  2057. {
  2058. struct pool_c *pt = ti->private;
  2059. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  2060. if (!q->merge_bvec_fn)
  2061. return max_size;
  2062. bvm->bi_bdev = pt->data_dev->bdev;
  2063. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  2064. }
  2065. static void set_discard_limits(struct pool *pool, struct queue_limits *limits)
  2066. {
  2067. /*
  2068. * FIXME: these limits may be incompatible with the pool's data device
  2069. */
  2070. limits->max_discard_sectors = pool->sectors_per_block;
  2071. /*
  2072. * This is just a hint, and not enforced. We have to cope with
  2073. * bios that overlap 2 blocks.
  2074. */
  2075. limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
  2076. limits->discard_zeroes_data = pool->pf.zero_new_blocks;
  2077. }
  2078. static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2079. {
  2080. struct pool_c *pt = ti->private;
  2081. struct pool *pool = pt->pool;
  2082. blk_limits_io_min(limits, 0);
  2083. blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
  2084. if (pool->pf.discard_enabled)
  2085. set_discard_limits(pool, limits);
  2086. }
  2087. static struct target_type pool_target = {
  2088. .name = "thin-pool",
  2089. .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
  2090. DM_TARGET_IMMUTABLE,
  2091. .version = {1, 2, 0},
  2092. .module = THIS_MODULE,
  2093. .ctr = pool_ctr,
  2094. .dtr = pool_dtr,
  2095. .map = pool_map,
  2096. .postsuspend = pool_postsuspend,
  2097. .preresume = pool_preresume,
  2098. .resume = pool_resume,
  2099. .message = pool_message,
  2100. .status = pool_status,
  2101. .merge = pool_merge,
  2102. .iterate_devices = pool_iterate_devices,
  2103. .io_hints = pool_io_hints,
  2104. };
  2105. /*----------------------------------------------------------------
  2106. * Thin target methods
  2107. *--------------------------------------------------------------*/
  2108. static void thin_dtr(struct dm_target *ti)
  2109. {
  2110. struct thin_c *tc = ti->private;
  2111. mutex_lock(&dm_thin_pool_table.mutex);
  2112. __pool_dec(tc->pool);
  2113. dm_pool_close_thin_device(tc->td);
  2114. dm_put_device(ti, tc->pool_dev);
  2115. if (tc->origin_dev)
  2116. dm_put_device(ti, tc->origin_dev);
  2117. kfree(tc);
  2118. mutex_unlock(&dm_thin_pool_table.mutex);
  2119. }
  2120. /*
  2121. * Thin target parameters:
  2122. *
  2123. * <pool_dev> <dev_id> [origin_dev]
  2124. *
  2125. * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
  2126. * dev_id: the internal device identifier
  2127. * origin_dev: a device external to the pool that should act as the origin
  2128. *
  2129. * If the pool device has discards disabled, they get disabled for the thin
  2130. * device as well.
  2131. */
  2132. static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2133. {
  2134. int r;
  2135. struct thin_c *tc;
  2136. struct dm_dev *pool_dev, *origin_dev;
  2137. struct mapped_device *pool_md;
  2138. mutex_lock(&dm_thin_pool_table.mutex);
  2139. if (argc != 2 && argc != 3) {
  2140. ti->error = "Invalid argument count";
  2141. r = -EINVAL;
  2142. goto out_unlock;
  2143. }
  2144. tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
  2145. if (!tc) {
  2146. ti->error = "Out of memory";
  2147. r = -ENOMEM;
  2148. goto out_unlock;
  2149. }
  2150. if (argc == 3) {
  2151. r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
  2152. if (r) {
  2153. ti->error = "Error opening origin device";
  2154. goto bad_origin_dev;
  2155. }
  2156. tc->origin_dev = origin_dev;
  2157. }
  2158. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
  2159. if (r) {
  2160. ti->error = "Error opening pool device";
  2161. goto bad_pool_dev;
  2162. }
  2163. tc->pool_dev = pool_dev;
  2164. if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
  2165. ti->error = "Invalid device id";
  2166. r = -EINVAL;
  2167. goto bad_common;
  2168. }
  2169. pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
  2170. if (!pool_md) {
  2171. ti->error = "Couldn't get pool mapped device";
  2172. r = -EINVAL;
  2173. goto bad_common;
  2174. }
  2175. tc->pool = __pool_table_lookup(pool_md);
  2176. if (!tc->pool) {
  2177. ti->error = "Couldn't find pool object";
  2178. r = -EINVAL;
  2179. goto bad_pool_lookup;
  2180. }
  2181. __pool_inc(tc->pool);
  2182. r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
  2183. if (r) {
  2184. ti->error = "Couldn't open thin internal device";
  2185. goto bad_thin_open;
  2186. }
  2187. ti->split_io = tc->pool->sectors_per_block;
  2188. ti->num_flush_requests = 1;
  2189. /* In case the pool supports discards, pass them on. */
  2190. if (tc->pool->pf.discard_enabled) {
  2191. ti->discards_supported = 1;
  2192. ti->num_discard_requests = 1;
  2193. }
  2194. dm_put(pool_md);
  2195. mutex_unlock(&dm_thin_pool_table.mutex);
  2196. return 0;
  2197. bad_thin_open:
  2198. __pool_dec(tc->pool);
  2199. bad_pool_lookup:
  2200. dm_put(pool_md);
  2201. bad_common:
  2202. dm_put_device(ti, tc->pool_dev);
  2203. bad_pool_dev:
  2204. if (tc->origin_dev)
  2205. dm_put_device(ti, tc->origin_dev);
  2206. bad_origin_dev:
  2207. kfree(tc);
  2208. out_unlock:
  2209. mutex_unlock(&dm_thin_pool_table.mutex);
  2210. return r;
  2211. }
  2212. static int thin_map(struct dm_target *ti, struct bio *bio,
  2213. union map_info *map_context)
  2214. {
  2215. bio->bi_sector = dm_target_offset(ti, bio->bi_sector);
  2216. return thin_bio_map(ti, bio, map_context);
  2217. }
  2218. static int thin_endio(struct dm_target *ti,
  2219. struct bio *bio, int err,
  2220. union map_info *map_context)
  2221. {
  2222. unsigned long flags;
  2223. struct dm_thin_endio_hook *h = map_context->ptr;
  2224. struct list_head work;
  2225. struct dm_thin_new_mapping *m, *tmp;
  2226. struct pool *pool = h->tc->pool;
  2227. if (h->shared_read_entry) {
  2228. INIT_LIST_HEAD(&work);
  2229. ds_dec(h->shared_read_entry, &work);
  2230. spin_lock_irqsave(&pool->lock, flags);
  2231. list_for_each_entry_safe(m, tmp, &work, list) {
  2232. list_del(&m->list);
  2233. m->quiesced = 1;
  2234. __maybe_add_mapping(m);
  2235. }
  2236. spin_unlock_irqrestore(&pool->lock, flags);
  2237. }
  2238. if (h->all_io_entry) {
  2239. INIT_LIST_HEAD(&work);
  2240. ds_dec(h->all_io_entry, &work);
  2241. spin_lock_irqsave(&pool->lock, flags);
  2242. list_for_each_entry_safe(m, tmp, &work, list)
  2243. list_add(&m->list, &pool->prepared_discards);
  2244. spin_unlock_irqrestore(&pool->lock, flags);
  2245. }
  2246. mempool_free(h, pool->endio_hook_pool);
  2247. return 0;
  2248. }
  2249. static void thin_postsuspend(struct dm_target *ti)
  2250. {
  2251. if (dm_noflush_suspending(ti))
  2252. requeue_io((struct thin_c *)ti->private);
  2253. }
  2254. /*
  2255. * <nr mapped sectors> <highest mapped sector>
  2256. */
  2257. static int thin_status(struct dm_target *ti, status_type_t type,
  2258. char *result, unsigned maxlen)
  2259. {
  2260. int r;
  2261. ssize_t sz = 0;
  2262. dm_block_t mapped, highest;
  2263. char buf[BDEVNAME_SIZE];
  2264. struct thin_c *tc = ti->private;
  2265. if (!tc->td)
  2266. DMEMIT("-");
  2267. else {
  2268. switch (type) {
  2269. case STATUSTYPE_INFO:
  2270. r = dm_thin_get_mapped_count(tc->td, &mapped);
  2271. if (r)
  2272. return r;
  2273. r = dm_thin_get_highest_mapped_block(tc->td, &highest);
  2274. if (r < 0)
  2275. return r;
  2276. DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
  2277. if (r)
  2278. DMEMIT("%llu", ((highest + 1) *
  2279. tc->pool->sectors_per_block) - 1);
  2280. else
  2281. DMEMIT("-");
  2282. break;
  2283. case STATUSTYPE_TABLE:
  2284. DMEMIT("%s %lu",
  2285. format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
  2286. (unsigned long) tc->dev_id);
  2287. if (tc->origin_dev)
  2288. DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
  2289. break;
  2290. }
  2291. }
  2292. return 0;
  2293. }
  2294. static int thin_iterate_devices(struct dm_target *ti,
  2295. iterate_devices_callout_fn fn, void *data)
  2296. {
  2297. dm_block_t blocks;
  2298. struct thin_c *tc = ti->private;
  2299. /*
  2300. * We can't call dm_pool_get_data_dev_size() since that blocks. So
  2301. * we follow a more convoluted path through to the pool's target.
  2302. */
  2303. if (!tc->pool->ti)
  2304. return 0; /* nothing is bound */
  2305. blocks = tc->pool->ti->len >> tc->pool->block_shift;
  2306. if (blocks)
  2307. return fn(ti, tc->pool_dev, 0, tc->pool->sectors_per_block * blocks, data);
  2308. return 0;
  2309. }
  2310. static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2311. {
  2312. struct thin_c *tc = ti->private;
  2313. struct pool *pool = tc->pool;
  2314. blk_limits_io_min(limits, 0);
  2315. blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
  2316. set_discard_limits(pool, limits);
  2317. }
  2318. static struct target_type thin_target = {
  2319. .name = "thin",
  2320. .version = {1, 1, 0},
  2321. .module = THIS_MODULE,
  2322. .ctr = thin_ctr,
  2323. .dtr = thin_dtr,
  2324. .map = thin_map,
  2325. .end_io = thin_endio,
  2326. .postsuspend = thin_postsuspend,
  2327. .status = thin_status,
  2328. .iterate_devices = thin_iterate_devices,
  2329. .io_hints = thin_io_hints,
  2330. };
  2331. /*----------------------------------------------------------------*/
  2332. static int __init dm_thin_init(void)
  2333. {
  2334. int r;
  2335. pool_table_init();
  2336. r = dm_register_target(&thin_target);
  2337. if (r)
  2338. return r;
  2339. r = dm_register_target(&pool_target);
  2340. if (r)
  2341. goto bad_pool_target;
  2342. r = -ENOMEM;
  2343. _cell_cache = KMEM_CACHE(dm_bio_prison_cell, 0);
  2344. if (!_cell_cache)
  2345. goto bad_cell_cache;
  2346. _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
  2347. if (!_new_mapping_cache)
  2348. goto bad_new_mapping_cache;
  2349. _endio_hook_cache = KMEM_CACHE(dm_thin_endio_hook, 0);
  2350. if (!_endio_hook_cache)
  2351. goto bad_endio_hook_cache;
  2352. return 0;
  2353. bad_endio_hook_cache:
  2354. kmem_cache_destroy(_new_mapping_cache);
  2355. bad_new_mapping_cache:
  2356. kmem_cache_destroy(_cell_cache);
  2357. bad_cell_cache:
  2358. dm_unregister_target(&pool_target);
  2359. bad_pool_target:
  2360. dm_unregister_target(&thin_target);
  2361. return r;
  2362. }
  2363. static void dm_thin_exit(void)
  2364. {
  2365. dm_unregister_target(&thin_target);
  2366. dm_unregister_target(&pool_target);
  2367. kmem_cache_destroy(_cell_cache);
  2368. kmem_cache_destroy(_new_mapping_cache);
  2369. kmem_cache_destroy(_endio_hook_cache);
  2370. }
  2371. module_init(dm_thin_init);
  2372. module_exit(dm_thin_exit);
  2373. MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
  2374. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2375. MODULE_LICENSE("GPL");