dm-thin.c 59 KB

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