dm-thin.c 66 KB

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