dm-thin.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977
  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 for data device: 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. static bool is_factor(sector_t block_size, uint32_t n)
  1318. {
  1319. return !sector_div(block_size, n);
  1320. }
  1321. /*
  1322. * If discard_passdown was enabled verify that the data device
  1323. * supports discards. Disable discard_passdown if not.
  1324. */
  1325. static void disable_passdown_if_not_supported(struct pool_c *pt)
  1326. {
  1327. struct pool *pool = pt->pool;
  1328. struct block_device *data_bdev = pt->data_dev->bdev;
  1329. struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
  1330. sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
  1331. const char *reason = NULL;
  1332. char buf[BDEVNAME_SIZE];
  1333. if (!pt->adjusted_pf.discard_passdown)
  1334. return;
  1335. if (!data_dev_supports_discard(pt))
  1336. reason = "discard unsupported";
  1337. else if (data_limits->max_discard_sectors < pool->sectors_per_block)
  1338. reason = "max discard sectors smaller than a block";
  1339. else if (data_limits->discard_granularity > block_size)
  1340. reason = "discard granularity larger than a block";
  1341. else if (!is_factor(block_size, data_limits->discard_granularity))
  1342. reason = "discard granularity not a factor of block size";
  1343. if (reason) {
  1344. DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
  1345. pt->adjusted_pf.discard_passdown = false;
  1346. }
  1347. }
  1348. static int bind_control_target(struct pool *pool, struct dm_target *ti)
  1349. {
  1350. struct pool_c *pt = ti->private;
  1351. /*
  1352. * We want to make sure that degraded pools are never upgraded.
  1353. */
  1354. enum pool_mode old_mode = pool->pf.mode;
  1355. enum pool_mode new_mode = pt->adjusted_pf.mode;
  1356. if (old_mode > new_mode)
  1357. new_mode = old_mode;
  1358. pool->ti = ti;
  1359. pool->low_water_blocks = pt->low_water_blocks;
  1360. pool->pf = pt->adjusted_pf;
  1361. set_pool_mode(pool, new_mode);
  1362. return 0;
  1363. }
  1364. static void unbind_control_target(struct pool *pool, struct dm_target *ti)
  1365. {
  1366. if (pool->ti == ti)
  1367. pool->ti = NULL;
  1368. }
  1369. /*----------------------------------------------------------------
  1370. * Pool creation
  1371. *--------------------------------------------------------------*/
  1372. /* Initialize pool features. */
  1373. static void pool_features_init(struct pool_features *pf)
  1374. {
  1375. pf->mode = PM_WRITE;
  1376. pf->zero_new_blocks = true;
  1377. pf->discard_enabled = true;
  1378. pf->discard_passdown = true;
  1379. }
  1380. static void __pool_destroy(struct pool *pool)
  1381. {
  1382. __pool_table_remove(pool);
  1383. if (dm_pool_metadata_close(pool->pmd) < 0)
  1384. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  1385. dm_bio_prison_destroy(pool->prison);
  1386. dm_kcopyd_client_destroy(pool->copier);
  1387. if (pool->wq)
  1388. destroy_workqueue(pool->wq);
  1389. if (pool->next_mapping)
  1390. mempool_free(pool->next_mapping, pool->mapping_pool);
  1391. mempool_destroy(pool->mapping_pool);
  1392. dm_deferred_set_destroy(pool->shared_read_ds);
  1393. dm_deferred_set_destroy(pool->all_io_ds);
  1394. kfree(pool);
  1395. }
  1396. static struct kmem_cache *_new_mapping_cache;
  1397. static struct pool *pool_create(struct mapped_device *pool_md,
  1398. struct block_device *metadata_dev,
  1399. unsigned long block_size,
  1400. int read_only, char **error)
  1401. {
  1402. int r;
  1403. void *err_p;
  1404. struct pool *pool;
  1405. struct dm_pool_metadata *pmd;
  1406. bool format_device = read_only ? false : true;
  1407. pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
  1408. if (IS_ERR(pmd)) {
  1409. *error = "Error creating metadata object";
  1410. return (struct pool *)pmd;
  1411. }
  1412. pool = kmalloc(sizeof(*pool), GFP_KERNEL);
  1413. if (!pool) {
  1414. *error = "Error allocating memory for pool";
  1415. err_p = ERR_PTR(-ENOMEM);
  1416. goto bad_pool;
  1417. }
  1418. pool->pmd = pmd;
  1419. pool->sectors_per_block = block_size;
  1420. if (block_size & (block_size - 1))
  1421. pool->sectors_per_block_shift = -1;
  1422. else
  1423. pool->sectors_per_block_shift = __ffs(block_size);
  1424. pool->low_water_blocks = 0;
  1425. pool_features_init(&pool->pf);
  1426. pool->prison = dm_bio_prison_create(PRISON_CELLS);
  1427. if (!pool->prison) {
  1428. *error = "Error creating pool's bio prison";
  1429. err_p = ERR_PTR(-ENOMEM);
  1430. goto bad_prison;
  1431. }
  1432. pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  1433. if (IS_ERR(pool->copier)) {
  1434. r = PTR_ERR(pool->copier);
  1435. *error = "Error creating pool's kcopyd client";
  1436. err_p = ERR_PTR(r);
  1437. goto bad_kcopyd_client;
  1438. }
  1439. /*
  1440. * Create singlethreaded workqueue that will service all devices
  1441. * that use this metadata.
  1442. */
  1443. pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  1444. if (!pool->wq) {
  1445. *error = "Error creating pool's workqueue";
  1446. err_p = ERR_PTR(-ENOMEM);
  1447. goto bad_wq;
  1448. }
  1449. INIT_WORK(&pool->worker, do_worker);
  1450. INIT_DELAYED_WORK(&pool->waker, do_waker);
  1451. spin_lock_init(&pool->lock);
  1452. bio_list_init(&pool->deferred_bios);
  1453. bio_list_init(&pool->deferred_flush_bios);
  1454. INIT_LIST_HEAD(&pool->prepared_mappings);
  1455. INIT_LIST_HEAD(&pool->prepared_discards);
  1456. pool->low_water_triggered = 0;
  1457. pool->no_free_space = 0;
  1458. bio_list_init(&pool->retry_on_resume_list);
  1459. pool->shared_read_ds = dm_deferred_set_create();
  1460. if (!pool->shared_read_ds) {
  1461. *error = "Error creating pool's shared read deferred set";
  1462. err_p = ERR_PTR(-ENOMEM);
  1463. goto bad_shared_read_ds;
  1464. }
  1465. pool->all_io_ds = dm_deferred_set_create();
  1466. if (!pool->all_io_ds) {
  1467. *error = "Error creating pool's all io deferred set";
  1468. err_p = ERR_PTR(-ENOMEM);
  1469. goto bad_all_io_ds;
  1470. }
  1471. pool->next_mapping = NULL;
  1472. pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
  1473. _new_mapping_cache);
  1474. if (!pool->mapping_pool) {
  1475. *error = "Error creating pool's mapping mempool";
  1476. err_p = ERR_PTR(-ENOMEM);
  1477. goto bad_mapping_pool;
  1478. }
  1479. pool->ref_count = 1;
  1480. pool->last_commit_jiffies = jiffies;
  1481. pool->pool_md = pool_md;
  1482. pool->md_dev = metadata_dev;
  1483. __pool_table_insert(pool);
  1484. return pool;
  1485. bad_mapping_pool:
  1486. dm_deferred_set_destroy(pool->all_io_ds);
  1487. bad_all_io_ds:
  1488. dm_deferred_set_destroy(pool->shared_read_ds);
  1489. bad_shared_read_ds:
  1490. destroy_workqueue(pool->wq);
  1491. bad_wq:
  1492. dm_kcopyd_client_destroy(pool->copier);
  1493. bad_kcopyd_client:
  1494. dm_bio_prison_destroy(pool->prison);
  1495. bad_prison:
  1496. kfree(pool);
  1497. bad_pool:
  1498. if (dm_pool_metadata_close(pmd))
  1499. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  1500. return err_p;
  1501. }
  1502. static void __pool_inc(struct pool *pool)
  1503. {
  1504. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  1505. pool->ref_count++;
  1506. }
  1507. static void __pool_dec(struct pool *pool)
  1508. {
  1509. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  1510. BUG_ON(!pool->ref_count);
  1511. if (!--pool->ref_count)
  1512. __pool_destroy(pool);
  1513. }
  1514. static struct pool *__pool_find(struct mapped_device *pool_md,
  1515. struct block_device *metadata_dev,
  1516. unsigned long block_size, int read_only,
  1517. char **error, int *created)
  1518. {
  1519. struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
  1520. if (pool) {
  1521. if (pool->pool_md != pool_md) {
  1522. *error = "metadata device already in use by a pool";
  1523. return ERR_PTR(-EBUSY);
  1524. }
  1525. __pool_inc(pool);
  1526. } else {
  1527. pool = __pool_table_lookup(pool_md);
  1528. if (pool) {
  1529. if (pool->md_dev != metadata_dev) {
  1530. *error = "different pool cannot replace a pool";
  1531. return ERR_PTR(-EINVAL);
  1532. }
  1533. __pool_inc(pool);
  1534. } else {
  1535. pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
  1536. *created = 1;
  1537. }
  1538. }
  1539. return pool;
  1540. }
  1541. /*----------------------------------------------------------------
  1542. * Pool target methods
  1543. *--------------------------------------------------------------*/
  1544. static void pool_dtr(struct dm_target *ti)
  1545. {
  1546. struct pool_c *pt = ti->private;
  1547. mutex_lock(&dm_thin_pool_table.mutex);
  1548. unbind_control_target(pt->pool, ti);
  1549. __pool_dec(pt->pool);
  1550. dm_put_device(ti, pt->metadata_dev);
  1551. dm_put_device(ti, pt->data_dev);
  1552. kfree(pt);
  1553. mutex_unlock(&dm_thin_pool_table.mutex);
  1554. }
  1555. static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
  1556. struct dm_target *ti)
  1557. {
  1558. int r;
  1559. unsigned argc;
  1560. const char *arg_name;
  1561. static struct dm_arg _args[] = {
  1562. {0, 3, "Invalid number of pool feature arguments"},
  1563. };
  1564. /*
  1565. * No feature arguments supplied.
  1566. */
  1567. if (!as->argc)
  1568. return 0;
  1569. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  1570. if (r)
  1571. return -EINVAL;
  1572. while (argc && !r) {
  1573. arg_name = dm_shift_arg(as);
  1574. argc--;
  1575. if (!strcasecmp(arg_name, "skip_block_zeroing"))
  1576. pf->zero_new_blocks = false;
  1577. else if (!strcasecmp(arg_name, "ignore_discard"))
  1578. pf->discard_enabled = false;
  1579. else if (!strcasecmp(arg_name, "no_discard_passdown"))
  1580. pf->discard_passdown = false;
  1581. else if (!strcasecmp(arg_name, "read_only"))
  1582. pf->mode = PM_READ_ONLY;
  1583. else {
  1584. ti->error = "Unrecognised pool feature requested";
  1585. r = -EINVAL;
  1586. break;
  1587. }
  1588. }
  1589. return r;
  1590. }
  1591. static sector_t get_metadata_dev_size(struct block_device *bdev)
  1592. {
  1593. sector_t metadata_dev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  1594. char buffer[BDEVNAME_SIZE];
  1595. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING) {
  1596. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  1597. bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
  1598. metadata_dev_size = THIN_METADATA_MAX_SECTORS_WARNING;
  1599. }
  1600. return metadata_dev_size;
  1601. }
  1602. static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
  1603. {
  1604. sector_t metadata_dev_size = get_metadata_dev_size(bdev);
  1605. sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
  1606. return metadata_dev_size;
  1607. }
  1608. /*
  1609. * thin-pool <metadata dev> <data dev>
  1610. * <data block size (sectors)>
  1611. * <low water mark (blocks)>
  1612. * [<#feature args> [<arg>]*]
  1613. *
  1614. * Optional feature arguments are:
  1615. * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
  1616. * ignore_discard: disable discard
  1617. * no_discard_passdown: don't pass discards down to the data device
  1618. */
  1619. static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
  1620. {
  1621. int r, pool_created = 0;
  1622. struct pool_c *pt;
  1623. struct pool *pool;
  1624. struct pool_features pf;
  1625. struct dm_arg_set as;
  1626. struct dm_dev *data_dev;
  1627. unsigned long block_size;
  1628. dm_block_t low_water_blocks;
  1629. struct dm_dev *metadata_dev;
  1630. fmode_t metadata_mode;
  1631. /*
  1632. * FIXME Remove validation from scope of lock.
  1633. */
  1634. mutex_lock(&dm_thin_pool_table.mutex);
  1635. if (argc < 4) {
  1636. ti->error = "Invalid argument count";
  1637. r = -EINVAL;
  1638. goto out_unlock;
  1639. }
  1640. as.argc = argc;
  1641. as.argv = argv;
  1642. /*
  1643. * Set default pool features.
  1644. */
  1645. pool_features_init(&pf);
  1646. dm_consume_args(&as, 4);
  1647. r = parse_pool_features(&as, &pf, ti);
  1648. if (r)
  1649. goto out_unlock;
  1650. metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
  1651. r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
  1652. if (r) {
  1653. ti->error = "Error opening metadata block device";
  1654. goto out_unlock;
  1655. }
  1656. /*
  1657. * Run for the side-effect of possibly issuing a warning if the
  1658. * device is too big.
  1659. */
  1660. (void) get_metadata_dev_size(metadata_dev->bdev);
  1661. r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
  1662. if (r) {
  1663. ti->error = "Error getting data device";
  1664. goto out_metadata;
  1665. }
  1666. if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
  1667. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  1668. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  1669. block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
  1670. ti->error = "Invalid block size";
  1671. r = -EINVAL;
  1672. goto out;
  1673. }
  1674. if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
  1675. ti->error = "Invalid low water mark";
  1676. r = -EINVAL;
  1677. goto out;
  1678. }
  1679. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  1680. if (!pt) {
  1681. r = -ENOMEM;
  1682. goto out;
  1683. }
  1684. pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
  1685. block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
  1686. if (IS_ERR(pool)) {
  1687. r = PTR_ERR(pool);
  1688. goto out_free_pt;
  1689. }
  1690. /*
  1691. * 'pool_created' reflects whether this is the first table load.
  1692. * Top level discard support is not allowed to be changed after
  1693. * initial load. This would require a pool reload to trigger thin
  1694. * device changes.
  1695. */
  1696. if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
  1697. ti->error = "Discard support cannot be disabled once enabled";
  1698. r = -EINVAL;
  1699. goto out_flags_changed;
  1700. }
  1701. pt->pool = pool;
  1702. pt->ti = ti;
  1703. pt->metadata_dev = metadata_dev;
  1704. pt->data_dev = data_dev;
  1705. pt->low_water_blocks = low_water_blocks;
  1706. pt->adjusted_pf = pt->requested_pf = pf;
  1707. ti->num_flush_bios = 1;
  1708. /*
  1709. * Only need to enable discards if the pool should pass
  1710. * them down to the data device. The thin device's discard
  1711. * processing will cause mappings to be removed from the btree.
  1712. */
  1713. if (pf.discard_enabled && pf.discard_passdown) {
  1714. ti->num_discard_bios = 1;
  1715. /*
  1716. * Setting 'discards_supported' circumvents the normal
  1717. * stacking of discard limits (this keeps the pool and
  1718. * thin devices' discard limits consistent).
  1719. */
  1720. ti->discards_supported = true;
  1721. ti->discard_zeroes_data_unsupported = true;
  1722. }
  1723. ti->private = pt;
  1724. pt->callbacks.congested_fn = pool_is_congested;
  1725. dm_table_add_target_callbacks(ti->table, &pt->callbacks);
  1726. mutex_unlock(&dm_thin_pool_table.mutex);
  1727. return 0;
  1728. out_flags_changed:
  1729. __pool_dec(pool);
  1730. out_free_pt:
  1731. kfree(pt);
  1732. out:
  1733. dm_put_device(ti, data_dev);
  1734. out_metadata:
  1735. dm_put_device(ti, metadata_dev);
  1736. out_unlock:
  1737. mutex_unlock(&dm_thin_pool_table.mutex);
  1738. return r;
  1739. }
  1740. static int pool_map(struct dm_target *ti, struct bio *bio)
  1741. {
  1742. int r;
  1743. struct pool_c *pt = ti->private;
  1744. struct pool *pool = pt->pool;
  1745. unsigned long flags;
  1746. /*
  1747. * As this is a singleton target, ti->begin is always zero.
  1748. */
  1749. spin_lock_irqsave(&pool->lock, flags);
  1750. bio->bi_bdev = pt->data_dev->bdev;
  1751. r = DM_MAPIO_REMAPPED;
  1752. spin_unlock_irqrestore(&pool->lock, flags);
  1753. return r;
  1754. }
  1755. static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
  1756. {
  1757. int r;
  1758. struct pool_c *pt = ti->private;
  1759. struct pool *pool = pt->pool;
  1760. sector_t data_size = ti->len;
  1761. dm_block_t sb_data_size;
  1762. *need_commit = false;
  1763. (void) sector_div(data_size, pool->sectors_per_block);
  1764. r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
  1765. if (r) {
  1766. DMERR("failed to retrieve data device size");
  1767. return r;
  1768. }
  1769. if (data_size < sb_data_size) {
  1770. DMERR("pool target (%llu blocks) too small: expected %llu",
  1771. (unsigned long long)data_size, sb_data_size);
  1772. return -EINVAL;
  1773. } else if (data_size > sb_data_size) {
  1774. r = dm_pool_resize_data_dev(pool->pmd, data_size);
  1775. if (r) {
  1776. DMERR("failed to resize data device");
  1777. set_pool_mode(pool, PM_READ_ONLY);
  1778. return r;
  1779. }
  1780. *need_commit = true;
  1781. }
  1782. return 0;
  1783. }
  1784. static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
  1785. {
  1786. int r;
  1787. struct pool_c *pt = ti->private;
  1788. struct pool *pool = pt->pool;
  1789. dm_block_t metadata_dev_size, sb_metadata_dev_size;
  1790. *need_commit = false;
  1791. metadata_dev_size = get_metadata_dev_size(pool->md_dev);
  1792. r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
  1793. if (r) {
  1794. DMERR("failed to retrieve data device size");
  1795. return r;
  1796. }
  1797. if (metadata_dev_size < sb_metadata_dev_size) {
  1798. DMERR("metadata device (%llu sectors) too small: expected %llu",
  1799. metadata_dev_size, sb_metadata_dev_size);
  1800. return -EINVAL;
  1801. } else if (metadata_dev_size > sb_metadata_dev_size) {
  1802. r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
  1803. if (r) {
  1804. DMERR("failed to resize metadata device");
  1805. return r;
  1806. }
  1807. *need_commit = true;
  1808. }
  1809. return 0;
  1810. }
  1811. /*
  1812. * Retrieves the number of blocks of the data device from
  1813. * the superblock and compares it to the actual device size,
  1814. * thus resizing the data device in case it has grown.
  1815. *
  1816. * This both copes with opening preallocated data devices in the ctr
  1817. * being followed by a resume
  1818. * -and-
  1819. * calling the resume method individually after userspace has
  1820. * grown the data device in reaction to a table event.
  1821. */
  1822. static int pool_preresume(struct dm_target *ti)
  1823. {
  1824. int r;
  1825. bool need_commit1, need_commit2;
  1826. struct pool_c *pt = ti->private;
  1827. struct pool *pool = pt->pool;
  1828. /*
  1829. * Take control of the pool object.
  1830. */
  1831. r = bind_control_target(pool, ti);
  1832. if (r)
  1833. return r;
  1834. r = maybe_resize_data_dev(ti, &need_commit1);
  1835. if (r)
  1836. return r;
  1837. r = maybe_resize_metadata_dev(ti, &need_commit2);
  1838. if (r)
  1839. return r;
  1840. if (need_commit1 || need_commit2)
  1841. (void) commit_or_fallback(pool);
  1842. return 0;
  1843. }
  1844. static void pool_resume(struct dm_target *ti)
  1845. {
  1846. struct pool_c *pt = ti->private;
  1847. struct pool *pool = pt->pool;
  1848. unsigned long flags;
  1849. spin_lock_irqsave(&pool->lock, flags);
  1850. pool->low_water_triggered = 0;
  1851. pool->no_free_space = 0;
  1852. __requeue_bios(pool);
  1853. spin_unlock_irqrestore(&pool->lock, flags);
  1854. do_waker(&pool->waker.work);
  1855. }
  1856. static void pool_postsuspend(struct dm_target *ti)
  1857. {
  1858. struct pool_c *pt = ti->private;
  1859. struct pool *pool = pt->pool;
  1860. cancel_delayed_work(&pool->waker);
  1861. flush_workqueue(pool->wq);
  1862. (void) commit_or_fallback(pool);
  1863. }
  1864. static int check_arg_count(unsigned argc, unsigned args_required)
  1865. {
  1866. if (argc != args_required) {
  1867. DMWARN("Message received with %u arguments instead of %u.",
  1868. argc, args_required);
  1869. return -EINVAL;
  1870. }
  1871. return 0;
  1872. }
  1873. static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
  1874. {
  1875. if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
  1876. *dev_id <= MAX_DEV_ID)
  1877. return 0;
  1878. if (warning)
  1879. DMWARN("Message received with invalid device id: %s", arg);
  1880. return -EINVAL;
  1881. }
  1882. static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
  1883. {
  1884. dm_thin_id dev_id;
  1885. int r;
  1886. r = check_arg_count(argc, 2);
  1887. if (r)
  1888. return r;
  1889. r = read_dev_id(argv[1], &dev_id, 1);
  1890. if (r)
  1891. return r;
  1892. r = dm_pool_create_thin(pool->pmd, dev_id);
  1893. if (r) {
  1894. DMWARN("Creation of new thinly-provisioned device with id %s failed.",
  1895. argv[1]);
  1896. return r;
  1897. }
  1898. return 0;
  1899. }
  1900. static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  1901. {
  1902. dm_thin_id dev_id;
  1903. dm_thin_id origin_dev_id;
  1904. int r;
  1905. r = check_arg_count(argc, 3);
  1906. if (r)
  1907. return r;
  1908. r = read_dev_id(argv[1], &dev_id, 1);
  1909. if (r)
  1910. return r;
  1911. r = read_dev_id(argv[2], &origin_dev_id, 1);
  1912. if (r)
  1913. return r;
  1914. r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
  1915. if (r) {
  1916. DMWARN("Creation of new snapshot %s of device %s failed.",
  1917. argv[1], argv[2]);
  1918. return r;
  1919. }
  1920. return 0;
  1921. }
  1922. static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
  1923. {
  1924. dm_thin_id dev_id;
  1925. int r;
  1926. r = check_arg_count(argc, 2);
  1927. if (r)
  1928. return r;
  1929. r = read_dev_id(argv[1], &dev_id, 1);
  1930. if (r)
  1931. return r;
  1932. r = dm_pool_delete_thin_device(pool->pmd, dev_id);
  1933. if (r)
  1934. DMWARN("Deletion of thin device %s failed.", argv[1]);
  1935. return r;
  1936. }
  1937. static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
  1938. {
  1939. dm_thin_id old_id, new_id;
  1940. int r;
  1941. r = check_arg_count(argc, 3);
  1942. if (r)
  1943. return r;
  1944. if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
  1945. DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
  1946. return -EINVAL;
  1947. }
  1948. if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
  1949. DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
  1950. return -EINVAL;
  1951. }
  1952. r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
  1953. if (r) {
  1954. DMWARN("Failed to change transaction id from %s to %s.",
  1955. argv[1], argv[2]);
  1956. return r;
  1957. }
  1958. return 0;
  1959. }
  1960. static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  1961. {
  1962. int r;
  1963. r = check_arg_count(argc, 1);
  1964. if (r)
  1965. return r;
  1966. (void) commit_or_fallback(pool);
  1967. r = dm_pool_reserve_metadata_snap(pool->pmd);
  1968. if (r)
  1969. DMWARN("reserve_metadata_snap message failed.");
  1970. return r;
  1971. }
  1972. static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  1973. {
  1974. int r;
  1975. r = check_arg_count(argc, 1);
  1976. if (r)
  1977. return r;
  1978. r = dm_pool_release_metadata_snap(pool->pmd);
  1979. if (r)
  1980. DMWARN("release_metadata_snap message failed.");
  1981. return r;
  1982. }
  1983. /*
  1984. * Messages supported:
  1985. * create_thin <dev_id>
  1986. * create_snap <dev_id> <origin_id>
  1987. * delete <dev_id>
  1988. * trim <dev_id> <new_size_in_sectors>
  1989. * set_transaction_id <current_trans_id> <new_trans_id>
  1990. * reserve_metadata_snap
  1991. * release_metadata_snap
  1992. */
  1993. static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
  1994. {
  1995. int r = -EINVAL;
  1996. struct pool_c *pt = ti->private;
  1997. struct pool *pool = pt->pool;
  1998. if (!strcasecmp(argv[0], "create_thin"))
  1999. r = process_create_thin_mesg(argc, argv, pool);
  2000. else if (!strcasecmp(argv[0], "create_snap"))
  2001. r = process_create_snap_mesg(argc, argv, pool);
  2002. else if (!strcasecmp(argv[0], "delete"))
  2003. r = process_delete_mesg(argc, argv, pool);
  2004. else if (!strcasecmp(argv[0], "set_transaction_id"))
  2005. r = process_set_transaction_id_mesg(argc, argv, pool);
  2006. else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
  2007. r = process_reserve_metadata_snap_mesg(argc, argv, pool);
  2008. else if (!strcasecmp(argv[0], "release_metadata_snap"))
  2009. r = process_release_metadata_snap_mesg(argc, argv, pool);
  2010. else
  2011. DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
  2012. if (!r)
  2013. (void) commit_or_fallback(pool);
  2014. return r;
  2015. }
  2016. static void emit_flags(struct pool_features *pf, char *result,
  2017. unsigned sz, unsigned maxlen)
  2018. {
  2019. unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
  2020. !pf->discard_passdown + (pf->mode == PM_READ_ONLY);
  2021. DMEMIT("%u ", count);
  2022. if (!pf->zero_new_blocks)
  2023. DMEMIT("skip_block_zeroing ");
  2024. if (!pf->discard_enabled)
  2025. DMEMIT("ignore_discard ");
  2026. if (!pf->discard_passdown)
  2027. DMEMIT("no_discard_passdown ");
  2028. if (pf->mode == PM_READ_ONLY)
  2029. DMEMIT("read_only ");
  2030. }
  2031. /*
  2032. * Status line is:
  2033. * <transaction id> <used metadata sectors>/<total metadata sectors>
  2034. * <used data sectors>/<total data sectors> <held metadata root>
  2035. */
  2036. static void pool_status(struct dm_target *ti, status_type_t type,
  2037. unsigned status_flags, char *result, unsigned maxlen)
  2038. {
  2039. int r;
  2040. unsigned sz = 0;
  2041. uint64_t transaction_id;
  2042. dm_block_t nr_free_blocks_data;
  2043. dm_block_t nr_free_blocks_metadata;
  2044. dm_block_t nr_blocks_data;
  2045. dm_block_t nr_blocks_metadata;
  2046. dm_block_t held_root;
  2047. char buf[BDEVNAME_SIZE];
  2048. char buf2[BDEVNAME_SIZE];
  2049. struct pool_c *pt = ti->private;
  2050. struct pool *pool = pt->pool;
  2051. switch (type) {
  2052. case STATUSTYPE_INFO:
  2053. if (get_pool_mode(pool) == PM_FAIL) {
  2054. DMEMIT("Fail");
  2055. break;
  2056. }
  2057. /* Commit to ensure statistics aren't out-of-date */
  2058. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
  2059. (void) commit_or_fallback(pool);
  2060. r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
  2061. if (r) {
  2062. DMERR("dm_pool_get_metadata_transaction_id returned %d", r);
  2063. goto err;
  2064. }
  2065. r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
  2066. if (r) {
  2067. DMERR("dm_pool_get_free_metadata_block_count returned %d", r);
  2068. goto err;
  2069. }
  2070. r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
  2071. if (r) {
  2072. DMERR("dm_pool_get_metadata_dev_size returned %d", r);
  2073. goto err;
  2074. }
  2075. r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
  2076. if (r) {
  2077. DMERR("dm_pool_get_free_block_count returned %d", r);
  2078. goto err;
  2079. }
  2080. r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
  2081. if (r) {
  2082. DMERR("dm_pool_get_data_dev_size returned %d", r);
  2083. goto err;
  2084. }
  2085. r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
  2086. if (r) {
  2087. DMERR("dm_pool_get_metadata_snap returned %d", r);
  2088. goto err;
  2089. }
  2090. DMEMIT("%llu %llu/%llu %llu/%llu ",
  2091. (unsigned long long)transaction_id,
  2092. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  2093. (unsigned long long)nr_blocks_metadata,
  2094. (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
  2095. (unsigned long long)nr_blocks_data);
  2096. if (held_root)
  2097. DMEMIT("%llu ", held_root);
  2098. else
  2099. DMEMIT("- ");
  2100. if (pool->pf.mode == PM_READ_ONLY)
  2101. DMEMIT("ro ");
  2102. else
  2103. DMEMIT("rw ");
  2104. if (!pool->pf.discard_enabled)
  2105. DMEMIT("ignore_discard");
  2106. else if (pool->pf.discard_passdown)
  2107. DMEMIT("discard_passdown");
  2108. else
  2109. DMEMIT("no_discard_passdown");
  2110. break;
  2111. case STATUSTYPE_TABLE:
  2112. DMEMIT("%s %s %lu %llu ",
  2113. format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
  2114. format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
  2115. (unsigned long)pool->sectors_per_block,
  2116. (unsigned long long)pt->low_water_blocks);
  2117. emit_flags(&pt->requested_pf, result, sz, maxlen);
  2118. break;
  2119. }
  2120. return;
  2121. err:
  2122. DMEMIT("Error");
  2123. }
  2124. static int pool_iterate_devices(struct dm_target *ti,
  2125. iterate_devices_callout_fn fn, void *data)
  2126. {
  2127. struct pool_c *pt = ti->private;
  2128. return fn(ti, pt->data_dev, 0, ti->len, data);
  2129. }
  2130. static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  2131. struct bio_vec *biovec, int max_size)
  2132. {
  2133. struct pool_c *pt = ti->private;
  2134. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  2135. if (!q->merge_bvec_fn)
  2136. return max_size;
  2137. bvm->bi_bdev = pt->data_dev->bdev;
  2138. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  2139. }
  2140. static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
  2141. {
  2142. struct pool *pool = pt->pool;
  2143. struct queue_limits *data_limits;
  2144. limits->max_discard_sectors = pool->sectors_per_block;
  2145. /*
  2146. * discard_granularity is just a hint, and not enforced.
  2147. */
  2148. if (pt->adjusted_pf.discard_passdown) {
  2149. data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
  2150. limits->discard_granularity = data_limits->discard_granularity;
  2151. } else
  2152. limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
  2153. }
  2154. static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2155. {
  2156. struct pool_c *pt = ti->private;
  2157. struct pool *pool = pt->pool;
  2158. blk_limits_io_min(limits, 0);
  2159. blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
  2160. /*
  2161. * pt->adjusted_pf is a staging area for the actual features to use.
  2162. * They get transferred to the live pool in bind_control_target()
  2163. * called from pool_preresume().
  2164. */
  2165. if (!pt->adjusted_pf.discard_enabled)
  2166. return;
  2167. disable_passdown_if_not_supported(pt);
  2168. set_discard_limits(pt, limits);
  2169. }
  2170. static struct target_type pool_target = {
  2171. .name = "thin-pool",
  2172. .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
  2173. DM_TARGET_IMMUTABLE,
  2174. .version = {1, 8, 0},
  2175. .module = THIS_MODULE,
  2176. .ctr = pool_ctr,
  2177. .dtr = pool_dtr,
  2178. .map = pool_map,
  2179. .postsuspend = pool_postsuspend,
  2180. .preresume = pool_preresume,
  2181. .resume = pool_resume,
  2182. .message = pool_message,
  2183. .status = pool_status,
  2184. .merge = pool_merge,
  2185. .iterate_devices = pool_iterate_devices,
  2186. .io_hints = pool_io_hints,
  2187. };
  2188. /*----------------------------------------------------------------
  2189. * Thin target methods
  2190. *--------------------------------------------------------------*/
  2191. static void thin_dtr(struct dm_target *ti)
  2192. {
  2193. struct thin_c *tc = ti->private;
  2194. mutex_lock(&dm_thin_pool_table.mutex);
  2195. __pool_dec(tc->pool);
  2196. dm_pool_close_thin_device(tc->td);
  2197. dm_put_device(ti, tc->pool_dev);
  2198. if (tc->origin_dev)
  2199. dm_put_device(ti, tc->origin_dev);
  2200. kfree(tc);
  2201. mutex_unlock(&dm_thin_pool_table.mutex);
  2202. }
  2203. /*
  2204. * Thin target parameters:
  2205. *
  2206. * <pool_dev> <dev_id> [origin_dev]
  2207. *
  2208. * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
  2209. * dev_id: the internal device identifier
  2210. * origin_dev: a device external to the pool that should act as the origin
  2211. *
  2212. * If the pool device has discards disabled, they get disabled for the thin
  2213. * device as well.
  2214. */
  2215. static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2216. {
  2217. int r;
  2218. struct thin_c *tc;
  2219. struct dm_dev *pool_dev, *origin_dev;
  2220. struct mapped_device *pool_md;
  2221. mutex_lock(&dm_thin_pool_table.mutex);
  2222. if (argc != 2 && argc != 3) {
  2223. ti->error = "Invalid argument count";
  2224. r = -EINVAL;
  2225. goto out_unlock;
  2226. }
  2227. tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
  2228. if (!tc) {
  2229. ti->error = "Out of memory";
  2230. r = -ENOMEM;
  2231. goto out_unlock;
  2232. }
  2233. if (argc == 3) {
  2234. r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
  2235. if (r) {
  2236. ti->error = "Error opening origin device";
  2237. goto bad_origin_dev;
  2238. }
  2239. tc->origin_dev = origin_dev;
  2240. }
  2241. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
  2242. if (r) {
  2243. ti->error = "Error opening pool device";
  2244. goto bad_pool_dev;
  2245. }
  2246. tc->pool_dev = pool_dev;
  2247. if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
  2248. ti->error = "Invalid device id";
  2249. r = -EINVAL;
  2250. goto bad_common;
  2251. }
  2252. pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
  2253. if (!pool_md) {
  2254. ti->error = "Couldn't get pool mapped device";
  2255. r = -EINVAL;
  2256. goto bad_common;
  2257. }
  2258. tc->pool = __pool_table_lookup(pool_md);
  2259. if (!tc->pool) {
  2260. ti->error = "Couldn't find pool object";
  2261. r = -EINVAL;
  2262. goto bad_pool_lookup;
  2263. }
  2264. __pool_inc(tc->pool);
  2265. if (get_pool_mode(tc->pool) == PM_FAIL) {
  2266. ti->error = "Couldn't open thin device, Pool is in fail mode";
  2267. goto bad_thin_open;
  2268. }
  2269. r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
  2270. if (r) {
  2271. ti->error = "Couldn't open thin internal device";
  2272. goto bad_thin_open;
  2273. }
  2274. r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
  2275. if (r)
  2276. goto bad_thin_open;
  2277. ti->num_flush_bios = 1;
  2278. ti->flush_supported = true;
  2279. ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
  2280. /* In case the pool supports discards, pass them on. */
  2281. if (tc->pool->pf.discard_enabled) {
  2282. ti->discards_supported = true;
  2283. ti->num_discard_bios = 1;
  2284. ti->discard_zeroes_data_unsupported = true;
  2285. /* Discard bios must be split on a block boundary */
  2286. ti->split_discard_bios = true;
  2287. }
  2288. dm_put(pool_md);
  2289. mutex_unlock(&dm_thin_pool_table.mutex);
  2290. return 0;
  2291. bad_thin_open:
  2292. __pool_dec(tc->pool);
  2293. bad_pool_lookup:
  2294. dm_put(pool_md);
  2295. bad_common:
  2296. dm_put_device(ti, tc->pool_dev);
  2297. bad_pool_dev:
  2298. if (tc->origin_dev)
  2299. dm_put_device(ti, tc->origin_dev);
  2300. bad_origin_dev:
  2301. kfree(tc);
  2302. out_unlock:
  2303. mutex_unlock(&dm_thin_pool_table.mutex);
  2304. return r;
  2305. }
  2306. static int thin_map(struct dm_target *ti, struct bio *bio)
  2307. {
  2308. bio->bi_sector = dm_target_offset(ti, bio->bi_sector);
  2309. return thin_bio_map(ti, bio);
  2310. }
  2311. static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
  2312. {
  2313. unsigned long flags;
  2314. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  2315. struct list_head work;
  2316. struct dm_thin_new_mapping *m, *tmp;
  2317. struct pool *pool = h->tc->pool;
  2318. if (h->shared_read_entry) {
  2319. INIT_LIST_HEAD(&work);
  2320. dm_deferred_entry_dec(h->shared_read_entry, &work);
  2321. spin_lock_irqsave(&pool->lock, flags);
  2322. list_for_each_entry_safe(m, tmp, &work, list) {
  2323. list_del(&m->list);
  2324. m->quiesced = 1;
  2325. __maybe_add_mapping(m);
  2326. }
  2327. spin_unlock_irqrestore(&pool->lock, flags);
  2328. }
  2329. if (h->all_io_entry) {
  2330. INIT_LIST_HEAD(&work);
  2331. dm_deferred_entry_dec(h->all_io_entry, &work);
  2332. if (!list_empty(&work)) {
  2333. spin_lock_irqsave(&pool->lock, flags);
  2334. list_for_each_entry_safe(m, tmp, &work, list)
  2335. list_add(&m->list, &pool->prepared_discards);
  2336. spin_unlock_irqrestore(&pool->lock, flags);
  2337. wake_worker(pool);
  2338. }
  2339. }
  2340. return 0;
  2341. }
  2342. static void thin_postsuspend(struct dm_target *ti)
  2343. {
  2344. if (dm_noflush_suspending(ti))
  2345. requeue_io((struct thin_c *)ti->private);
  2346. }
  2347. /*
  2348. * <nr mapped sectors> <highest mapped sector>
  2349. */
  2350. static void thin_status(struct dm_target *ti, status_type_t type,
  2351. unsigned status_flags, char *result, unsigned maxlen)
  2352. {
  2353. int r;
  2354. ssize_t sz = 0;
  2355. dm_block_t mapped, highest;
  2356. char buf[BDEVNAME_SIZE];
  2357. struct thin_c *tc = ti->private;
  2358. if (get_pool_mode(tc->pool) == PM_FAIL) {
  2359. DMEMIT("Fail");
  2360. return;
  2361. }
  2362. if (!tc->td)
  2363. DMEMIT("-");
  2364. else {
  2365. switch (type) {
  2366. case STATUSTYPE_INFO:
  2367. r = dm_thin_get_mapped_count(tc->td, &mapped);
  2368. if (r) {
  2369. DMERR("dm_thin_get_mapped_count returned %d", r);
  2370. goto err;
  2371. }
  2372. r = dm_thin_get_highest_mapped_block(tc->td, &highest);
  2373. if (r < 0) {
  2374. DMERR("dm_thin_get_highest_mapped_block returned %d", r);
  2375. goto err;
  2376. }
  2377. DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
  2378. if (r)
  2379. DMEMIT("%llu", ((highest + 1) *
  2380. tc->pool->sectors_per_block) - 1);
  2381. else
  2382. DMEMIT("-");
  2383. break;
  2384. case STATUSTYPE_TABLE:
  2385. DMEMIT("%s %lu",
  2386. format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
  2387. (unsigned long) tc->dev_id);
  2388. if (tc->origin_dev)
  2389. DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
  2390. break;
  2391. }
  2392. }
  2393. return;
  2394. err:
  2395. DMEMIT("Error");
  2396. }
  2397. static int thin_iterate_devices(struct dm_target *ti,
  2398. iterate_devices_callout_fn fn, void *data)
  2399. {
  2400. sector_t blocks;
  2401. struct thin_c *tc = ti->private;
  2402. struct pool *pool = tc->pool;
  2403. /*
  2404. * We can't call dm_pool_get_data_dev_size() since that blocks. So
  2405. * we follow a more convoluted path through to the pool's target.
  2406. */
  2407. if (!pool->ti)
  2408. return 0; /* nothing is bound */
  2409. blocks = pool->ti->len;
  2410. (void) sector_div(blocks, pool->sectors_per_block);
  2411. if (blocks)
  2412. return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
  2413. return 0;
  2414. }
  2415. static struct target_type thin_target = {
  2416. .name = "thin",
  2417. .version = {1, 8, 0},
  2418. .module = THIS_MODULE,
  2419. .ctr = thin_ctr,
  2420. .dtr = thin_dtr,
  2421. .map = thin_map,
  2422. .end_io = thin_endio,
  2423. .postsuspend = thin_postsuspend,
  2424. .status = thin_status,
  2425. .iterate_devices = thin_iterate_devices,
  2426. };
  2427. /*----------------------------------------------------------------*/
  2428. static int __init dm_thin_init(void)
  2429. {
  2430. int r;
  2431. pool_table_init();
  2432. r = dm_register_target(&thin_target);
  2433. if (r)
  2434. return r;
  2435. r = dm_register_target(&pool_target);
  2436. if (r)
  2437. goto bad_pool_target;
  2438. r = -ENOMEM;
  2439. _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
  2440. if (!_new_mapping_cache)
  2441. goto bad_new_mapping_cache;
  2442. return 0;
  2443. bad_new_mapping_cache:
  2444. dm_unregister_target(&pool_target);
  2445. bad_pool_target:
  2446. dm_unregister_target(&thin_target);
  2447. return r;
  2448. }
  2449. static void dm_thin_exit(void)
  2450. {
  2451. dm_unregister_target(&thin_target);
  2452. dm_unregister_target(&pool_target);
  2453. kmem_cache_destroy(_new_mapping_cache);
  2454. }
  2455. module_init(dm_thin_init);
  2456. module_exit(dm_thin_exit);
  2457. MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
  2458. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  2459. MODULE_LICENSE("GPL");