dm-thin.c 70 KB

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