dm-thin.c 73 KB

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