dm-thin.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463
  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 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. /*
  127. * @nr_cells should be the number of cells you want in use _concurrently_.
  128. * Don't confuse it with the number of distinct keys.
  129. */
  130. static struct bio_prison *prison_create(unsigned nr_cells)
  131. {
  132. unsigned i;
  133. uint32_t nr_buckets = calc_nr_buckets(nr_cells);
  134. size_t len = sizeof(struct bio_prison) +
  135. (sizeof(struct hlist_head) * nr_buckets);
  136. struct bio_prison *prison = kmalloc(len, GFP_KERNEL);
  137. if (!prison)
  138. return NULL;
  139. spin_lock_init(&prison->lock);
  140. prison->cell_pool = mempool_create_kmalloc_pool(nr_cells,
  141. sizeof(struct cell));
  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 cell *__search_bucket(struct hlist_head *bucket,
  171. struct cell_key *key)
  172. {
  173. struct 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 cell **ref)
  188. {
  189. int r = 1;
  190. unsigned long flags;
  191. uint32_t hash = hash_key(prison, key);
  192. struct 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 cell *cell, struct bio_list *inmates)
  235. {
  236. struct bio_prison *prison = cell->prison;
  237. hlist_del(&cell->list);
  238. bio_list_add(inmates, cell->holder);
  239. bio_list_merge(inmates, &cell->bios);
  240. mempool_free(cell, prison->cell_pool);
  241. }
  242. static void cell_release(struct cell *cell, struct bio_list *bios)
  243. {
  244. unsigned long flags;
  245. struct bio_prison *prison = cell->prison;
  246. spin_lock_irqsave(&prison->lock, flags);
  247. __cell_release(cell, bios);
  248. spin_unlock_irqrestore(&prison->lock, flags);
  249. }
  250. /*
  251. * There are a couple of places where we put a bio into a cell briefly
  252. * before taking it out again. In these situations we know that no other
  253. * bio may be in the cell. This function releases the cell, and also does
  254. * a sanity check.
  255. */
  256. static void __cell_release_singleton(struct cell *cell, struct bio *bio)
  257. {
  258. hlist_del(&cell->list);
  259. BUG_ON(cell->holder != bio);
  260. BUG_ON(!bio_list_empty(&cell->bios));
  261. }
  262. static void cell_release_singleton(struct cell *cell, struct bio *bio)
  263. {
  264. unsigned long flags;
  265. struct bio_prison *prison = cell->prison;
  266. spin_lock_irqsave(&prison->lock, flags);
  267. __cell_release_singleton(cell, bio);
  268. spin_unlock_irqrestore(&prison->lock, flags);
  269. }
  270. /*
  271. * Sometimes we don't want the holder, just the additional bios.
  272. */
  273. static void __cell_release_no_holder(struct cell *cell, struct bio_list *inmates)
  274. {
  275. struct bio_prison *prison = cell->prison;
  276. hlist_del(&cell->list);
  277. bio_list_merge(inmates, &cell->bios);
  278. mempool_free(cell, prison->cell_pool);
  279. }
  280. static void cell_release_no_holder(struct cell *cell, struct bio_list *inmates)
  281. {
  282. unsigned long flags;
  283. struct bio_prison *prison = cell->prison;
  284. spin_lock_irqsave(&prison->lock, flags);
  285. __cell_release_no_holder(cell, inmates);
  286. spin_unlock_irqrestore(&prison->lock, flags);
  287. }
  288. static void cell_error(struct cell *cell)
  289. {
  290. struct bio_prison *prison = cell->prison;
  291. struct bio_list bios;
  292. struct bio *bio;
  293. unsigned long flags;
  294. bio_list_init(&bios);
  295. spin_lock_irqsave(&prison->lock, flags);
  296. __cell_release(cell, &bios);
  297. spin_unlock_irqrestore(&prison->lock, flags);
  298. while ((bio = bio_list_pop(&bios)))
  299. bio_io_error(bio);
  300. }
  301. /*----------------------------------------------------------------*/
  302. /*
  303. * We use the deferred set to keep track of pending reads to shared blocks.
  304. * We do this to ensure the new mapping caused by a write isn't performed
  305. * until these prior reads have completed. Otherwise the insertion of the
  306. * new mapping could free the old block that the read bios are mapped to.
  307. */
  308. struct deferred_set;
  309. struct deferred_entry {
  310. struct deferred_set *ds;
  311. unsigned count;
  312. struct list_head work_items;
  313. };
  314. struct deferred_set {
  315. spinlock_t lock;
  316. unsigned current_entry;
  317. unsigned sweeper;
  318. struct deferred_entry entries[DEFERRED_SET_SIZE];
  319. };
  320. static void ds_init(struct deferred_set *ds)
  321. {
  322. int i;
  323. spin_lock_init(&ds->lock);
  324. ds->current_entry = 0;
  325. ds->sweeper = 0;
  326. for (i = 0; i < DEFERRED_SET_SIZE; i++) {
  327. ds->entries[i].ds = ds;
  328. ds->entries[i].count = 0;
  329. INIT_LIST_HEAD(&ds->entries[i].work_items);
  330. }
  331. }
  332. static struct deferred_entry *ds_inc(struct deferred_set *ds)
  333. {
  334. unsigned long flags;
  335. struct deferred_entry *entry;
  336. spin_lock_irqsave(&ds->lock, flags);
  337. entry = ds->entries + ds->current_entry;
  338. entry->count++;
  339. spin_unlock_irqrestore(&ds->lock, flags);
  340. return entry;
  341. }
  342. static unsigned ds_next(unsigned index)
  343. {
  344. return (index + 1) % DEFERRED_SET_SIZE;
  345. }
  346. static void __sweep(struct deferred_set *ds, struct list_head *head)
  347. {
  348. while ((ds->sweeper != ds->current_entry) &&
  349. !ds->entries[ds->sweeper].count) {
  350. list_splice_init(&ds->entries[ds->sweeper].work_items, head);
  351. ds->sweeper = ds_next(ds->sweeper);
  352. }
  353. if ((ds->sweeper == ds->current_entry) && !ds->entries[ds->sweeper].count)
  354. list_splice_init(&ds->entries[ds->sweeper].work_items, head);
  355. }
  356. static void ds_dec(struct deferred_entry *entry, struct list_head *head)
  357. {
  358. unsigned long flags;
  359. spin_lock_irqsave(&entry->ds->lock, flags);
  360. BUG_ON(!entry->count);
  361. --entry->count;
  362. __sweep(entry->ds, head);
  363. spin_unlock_irqrestore(&entry->ds->lock, flags);
  364. }
  365. /*
  366. * Returns 1 if deferred or 0 if no pending items to delay job.
  367. */
  368. static int ds_add_work(struct deferred_set *ds, struct list_head *work)
  369. {
  370. int r = 1;
  371. unsigned long flags;
  372. unsigned next_entry;
  373. spin_lock_irqsave(&ds->lock, flags);
  374. if ((ds->sweeper == ds->current_entry) &&
  375. !ds->entries[ds->current_entry].count)
  376. r = 0;
  377. else {
  378. list_add(work, &ds->entries[ds->current_entry].work_items);
  379. next_entry = ds_next(ds->current_entry);
  380. if (!ds->entries[next_entry].count)
  381. ds->current_entry = next_entry;
  382. }
  383. spin_unlock_irqrestore(&ds->lock, flags);
  384. return r;
  385. }
  386. /*----------------------------------------------------------------*/
  387. /*
  388. * Key building.
  389. */
  390. static void build_data_key(struct dm_thin_device *td,
  391. dm_block_t b, struct cell_key *key)
  392. {
  393. key->virtual = 0;
  394. key->dev = dm_thin_dev_id(td);
  395. key->block = b;
  396. }
  397. static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
  398. struct cell_key *key)
  399. {
  400. key->virtual = 1;
  401. key->dev = dm_thin_dev_id(td);
  402. key->block = b;
  403. }
  404. /*----------------------------------------------------------------*/
  405. /*
  406. * A pool device ties together a metadata device and a data device. It
  407. * also provides the interface for creating and destroying internal
  408. * devices.
  409. */
  410. struct new_mapping;
  411. struct pool {
  412. struct list_head list;
  413. struct dm_target *ti; /* Only set if a pool target is bound */
  414. struct mapped_device *pool_md;
  415. struct block_device *md_dev;
  416. struct dm_pool_metadata *pmd;
  417. uint32_t sectors_per_block;
  418. unsigned block_shift;
  419. dm_block_t offset_mask;
  420. dm_block_t low_water_blocks;
  421. unsigned zero_new_blocks:1;
  422. unsigned low_water_triggered:1; /* A dm event has been sent */
  423. unsigned no_free_space:1; /* A -ENOSPC warning has been issued */
  424. struct bio_prison *prison;
  425. struct dm_kcopyd_client *copier;
  426. struct workqueue_struct *wq;
  427. struct work_struct worker;
  428. struct delayed_work waker;
  429. unsigned ref_count;
  430. unsigned long last_commit_jiffies;
  431. spinlock_t lock;
  432. struct bio_list deferred_bios;
  433. struct bio_list deferred_flush_bios;
  434. struct list_head prepared_mappings;
  435. struct bio_list retry_on_resume_list;
  436. struct deferred_set ds; /* FIXME: move to thin_c */
  437. struct new_mapping *next_mapping;
  438. mempool_t *mapping_pool;
  439. mempool_t *endio_hook_pool;
  440. };
  441. /*
  442. * Target context for a pool.
  443. */
  444. struct pool_c {
  445. struct dm_target *ti;
  446. struct pool *pool;
  447. struct dm_dev *data_dev;
  448. struct dm_dev *metadata_dev;
  449. struct dm_target_callbacks callbacks;
  450. dm_block_t low_water_blocks;
  451. unsigned zero_new_blocks:1;
  452. };
  453. /*
  454. * Target context for a thin.
  455. */
  456. struct thin_c {
  457. struct dm_dev *pool_dev;
  458. dm_thin_id dev_id;
  459. struct pool *pool;
  460. struct dm_thin_device *td;
  461. };
  462. /*----------------------------------------------------------------*/
  463. /*
  464. * A global list of pools that uses a struct mapped_device as a key.
  465. */
  466. static struct dm_thin_pool_table {
  467. struct mutex mutex;
  468. struct list_head pools;
  469. } dm_thin_pool_table;
  470. static void pool_table_init(void)
  471. {
  472. mutex_init(&dm_thin_pool_table.mutex);
  473. INIT_LIST_HEAD(&dm_thin_pool_table.pools);
  474. }
  475. static void __pool_table_insert(struct pool *pool)
  476. {
  477. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  478. list_add(&pool->list, &dm_thin_pool_table.pools);
  479. }
  480. static void __pool_table_remove(struct pool *pool)
  481. {
  482. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  483. list_del(&pool->list);
  484. }
  485. static struct pool *__pool_table_lookup(struct mapped_device *md)
  486. {
  487. struct pool *pool = NULL, *tmp;
  488. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  489. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  490. if (tmp->pool_md == md) {
  491. pool = tmp;
  492. break;
  493. }
  494. }
  495. return pool;
  496. }
  497. static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
  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->md_dev == md_dev) {
  503. pool = tmp;
  504. break;
  505. }
  506. }
  507. return pool;
  508. }
  509. /*----------------------------------------------------------------*/
  510. static void __requeue_bio_list(struct thin_c *tc, struct bio_list *master)
  511. {
  512. struct bio *bio;
  513. struct bio_list bios;
  514. bio_list_init(&bios);
  515. bio_list_merge(&bios, master);
  516. bio_list_init(master);
  517. while ((bio = bio_list_pop(&bios))) {
  518. if (dm_get_mapinfo(bio)->ptr == tc)
  519. bio_endio(bio, DM_ENDIO_REQUEUE);
  520. else
  521. bio_list_add(master, bio);
  522. }
  523. }
  524. static void requeue_io(struct thin_c *tc)
  525. {
  526. struct pool *pool = tc->pool;
  527. unsigned long flags;
  528. spin_lock_irqsave(&pool->lock, flags);
  529. __requeue_bio_list(tc, &pool->deferred_bios);
  530. __requeue_bio_list(tc, &pool->retry_on_resume_list);
  531. spin_unlock_irqrestore(&pool->lock, flags);
  532. }
  533. /*
  534. * This section of code contains the logic for processing a thin device's IO.
  535. * Much of the code depends on pool object resources (lists, workqueues, etc)
  536. * but most is exclusively called from the thin target rather than the thin-pool
  537. * target.
  538. */
  539. static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
  540. {
  541. return bio->bi_sector >> tc->pool->block_shift;
  542. }
  543. static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
  544. {
  545. struct pool *pool = tc->pool;
  546. bio->bi_bdev = tc->pool_dev->bdev;
  547. bio->bi_sector = (block << pool->block_shift) +
  548. (bio->bi_sector & pool->offset_mask);
  549. }
  550. static void remap_and_issue(struct thin_c *tc, struct bio *bio,
  551. dm_block_t block)
  552. {
  553. struct pool *pool = tc->pool;
  554. unsigned long flags;
  555. remap(tc, bio, block);
  556. /*
  557. * Batch together any FUA/FLUSH bios we find and then issue
  558. * a single commit for them in process_deferred_bios().
  559. */
  560. if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
  561. spin_lock_irqsave(&pool->lock, flags);
  562. bio_list_add(&pool->deferred_flush_bios, bio);
  563. spin_unlock_irqrestore(&pool->lock, flags);
  564. } else
  565. generic_make_request(bio);
  566. }
  567. /*
  568. * wake_worker() is used when new work is queued and when pool_resume is
  569. * ready to continue deferred IO processing.
  570. */
  571. static void wake_worker(struct pool *pool)
  572. {
  573. queue_work(pool->wq, &pool->worker);
  574. }
  575. /*----------------------------------------------------------------*/
  576. /*
  577. * Bio endio functions.
  578. */
  579. struct endio_hook {
  580. struct thin_c *tc;
  581. bio_end_io_t *saved_bi_end_io;
  582. struct deferred_entry *entry;
  583. };
  584. struct new_mapping {
  585. struct list_head list;
  586. int prepared;
  587. struct thin_c *tc;
  588. dm_block_t virt_block;
  589. dm_block_t data_block;
  590. struct cell *cell;
  591. int err;
  592. /*
  593. * If the bio covers the whole area of a block then we can avoid
  594. * zeroing or copying. Instead this bio is hooked. The bio will
  595. * still be in the cell, so care has to be taken to avoid issuing
  596. * the bio twice.
  597. */
  598. struct bio *bio;
  599. bio_end_io_t *saved_bi_end_io;
  600. };
  601. static void __maybe_add_mapping(struct new_mapping *m)
  602. {
  603. struct pool *pool = m->tc->pool;
  604. if (list_empty(&m->list) && m->prepared) {
  605. list_add(&m->list, &pool->prepared_mappings);
  606. wake_worker(pool);
  607. }
  608. }
  609. static void copy_complete(int read_err, unsigned long write_err, void *context)
  610. {
  611. unsigned long flags;
  612. struct new_mapping *m = context;
  613. struct pool *pool = m->tc->pool;
  614. m->err = read_err || write_err ? -EIO : 0;
  615. spin_lock_irqsave(&pool->lock, flags);
  616. m->prepared = 1;
  617. __maybe_add_mapping(m);
  618. spin_unlock_irqrestore(&pool->lock, flags);
  619. }
  620. static void overwrite_endio(struct bio *bio, int err)
  621. {
  622. unsigned long flags;
  623. struct new_mapping *m = dm_get_mapinfo(bio)->ptr;
  624. struct pool *pool = m->tc->pool;
  625. m->err = err;
  626. spin_lock_irqsave(&pool->lock, flags);
  627. m->prepared = 1;
  628. __maybe_add_mapping(m);
  629. spin_unlock_irqrestore(&pool->lock, flags);
  630. }
  631. static void shared_read_endio(struct bio *bio, int err)
  632. {
  633. struct list_head mappings;
  634. struct new_mapping *m, *tmp;
  635. struct endio_hook *h = dm_get_mapinfo(bio)->ptr;
  636. unsigned long flags;
  637. struct pool *pool = h->tc->pool;
  638. bio->bi_end_io = h->saved_bi_end_io;
  639. bio_endio(bio, err);
  640. INIT_LIST_HEAD(&mappings);
  641. ds_dec(h->entry, &mappings);
  642. spin_lock_irqsave(&pool->lock, flags);
  643. list_for_each_entry_safe(m, tmp, &mappings, list) {
  644. list_del(&m->list);
  645. INIT_LIST_HEAD(&m->list);
  646. __maybe_add_mapping(m);
  647. }
  648. spin_unlock_irqrestore(&pool->lock, flags);
  649. mempool_free(h, pool->endio_hook_pool);
  650. }
  651. /*----------------------------------------------------------------*/
  652. /*
  653. * Workqueue.
  654. */
  655. /*
  656. * Prepared mapping jobs.
  657. */
  658. /*
  659. * This sends the bios in the cell back to the deferred_bios list.
  660. */
  661. static void cell_defer(struct thin_c *tc, struct cell *cell,
  662. dm_block_t data_block)
  663. {
  664. struct pool *pool = tc->pool;
  665. unsigned long flags;
  666. spin_lock_irqsave(&pool->lock, flags);
  667. cell_release(cell, &pool->deferred_bios);
  668. spin_unlock_irqrestore(&tc->pool->lock, flags);
  669. wake_worker(pool);
  670. }
  671. /*
  672. * Same as cell_defer above, except it omits one particular detainee,
  673. * a write bio that covers the block and has already been processed.
  674. */
  675. static void cell_defer_except(struct thin_c *tc, struct cell *cell)
  676. {
  677. struct bio_list bios;
  678. struct pool *pool = tc->pool;
  679. unsigned long flags;
  680. bio_list_init(&bios);
  681. spin_lock_irqsave(&pool->lock, flags);
  682. cell_release_no_holder(cell, &pool->deferred_bios);
  683. spin_unlock_irqrestore(&pool->lock, flags);
  684. wake_worker(pool);
  685. }
  686. static void process_prepared_mapping(struct new_mapping *m)
  687. {
  688. struct thin_c *tc = m->tc;
  689. struct bio *bio;
  690. int r;
  691. bio = m->bio;
  692. if (bio)
  693. bio->bi_end_io = m->saved_bi_end_io;
  694. if (m->err) {
  695. cell_error(m->cell);
  696. return;
  697. }
  698. /*
  699. * Commit the prepared block into the mapping btree.
  700. * Any I/O for this block arriving after this point will get
  701. * remapped to it directly.
  702. */
  703. r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
  704. if (r) {
  705. DMERR("dm_thin_insert_block() failed");
  706. cell_error(m->cell);
  707. return;
  708. }
  709. /*
  710. * Release any bios held while the block was being provisioned.
  711. * If we are processing a write bio that completely covers the block,
  712. * we already processed it so can ignore it now when processing
  713. * the bios in the cell.
  714. */
  715. if (bio) {
  716. cell_defer_except(tc, m->cell);
  717. bio_endio(bio, 0);
  718. } else
  719. cell_defer(tc, m->cell, m->data_block);
  720. list_del(&m->list);
  721. mempool_free(m, tc->pool->mapping_pool);
  722. }
  723. static void process_prepared_mappings(struct pool *pool)
  724. {
  725. unsigned long flags;
  726. struct list_head maps;
  727. struct new_mapping *m, *tmp;
  728. INIT_LIST_HEAD(&maps);
  729. spin_lock_irqsave(&pool->lock, flags);
  730. list_splice_init(&pool->prepared_mappings, &maps);
  731. spin_unlock_irqrestore(&pool->lock, flags);
  732. list_for_each_entry_safe(m, tmp, &maps, list)
  733. process_prepared_mapping(m);
  734. }
  735. /*
  736. * Deferred bio jobs.
  737. */
  738. static int io_overwrites_block(struct pool *pool, struct bio *bio)
  739. {
  740. return ((bio_data_dir(bio) == WRITE) &&
  741. !(bio->bi_sector & pool->offset_mask)) &&
  742. (bio->bi_size == (pool->sectors_per_block << SECTOR_SHIFT));
  743. }
  744. static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
  745. bio_end_io_t *fn)
  746. {
  747. *save = bio->bi_end_io;
  748. bio->bi_end_io = fn;
  749. }
  750. static int ensure_next_mapping(struct pool *pool)
  751. {
  752. if (pool->next_mapping)
  753. return 0;
  754. pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
  755. return pool->next_mapping ? 0 : -ENOMEM;
  756. }
  757. static struct new_mapping *get_next_mapping(struct pool *pool)
  758. {
  759. struct new_mapping *r = pool->next_mapping;
  760. BUG_ON(!pool->next_mapping);
  761. pool->next_mapping = NULL;
  762. return r;
  763. }
  764. static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
  765. dm_block_t data_origin, dm_block_t data_dest,
  766. struct cell *cell, struct bio *bio)
  767. {
  768. int r;
  769. struct pool *pool = tc->pool;
  770. struct new_mapping *m = get_next_mapping(pool);
  771. INIT_LIST_HEAD(&m->list);
  772. m->prepared = 0;
  773. m->tc = tc;
  774. m->virt_block = virt_block;
  775. m->data_block = data_dest;
  776. m->cell = cell;
  777. m->err = 0;
  778. m->bio = NULL;
  779. ds_add_work(&pool->ds, &m->list);
  780. /*
  781. * IO to pool_dev remaps to the pool target's data_dev.
  782. *
  783. * If the whole block of data is being overwritten, we can issue the
  784. * bio immediately. Otherwise we use kcopyd to clone the data first.
  785. */
  786. if (io_overwrites_block(pool, bio)) {
  787. m->bio = bio;
  788. save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
  789. dm_get_mapinfo(bio)->ptr = m;
  790. remap_and_issue(tc, bio, data_dest);
  791. } else {
  792. struct dm_io_region from, to;
  793. from.bdev = tc->pool_dev->bdev;
  794. from.sector = data_origin * pool->sectors_per_block;
  795. from.count = pool->sectors_per_block;
  796. to.bdev = tc->pool_dev->bdev;
  797. to.sector = data_dest * pool->sectors_per_block;
  798. to.count = pool->sectors_per_block;
  799. r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
  800. 0, copy_complete, m);
  801. if (r < 0) {
  802. mempool_free(m, pool->mapping_pool);
  803. DMERR("dm_kcopyd_copy() failed");
  804. cell_error(cell);
  805. }
  806. }
  807. }
  808. static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
  809. dm_block_t data_block, struct cell *cell,
  810. struct bio *bio)
  811. {
  812. struct pool *pool = tc->pool;
  813. struct new_mapping *m = get_next_mapping(pool);
  814. INIT_LIST_HEAD(&m->list);
  815. m->prepared = 0;
  816. m->tc = tc;
  817. m->virt_block = virt_block;
  818. m->data_block = data_block;
  819. m->cell = cell;
  820. m->err = 0;
  821. m->bio = NULL;
  822. /*
  823. * If the whole block of data is being overwritten or we are not
  824. * zeroing pre-existing data, we can issue the bio immediately.
  825. * Otherwise we use kcopyd to zero the data first.
  826. */
  827. if (!pool->zero_new_blocks)
  828. process_prepared_mapping(m);
  829. else if (io_overwrites_block(pool, bio)) {
  830. m->bio = bio;
  831. save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
  832. dm_get_mapinfo(bio)->ptr = m;
  833. remap_and_issue(tc, bio, data_block);
  834. } else {
  835. int r;
  836. struct dm_io_region to;
  837. to.bdev = tc->pool_dev->bdev;
  838. to.sector = data_block * pool->sectors_per_block;
  839. to.count = pool->sectors_per_block;
  840. r = dm_kcopyd_zero(pool->copier, 1, &to, 0, copy_complete, m);
  841. if (r < 0) {
  842. mempool_free(m, pool->mapping_pool);
  843. DMERR("dm_kcopyd_zero() failed");
  844. cell_error(cell);
  845. }
  846. }
  847. }
  848. static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
  849. {
  850. int r;
  851. dm_block_t free_blocks;
  852. unsigned long flags;
  853. struct pool *pool = tc->pool;
  854. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  855. if (r)
  856. return r;
  857. if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
  858. DMWARN("%s: reached low water mark, sending event.",
  859. dm_device_name(pool->pool_md));
  860. spin_lock_irqsave(&pool->lock, flags);
  861. pool->low_water_triggered = 1;
  862. spin_unlock_irqrestore(&pool->lock, flags);
  863. dm_table_event(pool->ti->table);
  864. }
  865. if (!free_blocks) {
  866. if (pool->no_free_space)
  867. return -ENOSPC;
  868. else {
  869. /*
  870. * Try to commit to see if that will free up some
  871. * more space.
  872. */
  873. r = dm_pool_commit_metadata(pool->pmd);
  874. if (r) {
  875. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  876. __func__, r);
  877. return r;
  878. }
  879. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  880. if (r)
  881. return r;
  882. /*
  883. * If we still have no space we set a flag to avoid
  884. * doing all this checking and return -ENOSPC.
  885. */
  886. if (!free_blocks) {
  887. DMWARN("%s: no free space available.",
  888. dm_device_name(pool->pool_md));
  889. spin_lock_irqsave(&pool->lock, flags);
  890. pool->no_free_space = 1;
  891. spin_unlock_irqrestore(&pool->lock, flags);
  892. return -ENOSPC;
  893. }
  894. }
  895. }
  896. r = dm_pool_alloc_data_block(pool->pmd, result);
  897. if (r)
  898. return r;
  899. return 0;
  900. }
  901. /*
  902. * If we have run out of space, queue bios until the device is
  903. * resumed, presumably after having been reloaded with more space.
  904. */
  905. static void retry_on_resume(struct bio *bio)
  906. {
  907. struct thin_c *tc = dm_get_mapinfo(bio)->ptr;
  908. struct pool *pool = tc->pool;
  909. unsigned long flags;
  910. spin_lock_irqsave(&pool->lock, flags);
  911. bio_list_add(&pool->retry_on_resume_list, bio);
  912. spin_unlock_irqrestore(&pool->lock, flags);
  913. }
  914. static void no_space(struct cell *cell)
  915. {
  916. struct bio *bio;
  917. struct bio_list bios;
  918. bio_list_init(&bios);
  919. cell_release(cell, &bios);
  920. while ((bio = bio_list_pop(&bios)))
  921. retry_on_resume(bio);
  922. }
  923. static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
  924. struct cell_key *key,
  925. struct dm_thin_lookup_result *lookup_result,
  926. struct cell *cell)
  927. {
  928. int r;
  929. dm_block_t data_block;
  930. r = alloc_data_block(tc, &data_block);
  931. switch (r) {
  932. case 0:
  933. schedule_copy(tc, block, lookup_result->block,
  934. data_block, cell, bio);
  935. break;
  936. case -ENOSPC:
  937. no_space(cell);
  938. break;
  939. default:
  940. DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
  941. cell_error(cell);
  942. break;
  943. }
  944. }
  945. static void process_shared_bio(struct thin_c *tc, struct bio *bio,
  946. dm_block_t block,
  947. struct dm_thin_lookup_result *lookup_result)
  948. {
  949. struct cell *cell;
  950. struct pool *pool = tc->pool;
  951. struct cell_key key;
  952. /*
  953. * If cell is already occupied, then sharing is already in the process
  954. * of being broken so we have nothing further to do here.
  955. */
  956. build_data_key(tc->td, lookup_result->block, &key);
  957. if (bio_detain(pool->prison, &key, bio, &cell))
  958. return;
  959. if (bio_data_dir(bio) == WRITE)
  960. break_sharing(tc, bio, block, &key, lookup_result, cell);
  961. else {
  962. struct endio_hook *h;
  963. h = mempool_alloc(pool->endio_hook_pool, GFP_NOIO);
  964. h->tc = tc;
  965. h->entry = ds_inc(&pool->ds);
  966. save_and_set_endio(bio, &h->saved_bi_end_io, shared_read_endio);
  967. dm_get_mapinfo(bio)->ptr = h;
  968. cell_release_singleton(cell, bio);
  969. remap_and_issue(tc, bio, lookup_result->block);
  970. }
  971. }
  972. static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
  973. struct cell *cell)
  974. {
  975. int r;
  976. dm_block_t data_block;
  977. /*
  978. * Remap empty bios (flushes) immediately, without provisioning.
  979. */
  980. if (!bio->bi_size) {
  981. cell_release_singleton(cell, bio);
  982. remap_and_issue(tc, bio, 0);
  983. return;
  984. }
  985. /*
  986. * Fill read bios with zeroes and complete them immediately.
  987. */
  988. if (bio_data_dir(bio) == READ) {
  989. zero_fill_bio(bio);
  990. cell_release_singleton(cell, bio);
  991. bio_endio(bio, 0);
  992. return;
  993. }
  994. r = alloc_data_block(tc, &data_block);
  995. switch (r) {
  996. case 0:
  997. schedule_zero(tc, block, data_block, cell, bio);
  998. break;
  999. case -ENOSPC:
  1000. no_space(cell);
  1001. break;
  1002. default:
  1003. DMERR("%s: alloc_data_block() failed, error = %d", __func__, r);
  1004. cell_error(cell);
  1005. break;
  1006. }
  1007. }
  1008. static void process_bio(struct thin_c *tc, struct bio *bio)
  1009. {
  1010. int r;
  1011. dm_block_t block = get_bio_block(tc, bio);
  1012. struct cell *cell;
  1013. struct cell_key key;
  1014. struct dm_thin_lookup_result lookup_result;
  1015. /*
  1016. * If cell is already occupied, then the block is already
  1017. * being provisioned so we have nothing further to do here.
  1018. */
  1019. build_virtual_key(tc->td, block, &key);
  1020. if (bio_detain(tc->pool->prison, &key, bio, &cell))
  1021. return;
  1022. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1023. switch (r) {
  1024. case 0:
  1025. /*
  1026. * We can release this cell now. This thread is the only
  1027. * one that puts bios into a cell, and we know there were
  1028. * no preceding bios.
  1029. */
  1030. /*
  1031. * TODO: this will probably have to change when discard goes
  1032. * back in.
  1033. */
  1034. cell_release_singleton(cell, bio);
  1035. if (lookup_result.shared)
  1036. process_shared_bio(tc, bio, block, &lookup_result);
  1037. else
  1038. remap_and_issue(tc, bio, lookup_result.block);
  1039. break;
  1040. case -ENODATA:
  1041. provision_block(tc, bio, block, cell);
  1042. break;
  1043. default:
  1044. DMERR("dm_thin_find_block() failed, error = %d", r);
  1045. bio_io_error(bio);
  1046. break;
  1047. }
  1048. }
  1049. static int need_commit_due_to_time(struct pool *pool)
  1050. {
  1051. return jiffies < pool->last_commit_jiffies ||
  1052. jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
  1053. }
  1054. static void process_deferred_bios(struct pool *pool)
  1055. {
  1056. unsigned long flags;
  1057. struct bio *bio;
  1058. struct bio_list bios;
  1059. int r;
  1060. bio_list_init(&bios);
  1061. spin_lock_irqsave(&pool->lock, flags);
  1062. bio_list_merge(&bios, &pool->deferred_bios);
  1063. bio_list_init(&pool->deferred_bios);
  1064. spin_unlock_irqrestore(&pool->lock, flags);
  1065. while ((bio = bio_list_pop(&bios))) {
  1066. struct thin_c *tc = dm_get_mapinfo(bio)->ptr;
  1067. /*
  1068. * If we've got no free new_mapping structs, and processing
  1069. * this bio might require one, we pause until there are some
  1070. * prepared mappings to process.
  1071. */
  1072. if (ensure_next_mapping(pool)) {
  1073. spin_lock_irqsave(&pool->lock, flags);
  1074. bio_list_merge(&pool->deferred_bios, &bios);
  1075. spin_unlock_irqrestore(&pool->lock, flags);
  1076. break;
  1077. }
  1078. process_bio(tc, bio);
  1079. }
  1080. /*
  1081. * If there are any deferred flush bios, we must commit
  1082. * the metadata before issuing them.
  1083. */
  1084. bio_list_init(&bios);
  1085. spin_lock_irqsave(&pool->lock, flags);
  1086. bio_list_merge(&bios, &pool->deferred_flush_bios);
  1087. bio_list_init(&pool->deferred_flush_bios);
  1088. spin_unlock_irqrestore(&pool->lock, flags);
  1089. if (bio_list_empty(&bios) && !need_commit_due_to_time(pool))
  1090. return;
  1091. r = dm_pool_commit_metadata(pool->pmd);
  1092. if (r) {
  1093. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  1094. __func__, r);
  1095. while ((bio = bio_list_pop(&bios)))
  1096. bio_io_error(bio);
  1097. return;
  1098. }
  1099. pool->last_commit_jiffies = jiffies;
  1100. while ((bio = bio_list_pop(&bios)))
  1101. generic_make_request(bio);
  1102. }
  1103. static void do_worker(struct work_struct *ws)
  1104. {
  1105. struct pool *pool = container_of(ws, struct pool, worker);
  1106. process_prepared_mappings(pool);
  1107. process_deferred_bios(pool);
  1108. }
  1109. /*
  1110. * We want to commit periodically so that not too much
  1111. * unwritten data builds up.
  1112. */
  1113. static void do_waker(struct work_struct *ws)
  1114. {
  1115. struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
  1116. wake_worker(pool);
  1117. queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
  1118. }
  1119. /*----------------------------------------------------------------*/
  1120. /*
  1121. * Mapping functions.
  1122. */
  1123. /*
  1124. * Called only while mapping a thin bio to hand it over to the workqueue.
  1125. */
  1126. static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
  1127. {
  1128. unsigned long flags;
  1129. struct pool *pool = tc->pool;
  1130. spin_lock_irqsave(&pool->lock, flags);
  1131. bio_list_add(&pool->deferred_bios, bio);
  1132. spin_unlock_irqrestore(&pool->lock, flags);
  1133. wake_worker(pool);
  1134. }
  1135. /*
  1136. * Non-blocking function called from the thin target's map function.
  1137. */
  1138. static int thin_bio_map(struct dm_target *ti, struct bio *bio,
  1139. union map_info *map_context)
  1140. {
  1141. int r;
  1142. struct thin_c *tc = ti->private;
  1143. dm_block_t block = get_bio_block(tc, bio);
  1144. struct dm_thin_device *td = tc->td;
  1145. struct dm_thin_lookup_result result;
  1146. /*
  1147. * Save the thin context for easy access from the deferred bio later.
  1148. */
  1149. map_context->ptr = tc;
  1150. if (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) {
  1151. thin_defer_bio(tc, bio);
  1152. return DM_MAPIO_SUBMITTED;
  1153. }
  1154. r = dm_thin_find_block(td, block, 0, &result);
  1155. /*
  1156. * Note that we defer readahead too.
  1157. */
  1158. switch (r) {
  1159. case 0:
  1160. if (unlikely(result.shared)) {
  1161. /*
  1162. * We have a race condition here between the
  1163. * result.shared value returned by the lookup and
  1164. * snapshot creation, which may cause new
  1165. * sharing.
  1166. *
  1167. * To avoid this always quiesce the origin before
  1168. * taking the snap. You want to do this anyway to
  1169. * ensure a consistent application view
  1170. * (i.e. lockfs).
  1171. *
  1172. * More distant ancestors are irrelevant. The
  1173. * shared flag will be set in their case.
  1174. */
  1175. thin_defer_bio(tc, bio);
  1176. r = DM_MAPIO_SUBMITTED;
  1177. } else {
  1178. remap(tc, bio, result.block);
  1179. r = DM_MAPIO_REMAPPED;
  1180. }
  1181. break;
  1182. case -ENODATA:
  1183. /*
  1184. * In future, the failed dm_thin_find_block above could
  1185. * provide the hint to load the metadata into cache.
  1186. */
  1187. case -EWOULDBLOCK:
  1188. thin_defer_bio(tc, bio);
  1189. r = DM_MAPIO_SUBMITTED;
  1190. break;
  1191. }
  1192. return r;
  1193. }
  1194. static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1195. {
  1196. int r;
  1197. unsigned long flags;
  1198. struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
  1199. spin_lock_irqsave(&pt->pool->lock, flags);
  1200. r = !bio_list_empty(&pt->pool->retry_on_resume_list);
  1201. spin_unlock_irqrestore(&pt->pool->lock, flags);
  1202. if (!r) {
  1203. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  1204. r = bdi_congested(&q->backing_dev_info, bdi_bits);
  1205. }
  1206. return r;
  1207. }
  1208. static void __requeue_bios(struct pool *pool)
  1209. {
  1210. bio_list_merge(&pool->deferred_bios, &pool->retry_on_resume_list);
  1211. bio_list_init(&pool->retry_on_resume_list);
  1212. }
  1213. /*----------------------------------------------------------------
  1214. * Binding of control targets to a pool object
  1215. *--------------------------------------------------------------*/
  1216. static int bind_control_target(struct pool *pool, struct dm_target *ti)
  1217. {
  1218. struct pool_c *pt = ti->private;
  1219. pool->ti = ti;
  1220. pool->low_water_blocks = pt->low_water_blocks;
  1221. pool->zero_new_blocks = pt->zero_new_blocks;
  1222. return 0;
  1223. }
  1224. static void unbind_control_target(struct pool *pool, struct dm_target *ti)
  1225. {
  1226. if (pool->ti == ti)
  1227. pool->ti = NULL;
  1228. }
  1229. /*----------------------------------------------------------------
  1230. * Pool creation
  1231. *--------------------------------------------------------------*/
  1232. static void __pool_destroy(struct pool *pool)
  1233. {
  1234. __pool_table_remove(pool);
  1235. if (dm_pool_metadata_close(pool->pmd) < 0)
  1236. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  1237. prison_destroy(pool->prison);
  1238. dm_kcopyd_client_destroy(pool->copier);
  1239. if (pool->wq)
  1240. destroy_workqueue(pool->wq);
  1241. if (pool->next_mapping)
  1242. mempool_free(pool->next_mapping, pool->mapping_pool);
  1243. mempool_destroy(pool->mapping_pool);
  1244. mempool_destroy(pool->endio_hook_pool);
  1245. kfree(pool);
  1246. }
  1247. static struct pool *pool_create(struct mapped_device *pool_md,
  1248. struct block_device *metadata_dev,
  1249. unsigned long block_size, char **error)
  1250. {
  1251. int r;
  1252. void *err_p;
  1253. struct pool *pool;
  1254. struct dm_pool_metadata *pmd;
  1255. pmd = dm_pool_metadata_open(metadata_dev, block_size);
  1256. if (IS_ERR(pmd)) {
  1257. *error = "Error creating metadata object";
  1258. return (struct pool *)pmd;
  1259. }
  1260. pool = kmalloc(sizeof(*pool), GFP_KERNEL);
  1261. if (!pool) {
  1262. *error = "Error allocating memory for pool";
  1263. err_p = ERR_PTR(-ENOMEM);
  1264. goto bad_pool;
  1265. }
  1266. pool->pmd = pmd;
  1267. pool->sectors_per_block = block_size;
  1268. pool->block_shift = ffs(block_size) - 1;
  1269. pool->offset_mask = block_size - 1;
  1270. pool->low_water_blocks = 0;
  1271. pool->zero_new_blocks = 1;
  1272. pool->prison = prison_create(PRISON_CELLS);
  1273. if (!pool->prison) {
  1274. *error = "Error creating pool's bio prison";
  1275. err_p = ERR_PTR(-ENOMEM);
  1276. goto bad_prison;
  1277. }
  1278. pool->copier = dm_kcopyd_client_create();
  1279. if (IS_ERR(pool->copier)) {
  1280. r = PTR_ERR(pool->copier);
  1281. *error = "Error creating pool's kcopyd client";
  1282. err_p = ERR_PTR(r);
  1283. goto bad_kcopyd_client;
  1284. }
  1285. /*
  1286. * Create singlethreaded workqueue that will service all devices
  1287. * that use this metadata.
  1288. */
  1289. pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  1290. if (!pool->wq) {
  1291. *error = "Error creating pool's workqueue";
  1292. err_p = ERR_PTR(-ENOMEM);
  1293. goto bad_wq;
  1294. }
  1295. INIT_WORK(&pool->worker, do_worker);
  1296. INIT_DELAYED_WORK(&pool->waker, do_waker);
  1297. spin_lock_init(&pool->lock);
  1298. bio_list_init(&pool->deferred_bios);
  1299. bio_list_init(&pool->deferred_flush_bios);
  1300. INIT_LIST_HEAD(&pool->prepared_mappings);
  1301. pool->low_water_triggered = 0;
  1302. pool->no_free_space = 0;
  1303. bio_list_init(&pool->retry_on_resume_list);
  1304. ds_init(&pool->ds);
  1305. pool->next_mapping = NULL;
  1306. pool->mapping_pool =
  1307. mempool_create_kmalloc_pool(MAPPING_POOL_SIZE, sizeof(struct new_mapping));
  1308. if (!pool->mapping_pool) {
  1309. *error = "Error creating pool's mapping mempool";
  1310. err_p = ERR_PTR(-ENOMEM);
  1311. goto bad_mapping_pool;
  1312. }
  1313. pool->endio_hook_pool =
  1314. mempool_create_kmalloc_pool(ENDIO_HOOK_POOL_SIZE, sizeof(struct endio_hook));
  1315. if (!pool->endio_hook_pool) {
  1316. *error = "Error creating pool's endio_hook mempool";
  1317. err_p = ERR_PTR(-ENOMEM);
  1318. goto bad_endio_hook_pool;
  1319. }
  1320. pool->ref_count = 1;
  1321. pool->last_commit_jiffies = jiffies;
  1322. pool->pool_md = pool_md;
  1323. pool->md_dev = metadata_dev;
  1324. __pool_table_insert(pool);
  1325. return pool;
  1326. bad_endio_hook_pool:
  1327. mempool_destroy(pool->mapping_pool);
  1328. bad_mapping_pool:
  1329. destroy_workqueue(pool->wq);
  1330. bad_wq:
  1331. dm_kcopyd_client_destroy(pool->copier);
  1332. bad_kcopyd_client:
  1333. prison_destroy(pool->prison);
  1334. bad_prison:
  1335. kfree(pool);
  1336. bad_pool:
  1337. if (dm_pool_metadata_close(pmd))
  1338. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  1339. return err_p;
  1340. }
  1341. static void __pool_inc(struct pool *pool)
  1342. {
  1343. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  1344. pool->ref_count++;
  1345. }
  1346. static void __pool_dec(struct pool *pool)
  1347. {
  1348. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  1349. BUG_ON(!pool->ref_count);
  1350. if (!--pool->ref_count)
  1351. __pool_destroy(pool);
  1352. }
  1353. static struct pool *__pool_find(struct mapped_device *pool_md,
  1354. struct block_device *metadata_dev,
  1355. unsigned long block_size, char **error)
  1356. {
  1357. struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
  1358. if (pool) {
  1359. if (pool->pool_md != pool_md)
  1360. return ERR_PTR(-EBUSY);
  1361. __pool_inc(pool);
  1362. } else {
  1363. pool = __pool_table_lookup(pool_md);
  1364. if (pool) {
  1365. if (pool->md_dev != metadata_dev)
  1366. return ERR_PTR(-EINVAL);
  1367. __pool_inc(pool);
  1368. } else
  1369. pool = pool_create(pool_md, metadata_dev, block_size, error);
  1370. }
  1371. return pool;
  1372. }
  1373. /*----------------------------------------------------------------
  1374. * Pool target methods
  1375. *--------------------------------------------------------------*/
  1376. static void pool_dtr(struct dm_target *ti)
  1377. {
  1378. struct pool_c *pt = ti->private;
  1379. mutex_lock(&dm_thin_pool_table.mutex);
  1380. unbind_control_target(pt->pool, ti);
  1381. __pool_dec(pt->pool);
  1382. dm_put_device(ti, pt->metadata_dev);
  1383. dm_put_device(ti, pt->data_dev);
  1384. kfree(pt);
  1385. mutex_unlock(&dm_thin_pool_table.mutex);
  1386. }
  1387. struct pool_features {
  1388. unsigned zero_new_blocks:1;
  1389. };
  1390. static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
  1391. struct dm_target *ti)
  1392. {
  1393. int r;
  1394. unsigned argc;
  1395. const char *arg_name;
  1396. static struct dm_arg _args[] = {
  1397. {0, 1, "Invalid number of pool feature arguments"},
  1398. };
  1399. /*
  1400. * No feature arguments supplied.
  1401. */
  1402. if (!as->argc)
  1403. return 0;
  1404. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  1405. if (r)
  1406. return -EINVAL;
  1407. while (argc && !r) {
  1408. arg_name = dm_shift_arg(as);
  1409. argc--;
  1410. if (!strcasecmp(arg_name, "skip_block_zeroing")) {
  1411. pf->zero_new_blocks = 0;
  1412. continue;
  1413. }
  1414. ti->error = "Unrecognised pool feature requested";
  1415. r = -EINVAL;
  1416. }
  1417. return r;
  1418. }
  1419. /*
  1420. * thin-pool <metadata dev> <data dev>
  1421. * <data block size (sectors)>
  1422. * <low water mark (blocks)>
  1423. * [<#feature args> [<arg>]*]
  1424. *
  1425. * Optional feature arguments are:
  1426. * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
  1427. */
  1428. static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
  1429. {
  1430. int r;
  1431. struct pool_c *pt;
  1432. struct pool *pool;
  1433. struct pool_features pf;
  1434. struct dm_arg_set as;
  1435. struct dm_dev *data_dev;
  1436. unsigned long block_size;
  1437. dm_block_t low_water_blocks;
  1438. struct dm_dev *metadata_dev;
  1439. sector_t metadata_dev_size;
  1440. char b[BDEVNAME_SIZE];
  1441. /*
  1442. * FIXME Remove validation from scope of lock.
  1443. */
  1444. mutex_lock(&dm_thin_pool_table.mutex);
  1445. if (argc < 4) {
  1446. ti->error = "Invalid argument count";
  1447. r = -EINVAL;
  1448. goto out_unlock;
  1449. }
  1450. as.argc = argc;
  1451. as.argv = argv;
  1452. r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &metadata_dev);
  1453. if (r) {
  1454. ti->error = "Error opening metadata block device";
  1455. goto out_unlock;
  1456. }
  1457. metadata_dev_size = i_size_read(metadata_dev->bdev->bd_inode) >> SECTOR_SHIFT;
  1458. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
  1459. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  1460. bdevname(metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
  1461. r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
  1462. if (r) {
  1463. ti->error = "Error getting data device";
  1464. goto out_metadata;
  1465. }
  1466. if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
  1467. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  1468. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  1469. !is_power_of_2(block_size)) {
  1470. ti->error = "Invalid block size";
  1471. r = -EINVAL;
  1472. goto out;
  1473. }
  1474. if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
  1475. ti->error = "Invalid low water mark";
  1476. r = -EINVAL;
  1477. goto out;
  1478. }
  1479. /*
  1480. * Set default pool features.
  1481. */
  1482. memset(&pf, 0, sizeof(pf));
  1483. pf.zero_new_blocks = 1;
  1484. dm_consume_args(&as, 4);
  1485. r = parse_pool_features(&as, &pf, ti);
  1486. if (r)
  1487. goto out;
  1488. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  1489. if (!pt) {
  1490. r = -ENOMEM;
  1491. goto out;
  1492. }
  1493. pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
  1494. block_size, &ti->error);
  1495. if (IS_ERR(pool)) {
  1496. r = PTR_ERR(pool);
  1497. goto out_free_pt;
  1498. }
  1499. pt->pool = pool;
  1500. pt->ti = ti;
  1501. pt->metadata_dev = metadata_dev;
  1502. pt->data_dev = data_dev;
  1503. pt->low_water_blocks = low_water_blocks;
  1504. pt->zero_new_blocks = pf.zero_new_blocks;
  1505. ti->num_flush_requests = 1;
  1506. ti->num_discard_requests = 0;
  1507. ti->private = pt;
  1508. pt->callbacks.congested_fn = pool_is_congested;
  1509. dm_table_add_target_callbacks(ti->table, &pt->callbacks);
  1510. mutex_unlock(&dm_thin_pool_table.mutex);
  1511. return 0;
  1512. out_free_pt:
  1513. kfree(pt);
  1514. out:
  1515. dm_put_device(ti, data_dev);
  1516. out_metadata:
  1517. dm_put_device(ti, metadata_dev);
  1518. out_unlock:
  1519. mutex_unlock(&dm_thin_pool_table.mutex);
  1520. return r;
  1521. }
  1522. static int pool_map(struct dm_target *ti, struct bio *bio,
  1523. union map_info *map_context)
  1524. {
  1525. int r;
  1526. struct pool_c *pt = ti->private;
  1527. struct pool *pool = pt->pool;
  1528. unsigned long flags;
  1529. /*
  1530. * As this is a singleton target, ti->begin is always zero.
  1531. */
  1532. spin_lock_irqsave(&pool->lock, flags);
  1533. bio->bi_bdev = pt->data_dev->bdev;
  1534. r = DM_MAPIO_REMAPPED;
  1535. spin_unlock_irqrestore(&pool->lock, flags);
  1536. return r;
  1537. }
  1538. /*
  1539. * Retrieves the number of blocks of the data device from
  1540. * the superblock and compares it to the actual device size,
  1541. * thus resizing the data device in case it has grown.
  1542. *
  1543. * This both copes with opening preallocated data devices in the ctr
  1544. * being followed by a resume
  1545. * -and-
  1546. * calling the resume method individually after userspace has
  1547. * grown the data device in reaction to a table event.
  1548. */
  1549. static int pool_preresume(struct dm_target *ti)
  1550. {
  1551. int r;
  1552. struct pool_c *pt = ti->private;
  1553. struct pool *pool = pt->pool;
  1554. dm_block_t data_size, sb_data_size;
  1555. /*
  1556. * Take control of the pool object.
  1557. */
  1558. r = bind_control_target(pool, ti);
  1559. if (r)
  1560. return r;
  1561. data_size = ti->len >> pool->block_shift;
  1562. r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
  1563. if (r) {
  1564. DMERR("failed to retrieve data device size");
  1565. return r;
  1566. }
  1567. if (data_size < sb_data_size) {
  1568. DMERR("pool target too small, is %llu blocks (expected %llu)",
  1569. data_size, sb_data_size);
  1570. return -EINVAL;
  1571. } else if (data_size > sb_data_size) {
  1572. r = dm_pool_resize_data_dev(pool->pmd, data_size);
  1573. if (r) {
  1574. DMERR("failed to resize data device");
  1575. return r;
  1576. }
  1577. r = dm_pool_commit_metadata(pool->pmd);
  1578. if (r) {
  1579. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  1580. __func__, r);
  1581. return r;
  1582. }
  1583. }
  1584. return 0;
  1585. }
  1586. static void pool_resume(struct dm_target *ti)
  1587. {
  1588. struct pool_c *pt = ti->private;
  1589. struct pool *pool = pt->pool;
  1590. unsigned long flags;
  1591. spin_lock_irqsave(&pool->lock, flags);
  1592. pool->low_water_triggered = 0;
  1593. pool->no_free_space = 0;
  1594. __requeue_bios(pool);
  1595. spin_unlock_irqrestore(&pool->lock, flags);
  1596. do_waker(&pool->waker.work);
  1597. }
  1598. static void pool_postsuspend(struct dm_target *ti)
  1599. {
  1600. int r;
  1601. struct pool_c *pt = ti->private;
  1602. struct pool *pool = pt->pool;
  1603. cancel_delayed_work(&pool->waker);
  1604. flush_workqueue(pool->wq);
  1605. r = dm_pool_commit_metadata(pool->pmd);
  1606. if (r < 0) {
  1607. DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
  1608. __func__, r);
  1609. /* FIXME: invalidate device? error the next FUA or FLUSH bio ?*/
  1610. }
  1611. }
  1612. static int check_arg_count(unsigned argc, unsigned args_required)
  1613. {
  1614. if (argc != args_required) {
  1615. DMWARN("Message received with %u arguments instead of %u.",
  1616. argc, args_required);
  1617. return -EINVAL;
  1618. }
  1619. return 0;
  1620. }
  1621. static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
  1622. {
  1623. if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
  1624. *dev_id <= MAX_DEV_ID)
  1625. return 0;
  1626. if (warning)
  1627. DMWARN("Message received with invalid device id: %s", arg);
  1628. return -EINVAL;
  1629. }
  1630. static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
  1631. {
  1632. dm_thin_id dev_id;
  1633. int r;
  1634. r = check_arg_count(argc, 2);
  1635. if (r)
  1636. return r;
  1637. r = read_dev_id(argv[1], &dev_id, 1);
  1638. if (r)
  1639. return r;
  1640. r = dm_pool_create_thin(pool->pmd, dev_id);
  1641. if (r) {
  1642. DMWARN("Creation of new thinly-provisioned device with id %s failed.",
  1643. argv[1]);
  1644. return r;
  1645. }
  1646. return 0;
  1647. }
  1648. static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  1649. {
  1650. dm_thin_id dev_id;
  1651. dm_thin_id origin_dev_id;
  1652. int r;
  1653. r = check_arg_count(argc, 3);
  1654. if (r)
  1655. return r;
  1656. r = read_dev_id(argv[1], &dev_id, 1);
  1657. if (r)
  1658. return r;
  1659. r = read_dev_id(argv[2], &origin_dev_id, 1);
  1660. if (r)
  1661. return r;
  1662. r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
  1663. if (r) {
  1664. DMWARN("Creation of new snapshot %s of device %s failed.",
  1665. argv[1], argv[2]);
  1666. return r;
  1667. }
  1668. return 0;
  1669. }
  1670. static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
  1671. {
  1672. dm_thin_id dev_id;
  1673. int r;
  1674. r = check_arg_count(argc, 2);
  1675. if (r)
  1676. return r;
  1677. r = read_dev_id(argv[1], &dev_id, 1);
  1678. if (r)
  1679. return r;
  1680. r = dm_pool_delete_thin_device(pool->pmd, dev_id);
  1681. if (r)
  1682. DMWARN("Deletion of thin device %s failed.", argv[1]);
  1683. return r;
  1684. }
  1685. static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
  1686. {
  1687. dm_thin_id old_id, new_id;
  1688. int r;
  1689. r = check_arg_count(argc, 3);
  1690. if (r)
  1691. return r;
  1692. if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
  1693. DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
  1694. return -EINVAL;
  1695. }
  1696. if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
  1697. DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
  1698. return -EINVAL;
  1699. }
  1700. r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
  1701. if (r) {
  1702. DMWARN("Failed to change transaction id from %s to %s.",
  1703. argv[1], argv[2]);
  1704. return r;
  1705. }
  1706. return 0;
  1707. }
  1708. /*
  1709. * Messages supported:
  1710. * create_thin <dev_id>
  1711. * create_snap <dev_id> <origin_id>
  1712. * delete <dev_id>
  1713. * trim <dev_id> <new_size_in_sectors>
  1714. * set_transaction_id <current_trans_id> <new_trans_id>
  1715. */
  1716. static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
  1717. {
  1718. int r = -EINVAL;
  1719. struct pool_c *pt = ti->private;
  1720. struct pool *pool = pt->pool;
  1721. if (!strcasecmp(argv[0], "create_thin"))
  1722. r = process_create_thin_mesg(argc, argv, pool);
  1723. else if (!strcasecmp(argv[0], "create_snap"))
  1724. r = process_create_snap_mesg(argc, argv, pool);
  1725. else if (!strcasecmp(argv[0], "delete"))
  1726. r = process_delete_mesg(argc, argv, pool);
  1727. else if (!strcasecmp(argv[0], "set_transaction_id"))
  1728. r = process_set_transaction_id_mesg(argc, argv, pool);
  1729. else
  1730. DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
  1731. if (!r) {
  1732. r = dm_pool_commit_metadata(pool->pmd);
  1733. if (r)
  1734. DMERR("%s message: dm_pool_commit_metadata() failed, error = %d",
  1735. argv[0], r);
  1736. }
  1737. return r;
  1738. }
  1739. /*
  1740. * Status line is:
  1741. * <transaction id> <used metadata sectors>/<total metadata sectors>
  1742. * <used data sectors>/<total data sectors> <held metadata root>
  1743. */
  1744. static int pool_status(struct dm_target *ti, status_type_t type,
  1745. char *result, unsigned maxlen)
  1746. {
  1747. int r;
  1748. unsigned sz = 0;
  1749. uint64_t transaction_id;
  1750. dm_block_t nr_free_blocks_data;
  1751. dm_block_t nr_free_blocks_metadata;
  1752. dm_block_t nr_blocks_data;
  1753. dm_block_t nr_blocks_metadata;
  1754. dm_block_t held_root;
  1755. char buf[BDEVNAME_SIZE];
  1756. char buf2[BDEVNAME_SIZE];
  1757. struct pool_c *pt = ti->private;
  1758. struct pool *pool = pt->pool;
  1759. switch (type) {
  1760. case STATUSTYPE_INFO:
  1761. r = dm_pool_get_metadata_transaction_id(pool->pmd,
  1762. &transaction_id);
  1763. if (r)
  1764. return r;
  1765. r = dm_pool_get_free_metadata_block_count(pool->pmd,
  1766. &nr_free_blocks_metadata);
  1767. if (r)
  1768. return r;
  1769. r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
  1770. if (r)
  1771. return r;
  1772. r = dm_pool_get_free_block_count(pool->pmd,
  1773. &nr_free_blocks_data);
  1774. if (r)
  1775. return r;
  1776. r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
  1777. if (r)
  1778. return r;
  1779. r = dm_pool_get_held_metadata_root(pool->pmd, &held_root);
  1780. if (r)
  1781. return r;
  1782. DMEMIT("%llu %llu/%llu %llu/%llu ",
  1783. (unsigned long long)transaction_id,
  1784. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  1785. (unsigned long long)nr_blocks_metadata,
  1786. (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
  1787. (unsigned long long)nr_blocks_data);
  1788. if (held_root)
  1789. DMEMIT("%llu", held_root);
  1790. else
  1791. DMEMIT("-");
  1792. break;
  1793. case STATUSTYPE_TABLE:
  1794. DMEMIT("%s %s %lu %llu ",
  1795. format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
  1796. format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
  1797. (unsigned long)pool->sectors_per_block,
  1798. (unsigned long long)pt->low_water_blocks);
  1799. DMEMIT("%u ", !pool->zero_new_blocks);
  1800. if (!pool->zero_new_blocks)
  1801. DMEMIT("skip_block_zeroing ");
  1802. break;
  1803. }
  1804. return 0;
  1805. }
  1806. static int pool_iterate_devices(struct dm_target *ti,
  1807. iterate_devices_callout_fn fn, void *data)
  1808. {
  1809. struct pool_c *pt = ti->private;
  1810. return fn(ti, pt->data_dev, 0, ti->len, data);
  1811. }
  1812. static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  1813. struct bio_vec *biovec, int max_size)
  1814. {
  1815. struct pool_c *pt = ti->private;
  1816. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  1817. if (!q->merge_bvec_fn)
  1818. return max_size;
  1819. bvm->bi_bdev = pt->data_dev->bdev;
  1820. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  1821. }
  1822. static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
  1823. {
  1824. struct pool_c *pt = ti->private;
  1825. struct pool *pool = pt->pool;
  1826. blk_limits_io_min(limits, 0);
  1827. blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
  1828. }
  1829. static struct target_type pool_target = {
  1830. .name = "thin-pool",
  1831. .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
  1832. DM_TARGET_IMMUTABLE,
  1833. .version = {1, 0, 0},
  1834. .module = THIS_MODULE,
  1835. .ctr = pool_ctr,
  1836. .dtr = pool_dtr,
  1837. .map = pool_map,
  1838. .postsuspend = pool_postsuspend,
  1839. .preresume = pool_preresume,
  1840. .resume = pool_resume,
  1841. .message = pool_message,
  1842. .status = pool_status,
  1843. .merge = pool_merge,
  1844. .iterate_devices = pool_iterate_devices,
  1845. .io_hints = pool_io_hints,
  1846. };
  1847. /*----------------------------------------------------------------
  1848. * Thin target methods
  1849. *--------------------------------------------------------------*/
  1850. static void thin_dtr(struct dm_target *ti)
  1851. {
  1852. struct thin_c *tc = ti->private;
  1853. mutex_lock(&dm_thin_pool_table.mutex);
  1854. __pool_dec(tc->pool);
  1855. dm_pool_close_thin_device(tc->td);
  1856. dm_put_device(ti, tc->pool_dev);
  1857. kfree(tc);
  1858. mutex_unlock(&dm_thin_pool_table.mutex);
  1859. }
  1860. /*
  1861. * Thin target parameters:
  1862. *
  1863. * <pool_dev> <dev_id>
  1864. *
  1865. * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
  1866. * dev_id: the internal device identifier
  1867. */
  1868. static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
  1869. {
  1870. int r;
  1871. struct thin_c *tc;
  1872. struct dm_dev *pool_dev;
  1873. struct mapped_device *pool_md;
  1874. mutex_lock(&dm_thin_pool_table.mutex);
  1875. if (argc != 2) {
  1876. ti->error = "Invalid argument count";
  1877. r = -EINVAL;
  1878. goto out_unlock;
  1879. }
  1880. tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
  1881. if (!tc) {
  1882. ti->error = "Out of memory";
  1883. r = -ENOMEM;
  1884. goto out_unlock;
  1885. }
  1886. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
  1887. if (r) {
  1888. ti->error = "Error opening pool device";
  1889. goto bad_pool_dev;
  1890. }
  1891. tc->pool_dev = pool_dev;
  1892. if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
  1893. ti->error = "Invalid device id";
  1894. r = -EINVAL;
  1895. goto bad_common;
  1896. }
  1897. pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
  1898. if (!pool_md) {
  1899. ti->error = "Couldn't get pool mapped device";
  1900. r = -EINVAL;
  1901. goto bad_common;
  1902. }
  1903. tc->pool = __pool_table_lookup(pool_md);
  1904. if (!tc->pool) {
  1905. ti->error = "Couldn't find pool object";
  1906. r = -EINVAL;
  1907. goto bad_pool_lookup;
  1908. }
  1909. __pool_inc(tc->pool);
  1910. r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
  1911. if (r) {
  1912. ti->error = "Couldn't open thin internal device";
  1913. goto bad_thin_open;
  1914. }
  1915. ti->split_io = tc->pool->sectors_per_block;
  1916. ti->num_flush_requests = 1;
  1917. ti->num_discard_requests = 0;
  1918. ti->discards_supported = 0;
  1919. dm_put(pool_md);
  1920. mutex_unlock(&dm_thin_pool_table.mutex);
  1921. return 0;
  1922. bad_thin_open:
  1923. __pool_dec(tc->pool);
  1924. bad_pool_lookup:
  1925. dm_put(pool_md);
  1926. bad_common:
  1927. dm_put_device(ti, tc->pool_dev);
  1928. bad_pool_dev:
  1929. kfree(tc);
  1930. out_unlock:
  1931. mutex_unlock(&dm_thin_pool_table.mutex);
  1932. return r;
  1933. }
  1934. static int thin_map(struct dm_target *ti, struct bio *bio,
  1935. union map_info *map_context)
  1936. {
  1937. bio->bi_sector -= ti->begin;
  1938. return thin_bio_map(ti, bio, map_context);
  1939. }
  1940. static void thin_postsuspend(struct dm_target *ti)
  1941. {
  1942. if (dm_noflush_suspending(ti))
  1943. requeue_io((struct thin_c *)ti->private);
  1944. }
  1945. /*
  1946. * <nr mapped sectors> <highest mapped sector>
  1947. */
  1948. static int thin_status(struct dm_target *ti, status_type_t type,
  1949. char *result, unsigned maxlen)
  1950. {
  1951. int r;
  1952. ssize_t sz = 0;
  1953. dm_block_t mapped, highest;
  1954. char buf[BDEVNAME_SIZE];
  1955. struct thin_c *tc = ti->private;
  1956. if (!tc->td)
  1957. DMEMIT("-");
  1958. else {
  1959. switch (type) {
  1960. case STATUSTYPE_INFO:
  1961. r = dm_thin_get_mapped_count(tc->td, &mapped);
  1962. if (r)
  1963. return r;
  1964. r = dm_thin_get_highest_mapped_block(tc->td, &highest);
  1965. if (r < 0)
  1966. return r;
  1967. DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
  1968. if (r)
  1969. DMEMIT("%llu", ((highest + 1) *
  1970. tc->pool->sectors_per_block) - 1);
  1971. else
  1972. DMEMIT("-");
  1973. break;
  1974. case STATUSTYPE_TABLE:
  1975. DMEMIT("%s %lu",
  1976. format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
  1977. (unsigned long) tc->dev_id);
  1978. break;
  1979. }
  1980. }
  1981. return 0;
  1982. }
  1983. static int thin_iterate_devices(struct dm_target *ti,
  1984. iterate_devices_callout_fn fn, void *data)
  1985. {
  1986. dm_block_t blocks;
  1987. struct thin_c *tc = ti->private;
  1988. /*
  1989. * We can't call dm_pool_get_data_dev_size() since that blocks. So
  1990. * we follow a more convoluted path through to the pool's target.
  1991. */
  1992. if (!tc->pool->ti)
  1993. return 0; /* nothing is bound */
  1994. blocks = tc->pool->ti->len >> tc->pool->block_shift;
  1995. if (blocks)
  1996. return fn(ti, tc->pool_dev, 0, tc->pool->sectors_per_block * blocks, data);
  1997. return 0;
  1998. }
  1999. static void thin_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2000. {
  2001. struct thin_c *tc = ti->private;
  2002. blk_limits_io_min(limits, 0);
  2003. blk_limits_io_opt(limits, tc->pool->sectors_per_block << SECTOR_SHIFT);
  2004. }
  2005. static struct target_type thin_target = {
  2006. .name = "thin",
  2007. .version = {1, 0, 0},
  2008. .module = THIS_MODULE,
  2009. .ctr = thin_ctr,
  2010. .dtr = thin_dtr,
  2011. .map = thin_map,
  2012. .postsuspend = thin_postsuspend,
  2013. .status = thin_status,
  2014. .iterate_devices = thin_iterate_devices,
  2015. .io_hints = thin_io_hints,
  2016. };
  2017. /*----------------------------------------------------------------*/
  2018. static int __init dm_thin_init(void)
  2019. {
  2020. int r;
  2021. pool_table_init();
  2022. r = dm_register_target(&thin_target);
  2023. if (r)
  2024. return r;
  2025. r = dm_register_target(&pool_target);
  2026. if (r)
  2027. dm_unregister_target(&thin_target);
  2028. return r;
  2029. }
  2030. static void dm_thin_exit(void)
  2031. {
  2032. dm_unregister_target(&thin_target);
  2033. dm_unregister_target(&pool_target);
  2034. }
  2035. module_init(dm_thin_init);
  2036. module_exit(dm_thin_exit);
  2037. MODULE_DESCRIPTION(DM_NAME "device-mapper thin provisioning target");
  2038. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2039. MODULE_LICENSE("GPL");