dm-thin.c 59 KB

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