dm-thin.c 70 KB

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