dm-thin.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100
  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/log2.h>
  13. #include <linux/list.h>
  14. #include <linux/rculist.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/sort.h>
  19. #include <linux/rbtree.h>
  20. #define DM_MSG_PREFIX "thin"
  21. /*
  22. * Tunable constants
  23. */
  24. #define ENDIO_HOOK_POOL_SIZE 1024
  25. #define MAPPING_POOL_SIZE 1024
  26. #define COMMIT_PERIOD HZ
  27. #define NO_SPACE_TIMEOUT_SECS 60
  28. static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
  29. DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
  30. "A percentage of time allocated for copy on write");
  31. /*
  32. * The block size of the device holding pool data must be
  33. * between 64KB and 1GB.
  34. */
  35. #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (64 * 1024 >> SECTOR_SHIFT)
  36. #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
  37. /*
  38. * Device id is restricted to 24 bits.
  39. */
  40. #define MAX_DEV_ID ((1 << 24) - 1)
  41. /*
  42. * How do we handle breaking sharing of data blocks?
  43. * =================================================
  44. *
  45. * We use a standard copy-on-write btree to store the mappings for the
  46. * devices (note I'm talking about copy-on-write of the metadata here, not
  47. * the data). When you take an internal snapshot you clone the root node
  48. * of the origin btree. After this there is no concept of an origin or a
  49. * snapshot. They are just two device trees that happen to point to the
  50. * same data blocks.
  51. *
  52. * When we get a write in we decide if it's to a shared data block using
  53. * some timestamp magic. If it is, we have to break sharing.
  54. *
  55. * Let's say we write to a shared block in what was the origin. The
  56. * steps are:
  57. *
  58. * i) plug io further to this physical block. (see bio_prison code).
  59. *
  60. * ii) quiesce any read io to that shared data block. Obviously
  61. * including all devices that share this block. (see dm_deferred_set code)
  62. *
  63. * iii) copy the data block to a newly allocate block. This step can be
  64. * missed out if the io covers the block. (schedule_copy).
  65. *
  66. * iv) insert the new mapping into the origin's btree
  67. * (process_prepared_mapping). This act of inserting breaks some
  68. * sharing of btree nodes between the two devices. Breaking sharing only
  69. * effects the btree of that specific device. Btrees for the other
  70. * devices that share the block never change. The btree for the origin
  71. * device as it was after the last commit is untouched, ie. we're using
  72. * persistent data structures in the functional programming sense.
  73. *
  74. * v) unplug io to this physical block, including the io that triggered
  75. * the breaking of sharing.
  76. *
  77. * Steps (ii) and (iii) occur in parallel.
  78. *
  79. * The metadata _doesn't_ need to be committed before the io continues. We
  80. * get away with this because the io is always written to a _new_ block.
  81. * If there's a crash, then:
  82. *
  83. * - The origin mapping will point to the old origin block (the shared
  84. * one). This will contain the data as it was before the io that triggered
  85. * the breaking of sharing came in.
  86. *
  87. * - The snap mapping still points to the old block. As it would after
  88. * the commit.
  89. *
  90. * The downside of this scheme is the timestamp magic isn't perfect, and
  91. * will continue to think that data block in the snapshot device is shared
  92. * even after the write to the origin has broken sharing. I suspect data
  93. * blocks will typically be shared by many different devices, so we're
  94. * breaking sharing n + 1 times, rather than n, where n is the number of
  95. * devices that reference this data block. At the moment I think the
  96. * benefits far, far outweigh the disadvantages.
  97. */
  98. /*----------------------------------------------------------------*/
  99. /*
  100. * Key building.
  101. */
  102. static void build_data_key(struct dm_thin_device *td,
  103. dm_block_t b, struct dm_cell_key *key)
  104. {
  105. key->virtual = 0;
  106. key->dev = dm_thin_dev_id(td);
  107. key->block_begin = b;
  108. key->block_end = b + 1ULL;
  109. }
  110. static void build_virtual_key(struct dm_thin_device *td, dm_block_t b,
  111. struct dm_cell_key *key)
  112. {
  113. key->virtual = 1;
  114. key->dev = dm_thin_dev_id(td);
  115. key->block_begin = b;
  116. key->block_end = b + 1ULL;
  117. }
  118. /*----------------------------------------------------------------*/
  119. #define THROTTLE_THRESHOLD (1 * HZ)
  120. struct throttle {
  121. struct rw_semaphore lock;
  122. unsigned long threshold;
  123. bool throttle_applied;
  124. };
  125. static void throttle_init(struct throttle *t)
  126. {
  127. init_rwsem(&t->lock);
  128. t->throttle_applied = false;
  129. }
  130. static void throttle_work_start(struct throttle *t)
  131. {
  132. t->threshold = jiffies + THROTTLE_THRESHOLD;
  133. }
  134. static void throttle_work_update(struct throttle *t)
  135. {
  136. if (!t->throttle_applied && jiffies > t->threshold) {
  137. down_write(&t->lock);
  138. t->throttle_applied = true;
  139. }
  140. }
  141. static void throttle_work_complete(struct throttle *t)
  142. {
  143. if (t->throttle_applied) {
  144. t->throttle_applied = false;
  145. up_write(&t->lock);
  146. }
  147. }
  148. static void throttle_lock(struct throttle *t)
  149. {
  150. down_read(&t->lock);
  151. }
  152. static void throttle_unlock(struct throttle *t)
  153. {
  154. up_read(&t->lock);
  155. }
  156. /*----------------------------------------------------------------*/
  157. /*
  158. * A pool device ties together a metadata device and a data device. It
  159. * also provides the interface for creating and destroying internal
  160. * devices.
  161. */
  162. struct dm_thin_new_mapping;
  163. /*
  164. * The pool runs in 4 modes. Ordered in degraded order for comparisons.
  165. */
  166. enum pool_mode {
  167. PM_WRITE, /* metadata may be changed */
  168. PM_OUT_OF_DATA_SPACE, /* metadata may be changed, though data may not be allocated */
  169. PM_READ_ONLY, /* metadata may not be changed */
  170. PM_FAIL, /* all I/O fails */
  171. };
  172. struct pool_features {
  173. enum pool_mode mode;
  174. bool zero_new_blocks:1;
  175. bool discard_enabled:1;
  176. bool discard_passdown:1;
  177. bool error_if_no_space:1;
  178. };
  179. struct thin_c;
  180. typedef void (*process_bio_fn)(struct thin_c *tc, struct bio *bio);
  181. typedef void (*process_cell_fn)(struct thin_c *tc, struct dm_bio_prison_cell *cell);
  182. typedef void (*process_mapping_fn)(struct dm_thin_new_mapping *m);
  183. #define CELL_SORT_ARRAY_SIZE 8192
  184. struct pool {
  185. struct list_head list;
  186. struct dm_target *ti; /* Only set if a pool target is bound */
  187. struct mapped_device *pool_md;
  188. struct block_device *md_dev;
  189. struct dm_pool_metadata *pmd;
  190. dm_block_t low_water_blocks;
  191. uint32_t sectors_per_block;
  192. int sectors_per_block_shift;
  193. struct pool_features pf;
  194. bool low_water_triggered:1; /* A dm event has been sent */
  195. bool suspended:1;
  196. struct dm_bio_prison *prison;
  197. struct dm_kcopyd_client *copier;
  198. struct workqueue_struct *wq;
  199. struct throttle throttle;
  200. struct work_struct worker;
  201. struct delayed_work waker;
  202. struct delayed_work no_space_timeout;
  203. unsigned long last_commit_jiffies;
  204. unsigned ref_count;
  205. spinlock_t lock;
  206. struct bio_list deferred_flush_bios;
  207. struct list_head prepared_mappings;
  208. struct list_head prepared_discards;
  209. struct list_head active_thins;
  210. struct dm_deferred_set *shared_read_ds;
  211. struct dm_deferred_set *all_io_ds;
  212. struct dm_thin_new_mapping *next_mapping;
  213. mempool_t *mapping_pool;
  214. process_bio_fn process_bio;
  215. process_bio_fn process_discard;
  216. process_cell_fn process_cell;
  217. process_cell_fn process_discard_cell;
  218. process_mapping_fn process_prepared_mapping;
  219. process_mapping_fn process_prepared_discard;
  220. struct dm_bio_prison_cell *cell_sort_array[CELL_SORT_ARRAY_SIZE];
  221. };
  222. static enum pool_mode get_pool_mode(struct pool *pool);
  223. static void metadata_operation_failed(struct pool *pool, const char *op, int r);
  224. /*
  225. * Target context for a pool.
  226. */
  227. struct pool_c {
  228. struct dm_target *ti;
  229. struct pool *pool;
  230. struct dm_dev *data_dev;
  231. struct dm_dev *metadata_dev;
  232. struct dm_target_callbacks callbacks;
  233. dm_block_t low_water_blocks;
  234. struct pool_features requested_pf; /* Features requested during table load */
  235. struct pool_features adjusted_pf; /* Features used after adjusting for constituent devices */
  236. };
  237. /*
  238. * Target context for a thin.
  239. */
  240. struct thin_c {
  241. struct list_head list;
  242. struct dm_dev *pool_dev;
  243. struct dm_dev *origin_dev;
  244. sector_t origin_size;
  245. dm_thin_id dev_id;
  246. struct pool *pool;
  247. struct dm_thin_device *td;
  248. struct mapped_device *thin_md;
  249. bool requeue_mode:1;
  250. spinlock_t lock;
  251. struct list_head deferred_cells;
  252. struct bio_list deferred_bio_list;
  253. struct bio_list retry_on_resume_list;
  254. struct rb_root sort_bio_list; /* sorted list of deferred bios */
  255. /*
  256. * Ensures the thin is not destroyed until the worker has finished
  257. * iterating the active_thins list.
  258. */
  259. atomic_t refcount;
  260. struct completion can_destroy;
  261. };
  262. /*----------------------------------------------------------------*/
  263. /*
  264. * wake_worker() is used when new work is queued and when pool_resume is
  265. * ready to continue deferred IO processing.
  266. */
  267. static void wake_worker(struct pool *pool)
  268. {
  269. queue_work(pool->wq, &pool->worker);
  270. }
  271. /*----------------------------------------------------------------*/
  272. static int bio_detain(struct pool *pool, struct dm_cell_key *key, struct bio *bio,
  273. struct dm_bio_prison_cell **cell_result)
  274. {
  275. int r;
  276. struct dm_bio_prison_cell *cell_prealloc;
  277. /*
  278. * Allocate a cell from the prison's mempool.
  279. * This might block but it can't fail.
  280. */
  281. cell_prealloc = dm_bio_prison_alloc_cell(pool->prison, GFP_NOIO);
  282. r = dm_bio_detain(pool->prison, key, bio, cell_prealloc, cell_result);
  283. if (r)
  284. /*
  285. * We reused an old cell; we can get rid of
  286. * the new one.
  287. */
  288. dm_bio_prison_free_cell(pool->prison, cell_prealloc);
  289. return r;
  290. }
  291. static void cell_release(struct pool *pool,
  292. struct dm_bio_prison_cell *cell,
  293. struct bio_list *bios)
  294. {
  295. dm_cell_release(pool->prison, cell, bios);
  296. dm_bio_prison_free_cell(pool->prison, cell);
  297. }
  298. static void cell_visit_release(struct pool *pool,
  299. void (*fn)(void *, struct dm_bio_prison_cell *),
  300. void *context,
  301. struct dm_bio_prison_cell *cell)
  302. {
  303. dm_cell_visit_release(pool->prison, fn, context, cell);
  304. dm_bio_prison_free_cell(pool->prison, cell);
  305. }
  306. static void cell_release_no_holder(struct pool *pool,
  307. struct dm_bio_prison_cell *cell,
  308. struct bio_list *bios)
  309. {
  310. dm_cell_release_no_holder(pool->prison, cell, bios);
  311. dm_bio_prison_free_cell(pool->prison, cell);
  312. }
  313. static void cell_error_with_code(struct pool *pool,
  314. struct dm_bio_prison_cell *cell, int error_code)
  315. {
  316. dm_cell_error(pool->prison, cell, error_code);
  317. dm_bio_prison_free_cell(pool->prison, cell);
  318. }
  319. static void cell_error(struct pool *pool, struct dm_bio_prison_cell *cell)
  320. {
  321. cell_error_with_code(pool, cell, -EIO);
  322. }
  323. static void cell_success(struct pool *pool, struct dm_bio_prison_cell *cell)
  324. {
  325. cell_error_with_code(pool, cell, 0);
  326. }
  327. static void cell_requeue(struct pool *pool, struct dm_bio_prison_cell *cell)
  328. {
  329. cell_error_with_code(pool, cell, DM_ENDIO_REQUEUE);
  330. }
  331. /*----------------------------------------------------------------*/
  332. /*
  333. * A global list of pools that uses a struct mapped_device as a key.
  334. */
  335. static struct dm_thin_pool_table {
  336. struct mutex mutex;
  337. struct list_head pools;
  338. } dm_thin_pool_table;
  339. static void pool_table_init(void)
  340. {
  341. mutex_init(&dm_thin_pool_table.mutex);
  342. INIT_LIST_HEAD(&dm_thin_pool_table.pools);
  343. }
  344. static void __pool_table_insert(struct pool *pool)
  345. {
  346. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  347. list_add(&pool->list, &dm_thin_pool_table.pools);
  348. }
  349. static void __pool_table_remove(struct pool *pool)
  350. {
  351. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  352. list_del(&pool->list);
  353. }
  354. static struct pool *__pool_table_lookup(struct mapped_device *md)
  355. {
  356. struct pool *pool = NULL, *tmp;
  357. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  358. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  359. if (tmp->pool_md == md) {
  360. pool = tmp;
  361. break;
  362. }
  363. }
  364. return pool;
  365. }
  366. static struct pool *__pool_table_lookup_metadata_dev(struct block_device *md_dev)
  367. {
  368. struct pool *pool = NULL, *tmp;
  369. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  370. list_for_each_entry(tmp, &dm_thin_pool_table.pools, list) {
  371. if (tmp->md_dev == md_dev) {
  372. pool = tmp;
  373. break;
  374. }
  375. }
  376. return pool;
  377. }
  378. /*----------------------------------------------------------------*/
  379. struct dm_thin_endio_hook {
  380. struct thin_c *tc;
  381. struct dm_deferred_entry *shared_read_entry;
  382. struct dm_deferred_entry *all_io_entry;
  383. struct dm_thin_new_mapping *overwrite_mapping;
  384. struct rb_node rb_node;
  385. };
  386. static void __merge_bio_list(struct bio_list *bios, struct bio_list *master)
  387. {
  388. bio_list_merge(bios, master);
  389. bio_list_init(master);
  390. }
  391. static void error_bio_list(struct bio_list *bios, int error)
  392. {
  393. struct bio *bio;
  394. while ((bio = bio_list_pop(bios)))
  395. bio_endio(bio, error);
  396. }
  397. static void error_thin_bio_list(struct thin_c *tc, struct bio_list *master, int error)
  398. {
  399. struct bio_list bios;
  400. unsigned long flags;
  401. bio_list_init(&bios);
  402. spin_lock_irqsave(&tc->lock, flags);
  403. __merge_bio_list(&bios, master);
  404. spin_unlock_irqrestore(&tc->lock, flags);
  405. error_bio_list(&bios, error);
  406. }
  407. static void requeue_deferred_cells(struct thin_c *tc)
  408. {
  409. struct pool *pool = tc->pool;
  410. unsigned long flags;
  411. struct list_head cells;
  412. struct dm_bio_prison_cell *cell, *tmp;
  413. INIT_LIST_HEAD(&cells);
  414. spin_lock_irqsave(&tc->lock, flags);
  415. list_splice_init(&tc->deferred_cells, &cells);
  416. spin_unlock_irqrestore(&tc->lock, flags);
  417. list_for_each_entry_safe(cell, tmp, &cells, user_list)
  418. cell_requeue(pool, cell);
  419. }
  420. static void requeue_io(struct thin_c *tc)
  421. {
  422. struct bio_list bios;
  423. unsigned long flags;
  424. bio_list_init(&bios);
  425. spin_lock_irqsave(&tc->lock, flags);
  426. __merge_bio_list(&bios, &tc->deferred_bio_list);
  427. __merge_bio_list(&bios, &tc->retry_on_resume_list);
  428. spin_unlock_irqrestore(&tc->lock, flags);
  429. error_bio_list(&bios, DM_ENDIO_REQUEUE);
  430. requeue_deferred_cells(tc);
  431. }
  432. static void error_retry_list(struct pool *pool)
  433. {
  434. struct thin_c *tc;
  435. rcu_read_lock();
  436. list_for_each_entry_rcu(tc, &pool->active_thins, list)
  437. error_thin_bio_list(tc, &tc->retry_on_resume_list, -EIO);
  438. rcu_read_unlock();
  439. }
  440. /*
  441. * This section of code contains the logic for processing a thin device's IO.
  442. * Much of the code depends on pool object resources (lists, workqueues, etc)
  443. * but most is exclusively called from the thin target rather than the thin-pool
  444. * target.
  445. */
  446. static bool block_size_is_power_of_two(struct pool *pool)
  447. {
  448. return pool->sectors_per_block_shift >= 0;
  449. }
  450. static dm_block_t get_bio_block(struct thin_c *tc, struct bio *bio)
  451. {
  452. struct pool *pool = tc->pool;
  453. sector_t block_nr = bio->bi_iter.bi_sector;
  454. if (block_size_is_power_of_two(pool))
  455. block_nr >>= pool->sectors_per_block_shift;
  456. else
  457. (void) sector_div(block_nr, pool->sectors_per_block);
  458. return block_nr;
  459. }
  460. static void remap(struct thin_c *tc, struct bio *bio, dm_block_t block)
  461. {
  462. struct pool *pool = tc->pool;
  463. sector_t bi_sector = bio->bi_iter.bi_sector;
  464. bio->bi_bdev = tc->pool_dev->bdev;
  465. if (block_size_is_power_of_two(pool))
  466. bio->bi_iter.bi_sector =
  467. (block << pool->sectors_per_block_shift) |
  468. (bi_sector & (pool->sectors_per_block - 1));
  469. else
  470. bio->bi_iter.bi_sector = (block * pool->sectors_per_block) +
  471. sector_div(bi_sector, pool->sectors_per_block);
  472. }
  473. static void remap_to_origin(struct thin_c *tc, struct bio *bio)
  474. {
  475. bio->bi_bdev = tc->origin_dev->bdev;
  476. }
  477. static int bio_triggers_commit(struct thin_c *tc, struct bio *bio)
  478. {
  479. return (bio->bi_rw & (REQ_FLUSH | REQ_FUA)) &&
  480. dm_thin_changed_this_transaction(tc->td);
  481. }
  482. static void inc_all_io_entry(struct pool *pool, struct bio *bio)
  483. {
  484. struct dm_thin_endio_hook *h;
  485. if (bio->bi_rw & REQ_DISCARD)
  486. return;
  487. h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  488. h->all_io_entry = dm_deferred_entry_inc(pool->all_io_ds);
  489. }
  490. static void issue(struct thin_c *tc, struct bio *bio)
  491. {
  492. struct pool *pool = tc->pool;
  493. unsigned long flags;
  494. if (!bio_triggers_commit(tc, bio)) {
  495. generic_make_request(bio);
  496. return;
  497. }
  498. /*
  499. * Complete bio with an error if earlier I/O caused changes to
  500. * the metadata that can't be committed e.g, due to I/O errors
  501. * on the metadata device.
  502. */
  503. if (dm_thin_aborted_changes(tc->td)) {
  504. bio_io_error(bio);
  505. return;
  506. }
  507. /*
  508. * Batch together any bios that trigger commits and then issue a
  509. * single commit for them in process_deferred_bios().
  510. */
  511. spin_lock_irqsave(&pool->lock, flags);
  512. bio_list_add(&pool->deferred_flush_bios, bio);
  513. spin_unlock_irqrestore(&pool->lock, flags);
  514. }
  515. static void remap_to_origin_and_issue(struct thin_c *tc, struct bio *bio)
  516. {
  517. remap_to_origin(tc, bio);
  518. issue(tc, bio);
  519. }
  520. static void remap_and_issue(struct thin_c *tc, struct bio *bio,
  521. dm_block_t block)
  522. {
  523. remap(tc, bio, block);
  524. issue(tc, bio);
  525. }
  526. /*----------------------------------------------------------------*/
  527. /*
  528. * Bio endio functions.
  529. */
  530. struct dm_thin_new_mapping {
  531. struct list_head list;
  532. bool pass_discard:1;
  533. bool definitely_not_shared:1;
  534. /*
  535. * Track quiescing, copying and zeroing preparation actions. When this
  536. * counter hits zero the block is prepared and can be inserted into the
  537. * btree.
  538. */
  539. atomic_t prepare_actions;
  540. int err;
  541. struct thin_c *tc;
  542. dm_block_t virt_block;
  543. dm_block_t data_block;
  544. struct dm_bio_prison_cell *cell, *cell2;
  545. /*
  546. * If the bio covers the whole area of a block then we can avoid
  547. * zeroing or copying. Instead this bio is hooked. The bio will
  548. * still be in the cell, so care has to be taken to avoid issuing
  549. * the bio twice.
  550. */
  551. struct bio *bio;
  552. bio_end_io_t *saved_bi_end_io;
  553. };
  554. static void __complete_mapping_preparation(struct dm_thin_new_mapping *m)
  555. {
  556. struct pool *pool = m->tc->pool;
  557. if (atomic_dec_and_test(&m->prepare_actions)) {
  558. list_add_tail(&m->list, &pool->prepared_mappings);
  559. wake_worker(pool);
  560. }
  561. }
  562. static void complete_mapping_preparation(struct dm_thin_new_mapping *m)
  563. {
  564. unsigned long flags;
  565. struct pool *pool = m->tc->pool;
  566. spin_lock_irqsave(&pool->lock, flags);
  567. __complete_mapping_preparation(m);
  568. spin_unlock_irqrestore(&pool->lock, flags);
  569. }
  570. static void copy_complete(int read_err, unsigned long write_err, void *context)
  571. {
  572. struct dm_thin_new_mapping *m = context;
  573. m->err = read_err || write_err ? -EIO : 0;
  574. complete_mapping_preparation(m);
  575. }
  576. static void overwrite_endio(struct bio *bio, int err)
  577. {
  578. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  579. struct dm_thin_new_mapping *m = h->overwrite_mapping;
  580. m->err = err;
  581. complete_mapping_preparation(m);
  582. }
  583. /*----------------------------------------------------------------*/
  584. /*
  585. * Workqueue.
  586. */
  587. /*
  588. * Prepared mapping jobs.
  589. */
  590. /*
  591. * This sends the bios in the cell, except the original holder, back
  592. * to the deferred_bios list.
  593. */
  594. static void cell_defer_no_holder(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  595. {
  596. struct pool *pool = tc->pool;
  597. unsigned long flags;
  598. spin_lock_irqsave(&tc->lock, flags);
  599. cell_release_no_holder(pool, cell, &tc->deferred_bio_list);
  600. spin_unlock_irqrestore(&tc->lock, flags);
  601. wake_worker(pool);
  602. }
  603. static void thin_defer_bio(struct thin_c *tc, struct bio *bio);
  604. struct remap_info {
  605. struct thin_c *tc;
  606. struct bio_list defer_bios;
  607. struct bio_list issue_bios;
  608. };
  609. static void __inc_remap_and_issue_cell(void *context,
  610. struct dm_bio_prison_cell *cell)
  611. {
  612. struct remap_info *info = context;
  613. struct bio *bio;
  614. while ((bio = bio_list_pop(&cell->bios))) {
  615. if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA))
  616. bio_list_add(&info->defer_bios, bio);
  617. else {
  618. inc_all_io_entry(info->tc->pool, bio);
  619. /*
  620. * We can't issue the bios with the bio prison lock
  621. * held, so we add them to a list to issue on
  622. * return from this function.
  623. */
  624. bio_list_add(&info->issue_bios, bio);
  625. }
  626. }
  627. }
  628. static void inc_remap_and_issue_cell(struct thin_c *tc,
  629. struct dm_bio_prison_cell *cell,
  630. dm_block_t block)
  631. {
  632. struct bio *bio;
  633. struct remap_info info;
  634. info.tc = tc;
  635. bio_list_init(&info.defer_bios);
  636. bio_list_init(&info.issue_bios);
  637. /*
  638. * We have to be careful to inc any bios we're about to issue
  639. * before the cell is released, and avoid a race with new bios
  640. * being added to the cell.
  641. */
  642. cell_visit_release(tc->pool, __inc_remap_and_issue_cell,
  643. &info, cell);
  644. while ((bio = bio_list_pop(&info.defer_bios)))
  645. thin_defer_bio(tc, bio);
  646. while ((bio = bio_list_pop(&info.issue_bios)))
  647. remap_and_issue(info.tc, bio, block);
  648. }
  649. static void process_prepared_mapping_fail(struct dm_thin_new_mapping *m)
  650. {
  651. if (m->bio) {
  652. m->bio->bi_end_io = m->saved_bi_end_io;
  653. atomic_inc(&m->bio->bi_remaining);
  654. }
  655. cell_error(m->tc->pool, m->cell);
  656. list_del(&m->list);
  657. mempool_free(m, m->tc->pool->mapping_pool);
  658. }
  659. static void process_prepared_mapping(struct dm_thin_new_mapping *m)
  660. {
  661. struct thin_c *tc = m->tc;
  662. struct pool *pool = tc->pool;
  663. struct bio *bio;
  664. int r;
  665. bio = m->bio;
  666. if (bio) {
  667. bio->bi_end_io = m->saved_bi_end_io;
  668. atomic_inc(&bio->bi_remaining);
  669. }
  670. if (m->err) {
  671. cell_error(pool, m->cell);
  672. goto out;
  673. }
  674. /*
  675. * Commit the prepared block into the mapping btree.
  676. * Any I/O for this block arriving after this point will get
  677. * remapped to it directly.
  678. */
  679. r = dm_thin_insert_block(tc->td, m->virt_block, m->data_block);
  680. if (r) {
  681. metadata_operation_failed(pool, "dm_thin_insert_block", r);
  682. cell_error(pool, m->cell);
  683. goto out;
  684. }
  685. /*
  686. * Release any bios held while the block was being provisioned.
  687. * If we are processing a write bio that completely covers the block,
  688. * we already processed it so can ignore it now when processing
  689. * the bios in the cell.
  690. */
  691. if (bio) {
  692. inc_remap_and_issue_cell(tc, m->cell, m->data_block);
  693. bio_endio(bio, 0);
  694. } else {
  695. inc_all_io_entry(tc->pool, m->cell->holder);
  696. remap_and_issue(tc, m->cell->holder, m->data_block);
  697. inc_remap_and_issue_cell(tc, m->cell, m->data_block);
  698. }
  699. out:
  700. list_del(&m->list);
  701. mempool_free(m, pool->mapping_pool);
  702. }
  703. static void process_prepared_discard_fail(struct dm_thin_new_mapping *m)
  704. {
  705. struct thin_c *tc = m->tc;
  706. bio_io_error(m->bio);
  707. cell_defer_no_holder(tc, m->cell);
  708. cell_defer_no_holder(tc, m->cell2);
  709. mempool_free(m, tc->pool->mapping_pool);
  710. }
  711. static void process_prepared_discard_passdown(struct dm_thin_new_mapping *m)
  712. {
  713. struct thin_c *tc = m->tc;
  714. inc_all_io_entry(tc->pool, m->bio);
  715. cell_defer_no_holder(tc, m->cell);
  716. cell_defer_no_holder(tc, m->cell2);
  717. if (m->pass_discard)
  718. if (m->definitely_not_shared)
  719. remap_and_issue(tc, m->bio, m->data_block);
  720. else {
  721. bool used = false;
  722. if (dm_pool_block_is_used(tc->pool->pmd, m->data_block, &used) || used)
  723. bio_endio(m->bio, 0);
  724. else
  725. remap_and_issue(tc, m->bio, m->data_block);
  726. }
  727. else
  728. bio_endio(m->bio, 0);
  729. mempool_free(m, tc->pool->mapping_pool);
  730. }
  731. static void process_prepared_discard(struct dm_thin_new_mapping *m)
  732. {
  733. int r;
  734. struct thin_c *tc = m->tc;
  735. r = dm_thin_remove_block(tc->td, m->virt_block);
  736. if (r)
  737. DMERR_LIMIT("dm_thin_remove_block() failed");
  738. process_prepared_discard_passdown(m);
  739. }
  740. static void process_prepared(struct pool *pool, struct list_head *head,
  741. process_mapping_fn *fn)
  742. {
  743. unsigned long flags;
  744. struct list_head maps;
  745. struct dm_thin_new_mapping *m, *tmp;
  746. INIT_LIST_HEAD(&maps);
  747. spin_lock_irqsave(&pool->lock, flags);
  748. list_splice_init(head, &maps);
  749. spin_unlock_irqrestore(&pool->lock, flags);
  750. list_for_each_entry_safe(m, tmp, &maps, list)
  751. (*fn)(m);
  752. }
  753. /*
  754. * Deferred bio jobs.
  755. */
  756. static int io_overlaps_block(struct pool *pool, struct bio *bio)
  757. {
  758. return bio->bi_iter.bi_size ==
  759. (pool->sectors_per_block << SECTOR_SHIFT);
  760. }
  761. static int io_overwrites_block(struct pool *pool, struct bio *bio)
  762. {
  763. return (bio_data_dir(bio) == WRITE) &&
  764. io_overlaps_block(pool, bio);
  765. }
  766. static void save_and_set_endio(struct bio *bio, bio_end_io_t **save,
  767. bio_end_io_t *fn)
  768. {
  769. *save = bio->bi_end_io;
  770. bio->bi_end_io = fn;
  771. }
  772. static int ensure_next_mapping(struct pool *pool)
  773. {
  774. if (pool->next_mapping)
  775. return 0;
  776. pool->next_mapping = mempool_alloc(pool->mapping_pool, GFP_ATOMIC);
  777. return pool->next_mapping ? 0 : -ENOMEM;
  778. }
  779. static struct dm_thin_new_mapping *get_next_mapping(struct pool *pool)
  780. {
  781. struct dm_thin_new_mapping *m = pool->next_mapping;
  782. BUG_ON(!pool->next_mapping);
  783. memset(m, 0, sizeof(struct dm_thin_new_mapping));
  784. INIT_LIST_HEAD(&m->list);
  785. m->bio = NULL;
  786. pool->next_mapping = NULL;
  787. return m;
  788. }
  789. static void ll_zero(struct thin_c *tc, struct dm_thin_new_mapping *m,
  790. sector_t begin, sector_t end)
  791. {
  792. int r;
  793. struct dm_io_region to;
  794. to.bdev = tc->pool_dev->bdev;
  795. to.sector = begin;
  796. to.count = end - begin;
  797. r = dm_kcopyd_zero(tc->pool->copier, 1, &to, 0, copy_complete, m);
  798. if (r < 0) {
  799. DMERR_LIMIT("dm_kcopyd_zero() failed");
  800. copy_complete(1, 1, m);
  801. }
  802. }
  803. static void remap_and_issue_overwrite(struct thin_c *tc, struct bio *bio,
  804. dm_block_t data_block,
  805. struct dm_thin_new_mapping *m)
  806. {
  807. struct pool *pool = tc->pool;
  808. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  809. h->overwrite_mapping = m;
  810. m->bio = bio;
  811. save_and_set_endio(bio, &m->saved_bi_end_io, overwrite_endio);
  812. inc_all_io_entry(pool, bio);
  813. remap_and_issue(tc, bio, data_block);
  814. }
  815. /*
  816. * A partial copy also needs to zero the uncopied region.
  817. */
  818. static void schedule_copy(struct thin_c *tc, dm_block_t virt_block,
  819. struct dm_dev *origin, dm_block_t data_origin,
  820. dm_block_t data_dest,
  821. struct dm_bio_prison_cell *cell, struct bio *bio,
  822. sector_t len)
  823. {
  824. int r;
  825. struct pool *pool = tc->pool;
  826. struct dm_thin_new_mapping *m = get_next_mapping(pool);
  827. m->tc = tc;
  828. m->virt_block = virt_block;
  829. m->data_block = data_dest;
  830. m->cell = cell;
  831. /*
  832. * quiesce action + copy action + an extra reference held for the
  833. * duration of this function (we may need to inc later for a
  834. * partial zero).
  835. */
  836. atomic_set(&m->prepare_actions, 3);
  837. if (!dm_deferred_set_add_work(pool->shared_read_ds, &m->list))
  838. complete_mapping_preparation(m); /* already quiesced */
  839. /*
  840. * IO to pool_dev remaps to the pool target's data_dev.
  841. *
  842. * If the whole block of data is being overwritten, we can issue the
  843. * bio immediately. Otherwise we use kcopyd to clone the data first.
  844. */
  845. if (io_overwrites_block(pool, bio))
  846. remap_and_issue_overwrite(tc, bio, data_dest, m);
  847. else {
  848. struct dm_io_region from, to;
  849. from.bdev = origin->bdev;
  850. from.sector = data_origin * pool->sectors_per_block;
  851. from.count = len;
  852. to.bdev = tc->pool_dev->bdev;
  853. to.sector = data_dest * pool->sectors_per_block;
  854. to.count = len;
  855. r = dm_kcopyd_copy(pool->copier, &from, 1, &to,
  856. 0, copy_complete, m);
  857. if (r < 0) {
  858. DMERR_LIMIT("dm_kcopyd_copy() failed");
  859. copy_complete(1, 1, m);
  860. /*
  861. * We allow the zero to be issued, to simplify the
  862. * error path. Otherwise we'd need to start
  863. * worrying about decrementing the prepare_actions
  864. * counter.
  865. */
  866. }
  867. /*
  868. * Do we need to zero a tail region?
  869. */
  870. if (len < pool->sectors_per_block && pool->pf.zero_new_blocks) {
  871. atomic_inc(&m->prepare_actions);
  872. ll_zero(tc, m,
  873. data_dest * pool->sectors_per_block + len,
  874. (data_dest + 1) * pool->sectors_per_block);
  875. }
  876. }
  877. complete_mapping_preparation(m); /* drop our ref */
  878. }
  879. static void schedule_internal_copy(struct thin_c *tc, dm_block_t virt_block,
  880. dm_block_t data_origin, dm_block_t data_dest,
  881. struct dm_bio_prison_cell *cell, struct bio *bio)
  882. {
  883. schedule_copy(tc, virt_block, tc->pool_dev,
  884. data_origin, data_dest, cell, bio,
  885. tc->pool->sectors_per_block);
  886. }
  887. static void schedule_zero(struct thin_c *tc, dm_block_t virt_block,
  888. dm_block_t data_block, struct dm_bio_prison_cell *cell,
  889. struct bio *bio)
  890. {
  891. struct pool *pool = tc->pool;
  892. struct dm_thin_new_mapping *m = get_next_mapping(pool);
  893. atomic_set(&m->prepare_actions, 1); /* no need to quiesce */
  894. m->tc = tc;
  895. m->virt_block = virt_block;
  896. m->data_block = data_block;
  897. m->cell = cell;
  898. /*
  899. * If the whole block of data is being overwritten or we are not
  900. * zeroing pre-existing data, we can issue the bio immediately.
  901. * Otherwise we use kcopyd to zero the data first.
  902. */
  903. if (!pool->pf.zero_new_blocks)
  904. process_prepared_mapping(m);
  905. else if (io_overwrites_block(pool, bio))
  906. remap_and_issue_overwrite(tc, bio, data_block, m);
  907. else
  908. ll_zero(tc, m,
  909. data_block * pool->sectors_per_block,
  910. (data_block + 1) * pool->sectors_per_block);
  911. }
  912. static void schedule_external_copy(struct thin_c *tc, dm_block_t virt_block,
  913. dm_block_t data_dest,
  914. struct dm_bio_prison_cell *cell, struct bio *bio)
  915. {
  916. struct pool *pool = tc->pool;
  917. sector_t virt_block_begin = virt_block * pool->sectors_per_block;
  918. sector_t virt_block_end = (virt_block + 1) * pool->sectors_per_block;
  919. if (virt_block_end <= tc->origin_size)
  920. schedule_copy(tc, virt_block, tc->origin_dev,
  921. virt_block, data_dest, cell, bio,
  922. pool->sectors_per_block);
  923. else if (virt_block_begin < tc->origin_size)
  924. schedule_copy(tc, virt_block, tc->origin_dev,
  925. virt_block, data_dest, cell, bio,
  926. tc->origin_size - virt_block_begin);
  927. else
  928. schedule_zero(tc, virt_block, data_dest, cell, bio);
  929. }
  930. static void set_pool_mode(struct pool *pool, enum pool_mode new_mode);
  931. static void check_for_space(struct pool *pool)
  932. {
  933. int r;
  934. dm_block_t nr_free;
  935. if (get_pool_mode(pool) != PM_OUT_OF_DATA_SPACE)
  936. return;
  937. r = dm_pool_get_free_block_count(pool->pmd, &nr_free);
  938. if (r)
  939. return;
  940. if (nr_free)
  941. set_pool_mode(pool, PM_WRITE);
  942. }
  943. /*
  944. * A non-zero return indicates read_only or fail_io mode.
  945. * Many callers don't care about the return value.
  946. */
  947. static int commit(struct pool *pool)
  948. {
  949. int r;
  950. if (get_pool_mode(pool) >= PM_READ_ONLY)
  951. return -EINVAL;
  952. r = dm_pool_commit_metadata(pool->pmd);
  953. if (r)
  954. metadata_operation_failed(pool, "dm_pool_commit_metadata", r);
  955. else
  956. check_for_space(pool);
  957. return r;
  958. }
  959. static void check_low_water_mark(struct pool *pool, dm_block_t free_blocks)
  960. {
  961. unsigned long flags;
  962. if (free_blocks <= pool->low_water_blocks && !pool->low_water_triggered) {
  963. DMWARN("%s: reached low water mark for data device: sending event.",
  964. dm_device_name(pool->pool_md));
  965. spin_lock_irqsave(&pool->lock, flags);
  966. pool->low_water_triggered = true;
  967. spin_unlock_irqrestore(&pool->lock, flags);
  968. dm_table_event(pool->ti->table);
  969. }
  970. }
  971. static int alloc_data_block(struct thin_c *tc, dm_block_t *result)
  972. {
  973. int r;
  974. dm_block_t free_blocks;
  975. struct pool *pool = tc->pool;
  976. if (WARN_ON(get_pool_mode(pool) != PM_WRITE))
  977. return -EINVAL;
  978. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  979. if (r) {
  980. metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
  981. return r;
  982. }
  983. check_low_water_mark(pool, free_blocks);
  984. if (!free_blocks) {
  985. /*
  986. * Try to commit to see if that will free up some
  987. * more space.
  988. */
  989. r = commit(pool);
  990. if (r)
  991. return r;
  992. r = dm_pool_get_free_block_count(pool->pmd, &free_blocks);
  993. if (r) {
  994. metadata_operation_failed(pool, "dm_pool_get_free_block_count", r);
  995. return r;
  996. }
  997. if (!free_blocks) {
  998. set_pool_mode(pool, PM_OUT_OF_DATA_SPACE);
  999. return -ENOSPC;
  1000. }
  1001. }
  1002. r = dm_pool_alloc_data_block(pool->pmd, result);
  1003. if (r) {
  1004. metadata_operation_failed(pool, "dm_pool_alloc_data_block", r);
  1005. return r;
  1006. }
  1007. return 0;
  1008. }
  1009. /*
  1010. * If we have run out of space, queue bios until the device is
  1011. * resumed, presumably after having been reloaded with more space.
  1012. */
  1013. static void retry_on_resume(struct bio *bio)
  1014. {
  1015. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1016. struct thin_c *tc = h->tc;
  1017. unsigned long flags;
  1018. spin_lock_irqsave(&tc->lock, flags);
  1019. bio_list_add(&tc->retry_on_resume_list, bio);
  1020. spin_unlock_irqrestore(&tc->lock, flags);
  1021. }
  1022. static int should_error_unserviceable_bio(struct pool *pool)
  1023. {
  1024. enum pool_mode m = get_pool_mode(pool);
  1025. switch (m) {
  1026. case PM_WRITE:
  1027. /* Shouldn't get here */
  1028. DMERR_LIMIT("bio unserviceable, yet pool is in PM_WRITE mode");
  1029. return -EIO;
  1030. case PM_OUT_OF_DATA_SPACE:
  1031. return pool->pf.error_if_no_space ? -ENOSPC : 0;
  1032. case PM_READ_ONLY:
  1033. case PM_FAIL:
  1034. return -EIO;
  1035. default:
  1036. /* Shouldn't get here */
  1037. DMERR_LIMIT("bio unserviceable, yet pool has an unknown mode");
  1038. return -EIO;
  1039. }
  1040. }
  1041. static void handle_unserviceable_bio(struct pool *pool, struct bio *bio)
  1042. {
  1043. int error = should_error_unserviceable_bio(pool);
  1044. if (error)
  1045. bio_endio(bio, error);
  1046. else
  1047. retry_on_resume(bio);
  1048. }
  1049. static void retry_bios_on_resume(struct pool *pool, struct dm_bio_prison_cell *cell)
  1050. {
  1051. struct bio *bio;
  1052. struct bio_list bios;
  1053. int error;
  1054. error = should_error_unserviceable_bio(pool);
  1055. if (error) {
  1056. cell_error_with_code(pool, cell, error);
  1057. return;
  1058. }
  1059. bio_list_init(&bios);
  1060. cell_release(pool, cell, &bios);
  1061. while ((bio = bio_list_pop(&bios)))
  1062. retry_on_resume(bio);
  1063. }
  1064. static void process_discard_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1065. {
  1066. int r;
  1067. struct bio *bio = cell->holder;
  1068. struct pool *pool = tc->pool;
  1069. struct dm_bio_prison_cell *cell2;
  1070. struct dm_cell_key key2;
  1071. dm_block_t block = get_bio_block(tc, bio);
  1072. struct dm_thin_lookup_result lookup_result;
  1073. struct dm_thin_new_mapping *m;
  1074. if (tc->requeue_mode) {
  1075. cell_requeue(pool, cell);
  1076. return;
  1077. }
  1078. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1079. switch (r) {
  1080. case 0:
  1081. /*
  1082. * Check nobody is fiddling with this pool block. This can
  1083. * happen if someone's in the process of breaking sharing
  1084. * on this block.
  1085. */
  1086. build_data_key(tc->td, lookup_result.block, &key2);
  1087. if (bio_detain(tc->pool, &key2, bio, &cell2)) {
  1088. cell_defer_no_holder(tc, cell);
  1089. break;
  1090. }
  1091. if (io_overlaps_block(pool, bio)) {
  1092. /*
  1093. * IO may still be going to the destination block. We must
  1094. * quiesce before we can do the removal.
  1095. */
  1096. m = get_next_mapping(pool);
  1097. m->tc = tc;
  1098. m->pass_discard = pool->pf.discard_passdown;
  1099. m->definitely_not_shared = !lookup_result.shared;
  1100. m->virt_block = block;
  1101. m->data_block = lookup_result.block;
  1102. m->cell = cell;
  1103. m->cell2 = cell2;
  1104. m->bio = bio;
  1105. if (!dm_deferred_set_add_work(pool->all_io_ds, &m->list))
  1106. pool->process_prepared_discard(m);
  1107. } else {
  1108. inc_all_io_entry(pool, bio);
  1109. cell_defer_no_holder(tc, cell);
  1110. cell_defer_no_holder(tc, cell2);
  1111. /*
  1112. * The DM core makes sure that the discard doesn't span
  1113. * a block boundary. So we submit the discard of a
  1114. * partial block appropriately.
  1115. */
  1116. if ((!lookup_result.shared) && pool->pf.discard_passdown)
  1117. remap_and_issue(tc, bio, lookup_result.block);
  1118. else
  1119. bio_endio(bio, 0);
  1120. }
  1121. break;
  1122. case -ENODATA:
  1123. /*
  1124. * It isn't provisioned, just forget it.
  1125. */
  1126. cell_defer_no_holder(tc, cell);
  1127. bio_endio(bio, 0);
  1128. break;
  1129. default:
  1130. DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
  1131. __func__, r);
  1132. cell_defer_no_holder(tc, cell);
  1133. bio_io_error(bio);
  1134. break;
  1135. }
  1136. }
  1137. static void process_discard_bio(struct thin_c *tc, struct bio *bio)
  1138. {
  1139. struct dm_bio_prison_cell *cell;
  1140. struct dm_cell_key key;
  1141. dm_block_t block = get_bio_block(tc, bio);
  1142. build_virtual_key(tc->td, block, &key);
  1143. if (bio_detain(tc->pool, &key, bio, &cell))
  1144. return;
  1145. process_discard_cell(tc, cell);
  1146. }
  1147. static void break_sharing(struct thin_c *tc, struct bio *bio, dm_block_t block,
  1148. struct dm_cell_key *key,
  1149. struct dm_thin_lookup_result *lookup_result,
  1150. struct dm_bio_prison_cell *cell)
  1151. {
  1152. int r;
  1153. dm_block_t data_block;
  1154. struct pool *pool = tc->pool;
  1155. r = alloc_data_block(tc, &data_block);
  1156. switch (r) {
  1157. case 0:
  1158. schedule_internal_copy(tc, block, lookup_result->block,
  1159. data_block, cell, bio);
  1160. break;
  1161. case -ENOSPC:
  1162. retry_bios_on_resume(pool, cell);
  1163. break;
  1164. default:
  1165. DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
  1166. __func__, r);
  1167. cell_error(pool, cell);
  1168. break;
  1169. }
  1170. }
  1171. static void __remap_and_issue_shared_cell(void *context,
  1172. struct dm_bio_prison_cell *cell)
  1173. {
  1174. struct remap_info *info = context;
  1175. struct bio *bio;
  1176. while ((bio = bio_list_pop(&cell->bios))) {
  1177. if ((bio_data_dir(bio) == WRITE) ||
  1178. (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)))
  1179. bio_list_add(&info->defer_bios, bio);
  1180. else {
  1181. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));;
  1182. h->shared_read_entry = dm_deferred_entry_inc(info->tc->pool->shared_read_ds);
  1183. inc_all_io_entry(info->tc->pool, bio);
  1184. bio_list_add(&info->issue_bios, bio);
  1185. }
  1186. }
  1187. }
  1188. static void remap_and_issue_shared_cell(struct thin_c *tc,
  1189. struct dm_bio_prison_cell *cell,
  1190. dm_block_t block)
  1191. {
  1192. struct bio *bio;
  1193. struct remap_info info;
  1194. info.tc = tc;
  1195. bio_list_init(&info.defer_bios);
  1196. bio_list_init(&info.issue_bios);
  1197. cell_visit_release(tc->pool, __remap_and_issue_shared_cell,
  1198. &info, cell);
  1199. while ((bio = bio_list_pop(&info.defer_bios)))
  1200. thin_defer_bio(tc, bio);
  1201. while ((bio = bio_list_pop(&info.issue_bios)))
  1202. remap_and_issue(tc, bio, block);
  1203. }
  1204. static void process_shared_bio(struct thin_c *tc, struct bio *bio,
  1205. dm_block_t block,
  1206. struct dm_thin_lookup_result *lookup_result,
  1207. struct dm_bio_prison_cell *virt_cell)
  1208. {
  1209. struct dm_bio_prison_cell *data_cell;
  1210. struct pool *pool = tc->pool;
  1211. struct dm_cell_key key;
  1212. /*
  1213. * If cell is already occupied, then sharing is already in the process
  1214. * of being broken so we have nothing further to do here.
  1215. */
  1216. build_data_key(tc->td, lookup_result->block, &key);
  1217. if (bio_detain(pool, &key, bio, &data_cell)) {
  1218. cell_defer_no_holder(tc, virt_cell);
  1219. return;
  1220. }
  1221. if (bio_data_dir(bio) == WRITE && bio->bi_iter.bi_size) {
  1222. break_sharing(tc, bio, block, &key, lookup_result, data_cell);
  1223. cell_defer_no_holder(tc, virt_cell);
  1224. } else {
  1225. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1226. h->shared_read_entry = dm_deferred_entry_inc(pool->shared_read_ds);
  1227. inc_all_io_entry(pool, bio);
  1228. remap_and_issue(tc, bio, lookup_result->block);
  1229. remap_and_issue_shared_cell(tc, data_cell, lookup_result->block);
  1230. remap_and_issue_shared_cell(tc, virt_cell, lookup_result->block);
  1231. }
  1232. }
  1233. static void provision_block(struct thin_c *tc, struct bio *bio, dm_block_t block,
  1234. struct dm_bio_prison_cell *cell)
  1235. {
  1236. int r;
  1237. dm_block_t data_block;
  1238. struct pool *pool = tc->pool;
  1239. /*
  1240. * Remap empty bios (flushes) immediately, without provisioning.
  1241. */
  1242. if (!bio->bi_iter.bi_size) {
  1243. inc_all_io_entry(pool, bio);
  1244. cell_defer_no_holder(tc, cell);
  1245. remap_and_issue(tc, bio, 0);
  1246. return;
  1247. }
  1248. /*
  1249. * Fill read bios with zeroes and complete them immediately.
  1250. */
  1251. if (bio_data_dir(bio) == READ) {
  1252. zero_fill_bio(bio);
  1253. cell_defer_no_holder(tc, cell);
  1254. bio_endio(bio, 0);
  1255. return;
  1256. }
  1257. r = alloc_data_block(tc, &data_block);
  1258. switch (r) {
  1259. case 0:
  1260. if (tc->origin_dev)
  1261. schedule_external_copy(tc, block, data_block, cell, bio);
  1262. else
  1263. schedule_zero(tc, block, data_block, cell, bio);
  1264. break;
  1265. case -ENOSPC:
  1266. retry_bios_on_resume(pool, cell);
  1267. break;
  1268. default:
  1269. DMERR_LIMIT("%s: alloc_data_block() failed: error = %d",
  1270. __func__, r);
  1271. cell_error(pool, cell);
  1272. break;
  1273. }
  1274. }
  1275. static void process_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1276. {
  1277. int r;
  1278. struct pool *pool = tc->pool;
  1279. struct bio *bio = cell->holder;
  1280. dm_block_t block = get_bio_block(tc, bio);
  1281. struct dm_thin_lookup_result lookup_result;
  1282. if (tc->requeue_mode) {
  1283. cell_requeue(pool, cell);
  1284. return;
  1285. }
  1286. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1287. switch (r) {
  1288. case 0:
  1289. if (lookup_result.shared)
  1290. process_shared_bio(tc, bio, block, &lookup_result, cell);
  1291. else {
  1292. inc_all_io_entry(pool, bio);
  1293. remap_and_issue(tc, bio, lookup_result.block);
  1294. inc_remap_and_issue_cell(tc, cell, lookup_result.block);
  1295. }
  1296. break;
  1297. case -ENODATA:
  1298. if (bio_data_dir(bio) == READ && tc->origin_dev) {
  1299. inc_all_io_entry(pool, bio);
  1300. cell_defer_no_holder(tc, cell);
  1301. if (bio_end_sector(bio) <= tc->origin_size)
  1302. remap_to_origin_and_issue(tc, bio);
  1303. else if (bio->bi_iter.bi_sector < tc->origin_size) {
  1304. zero_fill_bio(bio);
  1305. bio->bi_iter.bi_size = (tc->origin_size - bio->bi_iter.bi_sector) << SECTOR_SHIFT;
  1306. remap_to_origin_and_issue(tc, bio);
  1307. } else {
  1308. zero_fill_bio(bio);
  1309. bio_endio(bio, 0);
  1310. }
  1311. } else
  1312. provision_block(tc, bio, block, cell);
  1313. break;
  1314. default:
  1315. DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
  1316. __func__, r);
  1317. cell_defer_no_holder(tc, cell);
  1318. bio_io_error(bio);
  1319. break;
  1320. }
  1321. }
  1322. static void process_bio(struct thin_c *tc, struct bio *bio)
  1323. {
  1324. struct pool *pool = tc->pool;
  1325. dm_block_t block = get_bio_block(tc, bio);
  1326. struct dm_bio_prison_cell *cell;
  1327. struct dm_cell_key key;
  1328. /*
  1329. * If cell is already occupied, then the block is already
  1330. * being provisioned so we have nothing further to do here.
  1331. */
  1332. build_virtual_key(tc->td, block, &key);
  1333. if (bio_detain(pool, &key, bio, &cell))
  1334. return;
  1335. process_cell(tc, cell);
  1336. }
  1337. static void __process_bio_read_only(struct thin_c *tc, struct bio *bio,
  1338. struct dm_bio_prison_cell *cell)
  1339. {
  1340. int r;
  1341. int rw = bio_data_dir(bio);
  1342. dm_block_t block = get_bio_block(tc, bio);
  1343. struct dm_thin_lookup_result lookup_result;
  1344. r = dm_thin_find_block(tc->td, block, 1, &lookup_result);
  1345. switch (r) {
  1346. case 0:
  1347. if (lookup_result.shared && (rw == WRITE) && bio->bi_iter.bi_size) {
  1348. handle_unserviceable_bio(tc->pool, bio);
  1349. if (cell)
  1350. cell_defer_no_holder(tc, cell);
  1351. } else {
  1352. inc_all_io_entry(tc->pool, bio);
  1353. remap_and_issue(tc, bio, lookup_result.block);
  1354. if (cell)
  1355. inc_remap_and_issue_cell(tc, cell, lookup_result.block);
  1356. }
  1357. break;
  1358. case -ENODATA:
  1359. if (cell)
  1360. cell_defer_no_holder(tc, cell);
  1361. if (rw != READ) {
  1362. handle_unserviceable_bio(tc->pool, bio);
  1363. break;
  1364. }
  1365. if (tc->origin_dev) {
  1366. inc_all_io_entry(tc->pool, bio);
  1367. remap_to_origin_and_issue(tc, bio);
  1368. break;
  1369. }
  1370. zero_fill_bio(bio);
  1371. bio_endio(bio, 0);
  1372. break;
  1373. default:
  1374. DMERR_LIMIT("%s: dm_thin_find_block() failed: error = %d",
  1375. __func__, r);
  1376. if (cell)
  1377. cell_defer_no_holder(tc, cell);
  1378. bio_io_error(bio);
  1379. break;
  1380. }
  1381. }
  1382. static void process_bio_read_only(struct thin_c *tc, struct bio *bio)
  1383. {
  1384. __process_bio_read_only(tc, bio, NULL);
  1385. }
  1386. static void process_cell_read_only(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1387. {
  1388. __process_bio_read_only(tc, cell->holder, cell);
  1389. }
  1390. static void process_bio_success(struct thin_c *tc, struct bio *bio)
  1391. {
  1392. bio_endio(bio, 0);
  1393. }
  1394. static void process_bio_fail(struct thin_c *tc, struct bio *bio)
  1395. {
  1396. bio_io_error(bio);
  1397. }
  1398. static void process_cell_success(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1399. {
  1400. cell_success(tc->pool, cell);
  1401. }
  1402. static void process_cell_fail(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1403. {
  1404. cell_error(tc->pool, cell);
  1405. }
  1406. /*
  1407. * FIXME: should we also commit due to size of transaction, measured in
  1408. * metadata blocks?
  1409. */
  1410. static int need_commit_due_to_time(struct pool *pool)
  1411. {
  1412. return jiffies < pool->last_commit_jiffies ||
  1413. jiffies > pool->last_commit_jiffies + COMMIT_PERIOD;
  1414. }
  1415. #define thin_pbd(node) rb_entry((node), struct dm_thin_endio_hook, rb_node)
  1416. #define thin_bio(pbd) dm_bio_from_per_bio_data((pbd), sizeof(struct dm_thin_endio_hook))
  1417. static void __thin_bio_rb_add(struct thin_c *tc, struct bio *bio)
  1418. {
  1419. struct rb_node **rbp, *parent;
  1420. struct dm_thin_endio_hook *pbd;
  1421. sector_t bi_sector = bio->bi_iter.bi_sector;
  1422. rbp = &tc->sort_bio_list.rb_node;
  1423. parent = NULL;
  1424. while (*rbp) {
  1425. parent = *rbp;
  1426. pbd = thin_pbd(parent);
  1427. if (bi_sector < thin_bio(pbd)->bi_iter.bi_sector)
  1428. rbp = &(*rbp)->rb_left;
  1429. else
  1430. rbp = &(*rbp)->rb_right;
  1431. }
  1432. pbd = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1433. rb_link_node(&pbd->rb_node, parent, rbp);
  1434. rb_insert_color(&pbd->rb_node, &tc->sort_bio_list);
  1435. }
  1436. static void __extract_sorted_bios(struct thin_c *tc)
  1437. {
  1438. struct rb_node *node;
  1439. struct dm_thin_endio_hook *pbd;
  1440. struct bio *bio;
  1441. for (node = rb_first(&tc->sort_bio_list); node; node = rb_next(node)) {
  1442. pbd = thin_pbd(node);
  1443. bio = thin_bio(pbd);
  1444. bio_list_add(&tc->deferred_bio_list, bio);
  1445. rb_erase(&pbd->rb_node, &tc->sort_bio_list);
  1446. }
  1447. WARN_ON(!RB_EMPTY_ROOT(&tc->sort_bio_list));
  1448. }
  1449. static void __sort_thin_deferred_bios(struct thin_c *tc)
  1450. {
  1451. struct bio *bio;
  1452. struct bio_list bios;
  1453. bio_list_init(&bios);
  1454. bio_list_merge(&bios, &tc->deferred_bio_list);
  1455. bio_list_init(&tc->deferred_bio_list);
  1456. /* Sort deferred_bio_list using rb-tree */
  1457. while ((bio = bio_list_pop(&bios)))
  1458. __thin_bio_rb_add(tc, bio);
  1459. /*
  1460. * Transfer the sorted bios in sort_bio_list back to
  1461. * deferred_bio_list to allow lockless submission of
  1462. * all bios.
  1463. */
  1464. __extract_sorted_bios(tc);
  1465. }
  1466. static void process_thin_deferred_bios(struct thin_c *tc)
  1467. {
  1468. struct pool *pool = tc->pool;
  1469. unsigned long flags;
  1470. struct bio *bio;
  1471. struct bio_list bios;
  1472. struct blk_plug plug;
  1473. unsigned count = 0;
  1474. if (tc->requeue_mode) {
  1475. error_thin_bio_list(tc, &tc->deferred_bio_list, DM_ENDIO_REQUEUE);
  1476. return;
  1477. }
  1478. bio_list_init(&bios);
  1479. spin_lock_irqsave(&tc->lock, flags);
  1480. if (bio_list_empty(&tc->deferred_bio_list)) {
  1481. spin_unlock_irqrestore(&tc->lock, flags);
  1482. return;
  1483. }
  1484. __sort_thin_deferred_bios(tc);
  1485. bio_list_merge(&bios, &tc->deferred_bio_list);
  1486. bio_list_init(&tc->deferred_bio_list);
  1487. spin_unlock_irqrestore(&tc->lock, flags);
  1488. blk_start_plug(&plug);
  1489. while ((bio = bio_list_pop(&bios))) {
  1490. /*
  1491. * If we've got no free new_mapping structs, and processing
  1492. * this bio might require one, we pause until there are some
  1493. * prepared mappings to process.
  1494. */
  1495. if (ensure_next_mapping(pool)) {
  1496. spin_lock_irqsave(&tc->lock, flags);
  1497. bio_list_add(&tc->deferred_bio_list, bio);
  1498. bio_list_merge(&tc->deferred_bio_list, &bios);
  1499. spin_unlock_irqrestore(&tc->lock, flags);
  1500. break;
  1501. }
  1502. if (bio->bi_rw & REQ_DISCARD)
  1503. pool->process_discard(tc, bio);
  1504. else
  1505. pool->process_bio(tc, bio);
  1506. if ((count++ & 127) == 0) {
  1507. throttle_work_update(&pool->throttle);
  1508. dm_pool_issue_prefetches(pool->pmd);
  1509. }
  1510. }
  1511. blk_finish_plug(&plug);
  1512. }
  1513. static int cmp_cells(const void *lhs, const void *rhs)
  1514. {
  1515. struct dm_bio_prison_cell *lhs_cell = *((struct dm_bio_prison_cell **) lhs);
  1516. struct dm_bio_prison_cell *rhs_cell = *((struct dm_bio_prison_cell **) rhs);
  1517. BUG_ON(!lhs_cell->holder);
  1518. BUG_ON(!rhs_cell->holder);
  1519. if (lhs_cell->holder->bi_iter.bi_sector < rhs_cell->holder->bi_iter.bi_sector)
  1520. return -1;
  1521. if (lhs_cell->holder->bi_iter.bi_sector > rhs_cell->holder->bi_iter.bi_sector)
  1522. return 1;
  1523. return 0;
  1524. }
  1525. static unsigned sort_cells(struct pool *pool, struct list_head *cells)
  1526. {
  1527. unsigned count = 0;
  1528. struct dm_bio_prison_cell *cell, *tmp;
  1529. list_for_each_entry_safe(cell, tmp, cells, user_list) {
  1530. if (count >= CELL_SORT_ARRAY_SIZE)
  1531. break;
  1532. pool->cell_sort_array[count++] = cell;
  1533. list_del(&cell->user_list);
  1534. }
  1535. sort(pool->cell_sort_array, count, sizeof(cell), cmp_cells, NULL);
  1536. return count;
  1537. }
  1538. static void process_thin_deferred_cells(struct thin_c *tc)
  1539. {
  1540. struct pool *pool = tc->pool;
  1541. unsigned long flags;
  1542. struct list_head cells;
  1543. struct dm_bio_prison_cell *cell;
  1544. unsigned i, j, count;
  1545. INIT_LIST_HEAD(&cells);
  1546. spin_lock_irqsave(&tc->lock, flags);
  1547. list_splice_init(&tc->deferred_cells, &cells);
  1548. spin_unlock_irqrestore(&tc->lock, flags);
  1549. if (list_empty(&cells))
  1550. return;
  1551. do {
  1552. count = sort_cells(tc->pool, &cells);
  1553. for (i = 0; i < count; i++) {
  1554. cell = pool->cell_sort_array[i];
  1555. BUG_ON(!cell->holder);
  1556. /*
  1557. * If we've got no free new_mapping structs, and processing
  1558. * this bio might require one, we pause until there are some
  1559. * prepared mappings to process.
  1560. */
  1561. if (ensure_next_mapping(pool)) {
  1562. for (j = i; j < count; j++)
  1563. list_add(&pool->cell_sort_array[j]->user_list, &cells);
  1564. spin_lock_irqsave(&tc->lock, flags);
  1565. list_splice(&cells, &tc->deferred_cells);
  1566. spin_unlock_irqrestore(&tc->lock, flags);
  1567. return;
  1568. }
  1569. if (cell->holder->bi_rw & REQ_DISCARD)
  1570. pool->process_discard_cell(tc, cell);
  1571. else
  1572. pool->process_cell(tc, cell);
  1573. }
  1574. } while (!list_empty(&cells));
  1575. }
  1576. static void thin_get(struct thin_c *tc);
  1577. static void thin_put(struct thin_c *tc);
  1578. /*
  1579. * We can't hold rcu_read_lock() around code that can block. So we
  1580. * find a thin with the rcu lock held; bump a refcount; then drop
  1581. * the lock.
  1582. */
  1583. static struct thin_c *get_first_thin(struct pool *pool)
  1584. {
  1585. struct thin_c *tc = NULL;
  1586. rcu_read_lock();
  1587. if (!list_empty(&pool->active_thins)) {
  1588. tc = list_entry_rcu(pool->active_thins.next, struct thin_c, list);
  1589. thin_get(tc);
  1590. }
  1591. rcu_read_unlock();
  1592. return tc;
  1593. }
  1594. static struct thin_c *get_next_thin(struct pool *pool, struct thin_c *tc)
  1595. {
  1596. struct thin_c *old_tc = tc;
  1597. rcu_read_lock();
  1598. list_for_each_entry_continue_rcu(tc, &pool->active_thins, list) {
  1599. thin_get(tc);
  1600. thin_put(old_tc);
  1601. rcu_read_unlock();
  1602. return tc;
  1603. }
  1604. thin_put(old_tc);
  1605. rcu_read_unlock();
  1606. return NULL;
  1607. }
  1608. static void process_deferred_bios(struct pool *pool)
  1609. {
  1610. unsigned long flags;
  1611. struct bio *bio;
  1612. struct bio_list bios;
  1613. struct thin_c *tc;
  1614. tc = get_first_thin(pool);
  1615. while (tc) {
  1616. process_thin_deferred_cells(tc);
  1617. process_thin_deferred_bios(tc);
  1618. tc = get_next_thin(pool, tc);
  1619. }
  1620. /*
  1621. * If there are any deferred flush bios, we must commit
  1622. * the metadata before issuing them.
  1623. */
  1624. bio_list_init(&bios);
  1625. spin_lock_irqsave(&pool->lock, flags);
  1626. bio_list_merge(&bios, &pool->deferred_flush_bios);
  1627. bio_list_init(&pool->deferred_flush_bios);
  1628. spin_unlock_irqrestore(&pool->lock, flags);
  1629. if (bio_list_empty(&bios) &&
  1630. !(dm_pool_changed_this_transaction(pool->pmd) && need_commit_due_to_time(pool)))
  1631. return;
  1632. if (commit(pool)) {
  1633. while ((bio = bio_list_pop(&bios)))
  1634. bio_io_error(bio);
  1635. return;
  1636. }
  1637. pool->last_commit_jiffies = jiffies;
  1638. while ((bio = bio_list_pop(&bios)))
  1639. generic_make_request(bio);
  1640. }
  1641. static void do_worker(struct work_struct *ws)
  1642. {
  1643. struct pool *pool = container_of(ws, struct pool, worker);
  1644. throttle_work_start(&pool->throttle);
  1645. dm_pool_issue_prefetches(pool->pmd);
  1646. throttle_work_update(&pool->throttle);
  1647. process_prepared(pool, &pool->prepared_mappings, &pool->process_prepared_mapping);
  1648. throttle_work_update(&pool->throttle);
  1649. process_prepared(pool, &pool->prepared_discards, &pool->process_prepared_discard);
  1650. throttle_work_update(&pool->throttle);
  1651. process_deferred_bios(pool);
  1652. throttle_work_complete(&pool->throttle);
  1653. }
  1654. /*
  1655. * We want to commit periodically so that not too much
  1656. * unwritten data builds up.
  1657. */
  1658. static void do_waker(struct work_struct *ws)
  1659. {
  1660. struct pool *pool = container_of(to_delayed_work(ws), struct pool, waker);
  1661. wake_worker(pool);
  1662. queue_delayed_work(pool->wq, &pool->waker, COMMIT_PERIOD);
  1663. }
  1664. /*
  1665. * We're holding onto IO to allow userland time to react. After the
  1666. * timeout either the pool will have been resized (and thus back in
  1667. * PM_WRITE mode), or we degrade to PM_READ_ONLY and start erroring IO.
  1668. */
  1669. static void do_no_space_timeout(struct work_struct *ws)
  1670. {
  1671. struct pool *pool = container_of(to_delayed_work(ws), struct pool,
  1672. no_space_timeout);
  1673. if (get_pool_mode(pool) == PM_OUT_OF_DATA_SPACE && !pool->pf.error_if_no_space)
  1674. set_pool_mode(pool, PM_READ_ONLY);
  1675. }
  1676. /*----------------------------------------------------------------*/
  1677. struct pool_work {
  1678. struct work_struct worker;
  1679. struct completion complete;
  1680. };
  1681. static struct pool_work *to_pool_work(struct work_struct *ws)
  1682. {
  1683. return container_of(ws, struct pool_work, worker);
  1684. }
  1685. static void pool_work_complete(struct pool_work *pw)
  1686. {
  1687. complete(&pw->complete);
  1688. }
  1689. static void pool_work_wait(struct pool_work *pw, struct pool *pool,
  1690. void (*fn)(struct work_struct *))
  1691. {
  1692. INIT_WORK_ONSTACK(&pw->worker, fn);
  1693. init_completion(&pw->complete);
  1694. queue_work(pool->wq, &pw->worker);
  1695. wait_for_completion(&pw->complete);
  1696. }
  1697. /*----------------------------------------------------------------*/
  1698. struct noflush_work {
  1699. struct pool_work pw;
  1700. struct thin_c *tc;
  1701. };
  1702. static struct noflush_work *to_noflush(struct work_struct *ws)
  1703. {
  1704. return container_of(to_pool_work(ws), struct noflush_work, pw);
  1705. }
  1706. static void do_noflush_start(struct work_struct *ws)
  1707. {
  1708. struct noflush_work *w = to_noflush(ws);
  1709. w->tc->requeue_mode = true;
  1710. requeue_io(w->tc);
  1711. pool_work_complete(&w->pw);
  1712. }
  1713. static void do_noflush_stop(struct work_struct *ws)
  1714. {
  1715. struct noflush_work *w = to_noflush(ws);
  1716. w->tc->requeue_mode = false;
  1717. pool_work_complete(&w->pw);
  1718. }
  1719. static void noflush_work(struct thin_c *tc, void (*fn)(struct work_struct *))
  1720. {
  1721. struct noflush_work w;
  1722. w.tc = tc;
  1723. pool_work_wait(&w.pw, tc->pool, fn);
  1724. }
  1725. /*----------------------------------------------------------------*/
  1726. static enum pool_mode get_pool_mode(struct pool *pool)
  1727. {
  1728. return pool->pf.mode;
  1729. }
  1730. static void notify_of_pool_mode_change(struct pool *pool, const char *new_mode)
  1731. {
  1732. dm_table_event(pool->ti->table);
  1733. DMINFO("%s: switching pool to %s mode",
  1734. dm_device_name(pool->pool_md), new_mode);
  1735. }
  1736. static void set_pool_mode(struct pool *pool, enum pool_mode new_mode)
  1737. {
  1738. struct pool_c *pt = pool->ti->private;
  1739. bool needs_check = dm_pool_metadata_needs_check(pool->pmd);
  1740. enum pool_mode old_mode = get_pool_mode(pool);
  1741. unsigned long no_space_timeout = ACCESS_ONCE(no_space_timeout_secs) * HZ;
  1742. /*
  1743. * Never allow the pool to transition to PM_WRITE mode if user
  1744. * intervention is required to verify metadata and data consistency.
  1745. */
  1746. if (new_mode == PM_WRITE && needs_check) {
  1747. DMERR("%s: unable to switch pool to write mode until repaired.",
  1748. dm_device_name(pool->pool_md));
  1749. if (old_mode != new_mode)
  1750. new_mode = old_mode;
  1751. else
  1752. new_mode = PM_READ_ONLY;
  1753. }
  1754. /*
  1755. * If we were in PM_FAIL mode, rollback of metadata failed. We're
  1756. * not going to recover without a thin_repair. So we never let the
  1757. * pool move out of the old mode.
  1758. */
  1759. if (old_mode == PM_FAIL)
  1760. new_mode = old_mode;
  1761. switch (new_mode) {
  1762. case PM_FAIL:
  1763. if (old_mode != new_mode)
  1764. notify_of_pool_mode_change(pool, "failure");
  1765. dm_pool_metadata_read_only(pool->pmd);
  1766. pool->process_bio = process_bio_fail;
  1767. pool->process_discard = process_bio_fail;
  1768. pool->process_cell = process_cell_fail;
  1769. pool->process_discard_cell = process_cell_fail;
  1770. pool->process_prepared_mapping = process_prepared_mapping_fail;
  1771. pool->process_prepared_discard = process_prepared_discard_fail;
  1772. error_retry_list(pool);
  1773. break;
  1774. case PM_READ_ONLY:
  1775. if (old_mode != new_mode)
  1776. notify_of_pool_mode_change(pool, "read-only");
  1777. dm_pool_metadata_read_only(pool->pmd);
  1778. pool->process_bio = process_bio_read_only;
  1779. pool->process_discard = process_bio_success;
  1780. pool->process_cell = process_cell_read_only;
  1781. pool->process_discard_cell = process_cell_success;
  1782. pool->process_prepared_mapping = process_prepared_mapping_fail;
  1783. pool->process_prepared_discard = process_prepared_discard_passdown;
  1784. error_retry_list(pool);
  1785. break;
  1786. case PM_OUT_OF_DATA_SPACE:
  1787. /*
  1788. * Ideally we'd never hit this state; the low water mark
  1789. * would trigger userland to extend the pool before we
  1790. * completely run out of data space. However, many small
  1791. * IOs to unprovisioned space can consume data space at an
  1792. * alarming rate. Adjust your low water mark if you're
  1793. * frequently seeing this mode.
  1794. */
  1795. if (old_mode != new_mode)
  1796. notify_of_pool_mode_change(pool, "out-of-data-space");
  1797. pool->process_bio = process_bio_read_only;
  1798. pool->process_discard = process_discard_bio;
  1799. pool->process_cell = process_cell_read_only;
  1800. pool->process_discard_cell = process_discard_cell;
  1801. pool->process_prepared_mapping = process_prepared_mapping;
  1802. pool->process_prepared_discard = process_prepared_discard;
  1803. if (!pool->pf.error_if_no_space && no_space_timeout)
  1804. queue_delayed_work(pool->wq, &pool->no_space_timeout, no_space_timeout);
  1805. break;
  1806. case PM_WRITE:
  1807. if (old_mode != new_mode)
  1808. notify_of_pool_mode_change(pool, "write");
  1809. dm_pool_metadata_read_write(pool->pmd);
  1810. pool->process_bio = process_bio;
  1811. pool->process_discard = process_discard_bio;
  1812. pool->process_cell = process_cell;
  1813. pool->process_discard_cell = process_discard_cell;
  1814. pool->process_prepared_mapping = process_prepared_mapping;
  1815. pool->process_prepared_discard = process_prepared_discard;
  1816. break;
  1817. }
  1818. pool->pf.mode = new_mode;
  1819. /*
  1820. * The pool mode may have changed, sync it so bind_control_target()
  1821. * doesn't cause an unexpected mode transition on resume.
  1822. */
  1823. pt->adjusted_pf.mode = new_mode;
  1824. }
  1825. static void abort_transaction(struct pool *pool)
  1826. {
  1827. const char *dev_name = dm_device_name(pool->pool_md);
  1828. DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
  1829. if (dm_pool_abort_metadata(pool->pmd)) {
  1830. DMERR("%s: failed to abort metadata transaction", dev_name);
  1831. set_pool_mode(pool, PM_FAIL);
  1832. }
  1833. if (dm_pool_metadata_set_needs_check(pool->pmd)) {
  1834. DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
  1835. set_pool_mode(pool, PM_FAIL);
  1836. }
  1837. }
  1838. static void metadata_operation_failed(struct pool *pool, const char *op, int r)
  1839. {
  1840. DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
  1841. dm_device_name(pool->pool_md), op, r);
  1842. abort_transaction(pool);
  1843. set_pool_mode(pool, PM_READ_ONLY);
  1844. }
  1845. /*----------------------------------------------------------------*/
  1846. /*
  1847. * Mapping functions.
  1848. */
  1849. /*
  1850. * Called only while mapping a thin bio to hand it over to the workqueue.
  1851. */
  1852. static void thin_defer_bio(struct thin_c *tc, struct bio *bio)
  1853. {
  1854. unsigned long flags;
  1855. struct pool *pool = tc->pool;
  1856. spin_lock_irqsave(&tc->lock, flags);
  1857. bio_list_add(&tc->deferred_bio_list, bio);
  1858. spin_unlock_irqrestore(&tc->lock, flags);
  1859. wake_worker(pool);
  1860. }
  1861. static void thin_defer_bio_with_throttle(struct thin_c *tc, struct bio *bio)
  1862. {
  1863. struct pool *pool = tc->pool;
  1864. throttle_lock(&pool->throttle);
  1865. thin_defer_bio(tc, bio);
  1866. throttle_unlock(&pool->throttle);
  1867. }
  1868. static void thin_defer_cell(struct thin_c *tc, struct dm_bio_prison_cell *cell)
  1869. {
  1870. unsigned long flags;
  1871. struct pool *pool = tc->pool;
  1872. throttle_lock(&pool->throttle);
  1873. spin_lock_irqsave(&tc->lock, flags);
  1874. list_add_tail(&cell->user_list, &tc->deferred_cells);
  1875. spin_unlock_irqrestore(&tc->lock, flags);
  1876. throttle_unlock(&pool->throttle);
  1877. wake_worker(pool);
  1878. }
  1879. static void thin_hook_bio(struct thin_c *tc, struct bio *bio)
  1880. {
  1881. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  1882. h->tc = tc;
  1883. h->shared_read_entry = NULL;
  1884. h->all_io_entry = NULL;
  1885. h->overwrite_mapping = NULL;
  1886. }
  1887. /*
  1888. * Non-blocking function called from the thin target's map function.
  1889. */
  1890. static int thin_bio_map(struct dm_target *ti, struct bio *bio)
  1891. {
  1892. int r;
  1893. struct thin_c *tc = ti->private;
  1894. dm_block_t block = get_bio_block(tc, bio);
  1895. struct dm_thin_device *td = tc->td;
  1896. struct dm_thin_lookup_result result;
  1897. struct dm_bio_prison_cell *virt_cell, *data_cell;
  1898. struct dm_cell_key key;
  1899. thin_hook_bio(tc, bio);
  1900. if (tc->requeue_mode) {
  1901. bio_endio(bio, DM_ENDIO_REQUEUE);
  1902. return DM_MAPIO_SUBMITTED;
  1903. }
  1904. if (get_pool_mode(tc->pool) == PM_FAIL) {
  1905. bio_io_error(bio);
  1906. return DM_MAPIO_SUBMITTED;
  1907. }
  1908. if (bio->bi_rw & (REQ_DISCARD | REQ_FLUSH | REQ_FUA)) {
  1909. thin_defer_bio_with_throttle(tc, bio);
  1910. return DM_MAPIO_SUBMITTED;
  1911. }
  1912. /*
  1913. * We must hold the virtual cell before doing the lookup, otherwise
  1914. * there's a race with discard.
  1915. */
  1916. build_virtual_key(tc->td, block, &key);
  1917. if (bio_detain(tc->pool, &key, bio, &virt_cell))
  1918. return DM_MAPIO_SUBMITTED;
  1919. r = dm_thin_find_block(td, block, 0, &result);
  1920. /*
  1921. * Note that we defer readahead too.
  1922. */
  1923. switch (r) {
  1924. case 0:
  1925. if (unlikely(result.shared)) {
  1926. /*
  1927. * We have a race condition here between the
  1928. * result.shared value returned by the lookup and
  1929. * snapshot creation, which may cause new
  1930. * sharing.
  1931. *
  1932. * To avoid this always quiesce the origin before
  1933. * taking the snap. You want to do this anyway to
  1934. * ensure a consistent application view
  1935. * (i.e. lockfs).
  1936. *
  1937. * More distant ancestors are irrelevant. The
  1938. * shared flag will be set in their case.
  1939. */
  1940. thin_defer_cell(tc, virt_cell);
  1941. return DM_MAPIO_SUBMITTED;
  1942. }
  1943. build_data_key(tc->td, result.block, &key);
  1944. if (bio_detain(tc->pool, &key, bio, &data_cell)) {
  1945. cell_defer_no_holder(tc, virt_cell);
  1946. return DM_MAPIO_SUBMITTED;
  1947. }
  1948. inc_all_io_entry(tc->pool, bio);
  1949. cell_defer_no_holder(tc, data_cell);
  1950. cell_defer_no_holder(tc, virt_cell);
  1951. remap(tc, bio, result.block);
  1952. return DM_MAPIO_REMAPPED;
  1953. case -ENODATA:
  1954. if (get_pool_mode(tc->pool) == PM_READ_ONLY) {
  1955. /*
  1956. * This block isn't provisioned, and we have no way
  1957. * of doing so.
  1958. */
  1959. handle_unserviceable_bio(tc->pool, bio);
  1960. cell_defer_no_holder(tc, virt_cell);
  1961. return DM_MAPIO_SUBMITTED;
  1962. }
  1963. /* fall through */
  1964. case -EWOULDBLOCK:
  1965. thin_defer_cell(tc, virt_cell);
  1966. return DM_MAPIO_SUBMITTED;
  1967. default:
  1968. /*
  1969. * Must always call bio_io_error on failure.
  1970. * dm_thin_find_block can fail with -EINVAL if the
  1971. * pool is switched to fail-io mode.
  1972. */
  1973. bio_io_error(bio);
  1974. cell_defer_no_holder(tc, virt_cell);
  1975. return DM_MAPIO_SUBMITTED;
  1976. }
  1977. }
  1978. static int pool_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1979. {
  1980. struct pool_c *pt = container_of(cb, struct pool_c, callbacks);
  1981. struct request_queue *q;
  1982. if (get_pool_mode(pt->pool) == PM_OUT_OF_DATA_SPACE)
  1983. return 1;
  1984. q = bdev_get_queue(pt->data_dev->bdev);
  1985. return bdi_congested(&q->backing_dev_info, bdi_bits);
  1986. }
  1987. static void requeue_bios(struct pool *pool)
  1988. {
  1989. unsigned long flags;
  1990. struct thin_c *tc;
  1991. rcu_read_lock();
  1992. list_for_each_entry_rcu(tc, &pool->active_thins, list) {
  1993. spin_lock_irqsave(&tc->lock, flags);
  1994. bio_list_merge(&tc->deferred_bio_list, &tc->retry_on_resume_list);
  1995. bio_list_init(&tc->retry_on_resume_list);
  1996. spin_unlock_irqrestore(&tc->lock, flags);
  1997. }
  1998. rcu_read_unlock();
  1999. }
  2000. /*----------------------------------------------------------------
  2001. * Binding of control targets to a pool object
  2002. *--------------------------------------------------------------*/
  2003. static bool data_dev_supports_discard(struct pool_c *pt)
  2004. {
  2005. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  2006. return q && blk_queue_discard(q);
  2007. }
  2008. static bool is_factor(sector_t block_size, uint32_t n)
  2009. {
  2010. return !sector_div(block_size, n);
  2011. }
  2012. /*
  2013. * If discard_passdown was enabled verify that the data device
  2014. * supports discards. Disable discard_passdown if not.
  2015. */
  2016. static void disable_passdown_if_not_supported(struct pool_c *pt)
  2017. {
  2018. struct pool *pool = pt->pool;
  2019. struct block_device *data_bdev = pt->data_dev->bdev;
  2020. struct queue_limits *data_limits = &bdev_get_queue(data_bdev)->limits;
  2021. sector_t block_size = pool->sectors_per_block << SECTOR_SHIFT;
  2022. const char *reason = NULL;
  2023. char buf[BDEVNAME_SIZE];
  2024. if (!pt->adjusted_pf.discard_passdown)
  2025. return;
  2026. if (!data_dev_supports_discard(pt))
  2027. reason = "discard unsupported";
  2028. else if (data_limits->max_discard_sectors < pool->sectors_per_block)
  2029. reason = "max discard sectors smaller than a block";
  2030. else if (data_limits->discard_granularity > block_size)
  2031. reason = "discard granularity larger than a block";
  2032. else if (!is_factor(block_size, data_limits->discard_granularity))
  2033. reason = "discard granularity not a factor of block size";
  2034. if (reason) {
  2035. DMWARN("Data device (%s) %s: Disabling discard passdown.", bdevname(data_bdev, buf), reason);
  2036. pt->adjusted_pf.discard_passdown = false;
  2037. }
  2038. }
  2039. static int bind_control_target(struct pool *pool, struct dm_target *ti)
  2040. {
  2041. struct pool_c *pt = ti->private;
  2042. /*
  2043. * We want to make sure that a pool in PM_FAIL mode is never upgraded.
  2044. */
  2045. enum pool_mode old_mode = get_pool_mode(pool);
  2046. enum pool_mode new_mode = pt->adjusted_pf.mode;
  2047. /*
  2048. * Don't change the pool's mode until set_pool_mode() below.
  2049. * Otherwise the pool's process_* function pointers may
  2050. * not match the desired pool mode.
  2051. */
  2052. pt->adjusted_pf.mode = old_mode;
  2053. pool->ti = ti;
  2054. pool->pf = pt->adjusted_pf;
  2055. pool->low_water_blocks = pt->low_water_blocks;
  2056. set_pool_mode(pool, new_mode);
  2057. return 0;
  2058. }
  2059. static void unbind_control_target(struct pool *pool, struct dm_target *ti)
  2060. {
  2061. if (pool->ti == ti)
  2062. pool->ti = NULL;
  2063. }
  2064. /*----------------------------------------------------------------
  2065. * Pool creation
  2066. *--------------------------------------------------------------*/
  2067. /* Initialize pool features. */
  2068. static void pool_features_init(struct pool_features *pf)
  2069. {
  2070. pf->mode = PM_WRITE;
  2071. pf->zero_new_blocks = true;
  2072. pf->discard_enabled = true;
  2073. pf->discard_passdown = true;
  2074. pf->error_if_no_space = false;
  2075. }
  2076. static void __pool_destroy(struct pool *pool)
  2077. {
  2078. __pool_table_remove(pool);
  2079. if (dm_pool_metadata_close(pool->pmd) < 0)
  2080. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  2081. dm_bio_prison_destroy(pool->prison);
  2082. dm_kcopyd_client_destroy(pool->copier);
  2083. if (pool->wq)
  2084. destroy_workqueue(pool->wq);
  2085. if (pool->next_mapping)
  2086. mempool_free(pool->next_mapping, pool->mapping_pool);
  2087. mempool_destroy(pool->mapping_pool);
  2088. dm_deferred_set_destroy(pool->shared_read_ds);
  2089. dm_deferred_set_destroy(pool->all_io_ds);
  2090. kfree(pool);
  2091. }
  2092. static struct kmem_cache *_new_mapping_cache;
  2093. static struct pool *pool_create(struct mapped_device *pool_md,
  2094. struct block_device *metadata_dev,
  2095. unsigned long block_size,
  2096. int read_only, char **error)
  2097. {
  2098. int r;
  2099. void *err_p;
  2100. struct pool *pool;
  2101. struct dm_pool_metadata *pmd;
  2102. bool format_device = read_only ? false : true;
  2103. pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
  2104. if (IS_ERR(pmd)) {
  2105. *error = "Error creating metadata object";
  2106. return (struct pool *)pmd;
  2107. }
  2108. pool = kmalloc(sizeof(*pool), GFP_KERNEL);
  2109. if (!pool) {
  2110. *error = "Error allocating memory for pool";
  2111. err_p = ERR_PTR(-ENOMEM);
  2112. goto bad_pool;
  2113. }
  2114. pool->pmd = pmd;
  2115. pool->sectors_per_block = block_size;
  2116. if (block_size & (block_size - 1))
  2117. pool->sectors_per_block_shift = -1;
  2118. else
  2119. pool->sectors_per_block_shift = __ffs(block_size);
  2120. pool->low_water_blocks = 0;
  2121. pool_features_init(&pool->pf);
  2122. pool->prison = dm_bio_prison_create();
  2123. if (!pool->prison) {
  2124. *error = "Error creating pool's bio prison";
  2125. err_p = ERR_PTR(-ENOMEM);
  2126. goto bad_prison;
  2127. }
  2128. pool->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  2129. if (IS_ERR(pool->copier)) {
  2130. r = PTR_ERR(pool->copier);
  2131. *error = "Error creating pool's kcopyd client";
  2132. err_p = ERR_PTR(r);
  2133. goto bad_kcopyd_client;
  2134. }
  2135. /*
  2136. * Create singlethreaded workqueue that will service all devices
  2137. * that use this metadata.
  2138. */
  2139. pool->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  2140. if (!pool->wq) {
  2141. *error = "Error creating pool's workqueue";
  2142. err_p = ERR_PTR(-ENOMEM);
  2143. goto bad_wq;
  2144. }
  2145. throttle_init(&pool->throttle);
  2146. INIT_WORK(&pool->worker, do_worker);
  2147. INIT_DELAYED_WORK(&pool->waker, do_waker);
  2148. INIT_DELAYED_WORK(&pool->no_space_timeout, do_no_space_timeout);
  2149. spin_lock_init(&pool->lock);
  2150. bio_list_init(&pool->deferred_flush_bios);
  2151. INIT_LIST_HEAD(&pool->prepared_mappings);
  2152. INIT_LIST_HEAD(&pool->prepared_discards);
  2153. INIT_LIST_HEAD(&pool->active_thins);
  2154. pool->low_water_triggered = false;
  2155. pool->suspended = true;
  2156. pool->shared_read_ds = dm_deferred_set_create();
  2157. if (!pool->shared_read_ds) {
  2158. *error = "Error creating pool's shared read deferred set";
  2159. err_p = ERR_PTR(-ENOMEM);
  2160. goto bad_shared_read_ds;
  2161. }
  2162. pool->all_io_ds = dm_deferred_set_create();
  2163. if (!pool->all_io_ds) {
  2164. *error = "Error creating pool's all io deferred set";
  2165. err_p = ERR_PTR(-ENOMEM);
  2166. goto bad_all_io_ds;
  2167. }
  2168. pool->next_mapping = NULL;
  2169. pool->mapping_pool = mempool_create_slab_pool(MAPPING_POOL_SIZE,
  2170. _new_mapping_cache);
  2171. if (!pool->mapping_pool) {
  2172. *error = "Error creating pool's mapping mempool";
  2173. err_p = ERR_PTR(-ENOMEM);
  2174. goto bad_mapping_pool;
  2175. }
  2176. pool->ref_count = 1;
  2177. pool->last_commit_jiffies = jiffies;
  2178. pool->pool_md = pool_md;
  2179. pool->md_dev = metadata_dev;
  2180. __pool_table_insert(pool);
  2181. return pool;
  2182. bad_mapping_pool:
  2183. dm_deferred_set_destroy(pool->all_io_ds);
  2184. bad_all_io_ds:
  2185. dm_deferred_set_destroy(pool->shared_read_ds);
  2186. bad_shared_read_ds:
  2187. destroy_workqueue(pool->wq);
  2188. bad_wq:
  2189. dm_kcopyd_client_destroy(pool->copier);
  2190. bad_kcopyd_client:
  2191. dm_bio_prison_destroy(pool->prison);
  2192. bad_prison:
  2193. kfree(pool);
  2194. bad_pool:
  2195. if (dm_pool_metadata_close(pmd))
  2196. DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
  2197. return err_p;
  2198. }
  2199. static void __pool_inc(struct pool *pool)
  2200. {
  2201. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  2202. pool->ref_count++;
  2203. }
  2204. static void __pool_dec(struct pool *pool)
  2205. {
  2206. BUG_ON(!mutex_is_locked(&dm_thin_pool_table.mutex));
  2207. BUG_ON(!pool->ref_count);
  2208. if (!--pool->ref_count)
  2209. __pool_destroy(pool);
  2210. }
  2211. static struct pool *__pool_find(struct mapped_device *pool_md,
  2212. struct block_device *metadata_dev,
  2213. unsigned long block_size, int read_only,
  2214. char **error, int *created)
  2215. {
  2216. struct pool *pool = __pool_table_lookup_metadata_dev(metadata_dev);
  2217. if (pool) {
  2218. if (pool->pool_md != pool_md) {
  2219. *error = "metadata device already in use by a pool";
  2220. return ERR_PTR(-EBUSY);
  2221. }
  2222. __pool_inc(pool);
  2223. } else {
  2224. pool = __pool_table_lookup(pool_md);
  2225. if (pool) {
  2226. if (pool->md_dev != metadata_dev) {
  2227. *error = "different pool cannot replace a pool";
  2228. return ERR_PTR(-EINVAL);
  2229. }
  2230. __pool_inc(pool);
  2231. } else {
  2232. pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
  2233. *created = 1;
  2234. }
  2235. }
  2236. return pool;
  2237. }
  2238. /*----------------------------------------------------------------
  2239. * Pool target methods
  2240. *--------------------------------------------------------------*/
  2241. static void pool_dtr(struct dm_target *ti)
  2242. {
  2243. struct pool_c *pt = ti->private;
  2244. mutex_lock(&dm_thin_pool_table.mutex);
  2245. unbind_control_target(pt->pool, ti);
  2246. __pool_dec(pt->pool);
  2247. dm_put_device(ti, pt->metadata_dev);
  2248. dm_put_device(ti, pt->data_dev);
  2249. kfree(pt);
  2250. mutex_unlock(&dm_thin_pool_table.mutex);
  2251. }
  2252. static int parse_pool_features(struct dm_arg_set *as, struct pool_features *pf,
  2253. struct dm_target *ti)
  2254. {
  2255. int r;
  2256. unsigned argc;
  2257. const char *arg_name;
  2258. static struct dm_arg _args[] = {
  2259. {0, 4, "Invalid number of pool feature arguments"},
  2260. };
  2261. /*
  2262. * No feature arguments supplied.
  2263. */
  2264. if (!as->argc)
  2265. return 0;
  2266. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  2267. if (r)
  2268. return -EINVAL;
  2269. while (argc && !r) {
  2270. arg_name = dm_shift_arg(as);
  2271. argc--;
  2272. if (!strcasecmp(arg_name, "skip_block_zeroing"))
  2273. pf->zero_new_blocks = false;
  2274. else if (!strcasecmp(arg_name, "ignore_discard"))
  2275. pf->discard_enabled = false;
  2276. else if (!strcasecmp(arg_name, "no_discard_passdown"))
  2277. pf->discard_passdown = false;
  2278. else if (!strcasecmp(arg_name, "read_only"))
  2279. pf->mode = PM_READ_ONLY;
  2280. else if (!strcasecmp(arg_name, "error_if_no_space"))
  2281. pf->error_if_no_space = true;
  2282. else {
  2283. ti->error = "Unrecognised pool feature requested";
  2284. r = -EINVAL;
  2285. break;
  2286. }
  2287. }
  2288. return r;
  2289. }
  2290. static void metadata_low_callback(void *context)
  2291. {
  2292. struct pool *pool = context;
  2293. DMWARN("%s: reached low water mark for metadata device: sending event.",
  2294. dm_device_name(pool->pool_md));
  2295. dm_table_event(pool->ti->table);
  2296. }
  2297. static sector_t get_dev_size(struct block_device *bdev)
  2298. {
  2299. return i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
  2300. }
  2301. static void warn_if_metadata_device_too_big(struct block_device *bdev)
  2302. {
  2303. sector_t metadata_dev_size = get_dev_size(bdev);
  2304. char buffer[BDEVNAME_SIZE];
  2305. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS_WARNING)
  2306. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  2307. bdevname(bdev, buffer), THIN_METADATA_MAX_SECTORS);
  2308. }
  2309. static sector_t get_metadata_dev_size(struct block_device *bdev)
  2310. {
  2311. sector_t metadata_dev_size = get_dev_size(bdev);
  2312. if (metadata_dev_size > THIN_METADATA_MAX_SECTORS)
  2313. metadata_dev_size = THIN_METADATA_MAX_SECTORS;
  2314. return metadata_dev_size;
  2315. }
  2316. static dm_block_t get_metadata_dev_size_in_blocks(struct block_device *bdev)
  2317. {
  2318. sector_t metadata_dev_size = get_metadata_dev_size(bdev);
  2319. sector_div(metadata_dev_size, THIN_METADATA_BLOCK_SIZE);
  2320. return metadata_dev_size;
  2321. }
  2322. /*
  2323. * When a metadata threshold is crossed a dm event is triggered, and
  2324. * userland should respond by growing the metadata device. We could let
  2325. * userland set the threshold, like we do with the data threshold, but I'm
  2326. * not sure they know enough to do this well.
  2327. */
  2328. static dm_block_t calc_metadata_threshold(struct pool_c *pt)
  2329. {
  2330. /*
  2331. * 4M is ample for all ops with the possible exception of thin
  2332. * device deletion which is harmless if it fails (just retry the
  2333. * delete after you've grown the device).
  2334. */
  2335. dm_block_t quarter = get_metadata_dev_size_in_blocks(pt->metadata_dev->bdev) / 4;
  2336. return min((dm_block_t)1024ULL /* 4M */, quarter);
  2337. }
  2338. /*
  2339. * thin-pool <metadata dev> <data dev>
  2340. * <data block size (sectors)>
  2341. * <low water mark (blocks)>
  2342. * [<#feature args> [<arg>]*]
  2343. *
  2344. * Optional feature arguments are:
  2345. * skip_block_zeroing: skips the zeroing of newly-provisioned blocks.
  2346. * ignore_discard: disable discard
  2347. * no_discard_passdown: don't pass discards down to the data device
  2348. * read_only: Don't allow any changes to be made to the pool metadata.
  2349. * error_if_no_space: error IOs, instead of queueing, if no space.
  2350. */
  2351. static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2352. {
  2353. int r, pool_created = 0;
  2354. struct pool_c *pt;
  2355. struct pool *pool;
  2356. struct pool_features pf;
  2357. struct dm_arg_set as;
  2358. struct dm_dev *data_dev;
  2359. unsigned long block_size;
  2360. dm_block_t low_water_blocks;
  2361. struct dm_dev *metadata_dev;
  2362. fmode_t metadata_mode;
  2363. /*
  2364. * FIXME Remove validation from scope of lock.
  2365. */
  2366. mutex_lock(&dm_thin_pool_table.mutex);
  2367. if (argc < 4) {
  2368. ti->error = "Invalid argument count";
  2369. r = -EINVAL;
  2370. goto out_unlock;
  2371. }
  2372. as.argc = argc;
  2373. as.argv = argv;
  2374. /*
  2375. * Set default pool features.
  2376. */
  2377. pool_features_init(&pf);
  2378. dm_consume_args(&as, 4);
  2379. r = parse_pool_features(&as, &pf, ti);
  2380. if (r)
  2381. goto out_unlock;
  2382. metadata_mode = FMODE_READ | ((pf.mode == PM_READ_ONLY) ? 0 : FMODE_WRITE);
  2383. r = dm_get_device(ti, argv[0], metadata_mode, &metadata_dev);
  2384. if (r) {
  2385. ti->error = "Error opening metadata block device";
  2386. goto out_unlock;
  2387. }
  2388. warn_if_metadata_device_too_big(metadata_dev->bdev);
  2389. r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &data_dev);
  2390. if (r) {
  2391. ti->error = "Error getting data device";
  2392. goto out_metadata;
  2393. }
  2394. if (kstrtoul(argv[2], 10, &block_size) || !block_size ||
  2395. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  2396. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  2397. block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
  2398. ti->error = "Invalid block size";
  2399. r = -EINVAL;
  2400. goto out;
  2401. }
  2402. if (kstrtoull(argv[3], 10, (unsigned long long *)&low_water_blocks)) {
  2403. ti->error = "Invalid low water mark";
  2404. r = -EINVAL;
  2405. goto out;
  2406. }
  2407. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  2408. if (!pt) {
  2409. r = -ENOMEM;
  2410. goto out;
  2411. }
  2412. pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
  2413. block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
  2414. if (IS_ERR(pool)) {
  2415. r = PTR_ERR(pool);
  2416. goto out_free_pt;
  2417. }
  2418. /*
  2419. * 'pool_created' reflects whether this is the first table load.
  2420. * Top level discard support is not allowed to be changed after
  2421. * initial load. This would require a pool reload to trigger thin
  2422. * device changes.
  2423. */
  2424. if (!pool_created && pf.discard_enabled != pool->pf.discard_enabled) {
  2425. ti->error = "Discard support cannot be disabled once enabled";
  2426. r = -EINVAL;
  2427. goto out_flags_changed;
  2428. }
  2429. pt->pool = pool;
  2430. pt->ti = ti;
  2431. pt->metadata_dev = metadata_dev;
  2432. pt->data_dev = data_dev;
  2433. pt->low_water_blocks = low_water_blocks;
  2434. pt->adjusted_pf = pt->requested_pf = pf;
  2435. ti->num_flush_bios = 1;
  2436. /*
  2437. * Only need to enable discards if the pool should pass
  2438. * them down to the data device. The thin device's discard
  2439. * processing will cause mappings to be removed from the btree.
  2440. */
  2441. ti->discard_zeroes_data_unsupported = true;
  2442. if (pf.discard_enabled && pf.discard_passdown) {
  2443. ti->num_discard_bios = 1;
  2444. /*
  2445. * Setting 'discards_supported' circumvents the normal
  2446. * stacking of discard limits (this keeps the pool and
  2447. * thin devices' discard limits consistent).
  2448. */
  2449. ti->discards_supported = true;
  2450. }
  2451. ti->private = pt;
  2452. r = dm_pool_register_metadata_threshold(pt->pool->pmd,
  2453. calc_metadata_threshold(pt),
  2454. metadata_low_callback,
  2455. pool);
  2456. if (r)
  2457. goto out_free_pt;
  2458. pt->callbacks.congested_fn = pool_is_congested;
  2459. dm_table_add_target_callbacks(ti->table, &pt->callbacks);
  2460. mutex_unlock(&dm_thin_pool_table.mutex);
  2461. return 0;
  2462. out_flags_changed:
  2463. __pool_dec(pool);
  2464. out_free_pt:
  2465. kfree(pt);
  2466. out:
  2467. dm_put_device(ti, data_dev);
  2468. out_metadata:
  2469. dm_put_device(ti, metadata_dev);
  2470. out_unlock:
  2471. mutex_unlock(&dm_thin_pool_table.mutex);
  2472. return r;
  2473. }
  2474. static int pool_map(struct dm_target *ti, struct bio *bio)
  2475. {
  2476. int r;
  2477. struct pool_c *pt = ti->private;
  2478. struct pool *pool = pt->pool;
  2479. unsigned long flags;
  2480. /*
  2481. * As this is a singleton target, ti->begin is always zero.
  2482. */
  2483. spin_lock_irqsave(&pool->lock, flags);
  2484. bio->bi_bdev = pt->data_dev->bdev;
  2485. r = DM_MAPIO_REMAPPED;
  2486. spin_unlock_irqrestore(&pool->lock, flags);
  2487. return r;
  2488. }
  2489. static int maybe_resize_data_dev(struct dm_target *ti, bool *need_commit)
  2490. {
  2491. int r;
  2492. struct pool_c *pt = ti->private;
  2493. struct pool *pool = pt->pool;
  2494. sector_t data_size = ti->len;
  2495. dm_block_t sb_data_size;
  2496. *need_commit = false;
  2497. (void) sector_div(data_size, pool->sectors_per_block);
  2498. r = dm_pool_get_data_dev_size(pool->pmd, &sb_data_size);
  2499. if (r) {
  2500. DMERR("%s: failed to retrieve data device size",
  2501. dm_device_name(pool->pool_md));
  2502. return r;
  2503. }
  2504. if (data_size < sb_data_size) {
  2505. DMERR("%s: pool target (%llu blocks) too small: expected %llu",
  2506. dm_device_name(pool->pool_md),
  2507. (unsigned long long)data_size, sb_data_size);
  2508. return -EINVAL;
  2509. } else if (data_size > sb_data_size) {
  2510. if (dm_pool_metadata_needs_check(pool->pmd)) {
  2511. DMERR("%s: unable to grow the data device until repaired.",
  2512. dm_device_name(pool->pool_md));
  2513. return 0;
  2514. }
  2515. if (sb_data_size)
  2516. DMINFO("%s: growing the data device from %llu to %llu blocks",
  2517. dm_device_name(pool->pool_md),
  2518. sb_data_size, (unsigned long long)data_size);
  2519. r = dm_pool_resize_data_dev(pool->pmd, data_size);
  2520. if (r) {
  2521. metadata_operation_failed(pool, "dm_pool_resize_data_dev", r);
  2522. return r;
  2523. }
  2524. *need_commit = true;
  2525. }
  2526. return 0;
  2527. }
  2528. static int maybe_resize_metadata_dev(struct dm_target *ti, bool *need_commit)
  2529. {
  2530. int r;
  2531. struct pool_c *pt = ti->private;
  2532. struct pool *pool = pt->pool;
  2533. dm_block_t metadata_dev_size, sb_metadata_dev_size;
  2534. *need_commit = false;
  2535. metadata_dev_size = get_metadata_dev_size_in_blocks(pool->md_dev);
  2536. r = dm_pool_get_metadata_dev_size(pool->pmd, &sb_metadata_dev_size);
  2537. if (r) {
  2538. DMERR("%s: failed to retrieve metadata device size",
  2539. dm_device_name(pool->pool_md));
  2540. return r;
  2541. }
  2542. if (metadata_dev_size < sb_metadata_dev_size) {
  2543. DMERR("%s: metadata device (%llu blocks) too small: expected %llu",
  2544. dm_device_name(pool->pool_md),
  2545. metadata_dev_size, sb_metadata_dev_size);
  2546. return -EINVAL;
  2547. } else if (metadata_dev_size > sb_metadata_dev_size) {
  2548. if (dm_pool_metadata_needs_check(pool->pmd)) {
  2549. DMERR("%s: unable to grow the metadata device until repaired.",
  2550. dm_device_name(pool->pool_md));
  2551. return 0;
  2552. }
  2553. warn_if_metadata_device_too_big(pool->md_dev);
  2554. DMINFO("%s: growing the metadata device from %llu to %llu blocks",
  2555. dm_device_name(pool->pool_md),
  2556. sb_metadata_dev_size, metadata_dev_size);
  2557. r = dm_pool_resize_metadata_dev(pool->pmd, metadata_dev_size);
  2558. if (r) {
  2559. metadata_operation_failed(pool, "dm_pool_resize_metadata_dev", r);
  2560. return r;
  2561. }
  2562. *need_commit = true;
  2563. }
  2564. return 0;
  2565. }
  2566. /*
  2567. * Retrieves the number of blocks of the data device from
  2568. * the superblock and compares it to the actual device size,
  2569. * thus resizing the data device in case it has grown.
  2570. *
  2571. * This both copes with opening preallocated data devices in the ctr
  2572. * being followed by a resume
  2573. * -and-
  2574. * calling the resume method individually after userspace has
  2575. * grown the data device in reaction to a table event.
  2576. */
  2577. static int pool_preresume(struct dm_target *ti)
  2578. {
  2579. int r;
  2580. bool need_commit1, need_commit2;
  2581. struct pool_c *pt = ti->private;
  2582. struct pool *pool = pt->pool;
  2583. /*
  2584. * Take control of the pool object.
  2585. */
  2586. r = bind_control_target(pool, ti);
  2587. if (r)
  2588. return r;
  2589. r = maybe_resize_data_dev(ti, &need_commit1);
  2590. if (r)
  2591. return r;
  2592. r = maybe_resize_metadata_dev(ti, &need_commit2);
  2593. if (r)
  2594. return r;
  2595. if (need_commit1 || need_commit2)
  2596. (void) commit(pool);
  2597. return 0;
  2598. }
  2599. static void pool_suspend_active_thins(struct pool *pool)
  2600. {
  2601. struct thin_c *tc;
  2602. /* Suspend all active thin devices */
  2603. tc = get_first_thin(pool);
  2604. while (tc) {
  2605. dm_internal_suspend_noflush(tc->thin_md);
  2606. tc = get_next_thin(pool, tc);
  2607. }
  2608. }
  2609. static void pool_resume_active_thins(struct pool *pool)
  2610. {
  2611. struct thin_c *tc;
  2612. /* Resume all active thin devices */
  2613. tc = get_first_thin(pool);
  2614. while (tc) {
  2615. dm_internal_resume(tc->thin_md);
  2616. tc = get_next_thin(pool, tc);
  2617. }
  2618. }
  2619. static void pool_resume(struct dm_target *ti)
  2620. {
  2621. struct pool_c *pt = ti->private;
  2622. struct pool *pool = pt->pool;
  2623. unsigned long flags;
  2624. /*
  2625. * Must requeue active_thins' bios and then resume
  2626. * active_thins _before_ clearing 'suspend' flag.
  2627. */
  2628. requeue_bios(pool);
  2629. pool_resume_active_thins(pool);
  2630. spin_lock_irqsave(&pool->lock, flags);
  2631. pool->low_water_triggered = false;
  2632. pool->suspended = false;
  2633. spin_unlock_irqrestore(&pool->lock, flags);
  2634. do_waker(&pool->waker.work);
  2635. }
  2636. static void pool_presuspend(struct dm_target *ti)
  2637. {
  2638. struct pool_c *pt = ti->private;
  2639. struct pool *pool = pt->pool;
  2640. unsigned long flags;
  2641. spin_lock_irqsave(&pool->lock, flags);
  2642. pool->suspended = true;
  2643. spin_unlock_irqrestore(&pool->lock, flags);
  2644. pool_suspend_active_thins(pool);
  2645. }
  2646. static void pool_presuspend_undo(struct dm_target *ti)
  2647. {
  2648. struct pool_c *pt = ti->private;
  2649. struct pool *pool = pt->pool;
  2650. unsigned long flags;
  2651. pool_resume_active_thins(pool);
  2652. spin_lock_irqsave(&pool->lock, flags);
  2653. pool->suspended = false;
  2654. spin_unlock_irqrestore(&pool->lock, flags);
  2655. }
  2656. static void pool_postsuspend(struct dm_target *ti)
  2657. {
  2658. struct pool_c *pt = ti->private;
  2659. struct pool *pool = pt->pool;
  2660. cancel_delayed_work(&pool->waker);
  2661. cancel_delayed_work(&pool->no_space_timeout);
  2662. flush_workqueue(pool->wq);
  2663. (void) commit(pool);
  2664. }
  2665. static int check_arg_count(unsigned argc, unsigned args_required)
  2666. {
  2667. if (argc != args_required) {
  2668. DMWARN("Message received with %u arguments instead of %u.",
  2669. argc, args_required);
  2670. return -EINVAL;
  2671. }
  2672. return 0;
  2673. }
  2674. static int read_dev_id(char *arg, dm_thin_id *dev_id, int warning)
  2675. {
  2676. if (!kstrtoull(arg, 10, (unsigned long long *)dev_id) &&
  2677. *dev_id <= MAX_DEV_ID)
  2678. return 0;
  2679. if (warning)
  2680. DMWARN("Message received with invalid device id: %s", arg);
  2681. return -EINVAL;
  2682. }
  2683. static int process_create_thin_mesg(unsigned argc, char **argv, struct pool *pool)
  2684. {
  2685. dm_thin_id dev_id;
  2686. int r;
  2687. r = check_arg_count(argc, 2);
  2688. if (r)
  2689. return r;
  2690. r = read_dev_id(argv[1], &dev_id, 1);
  2691. if (r)
  2692. return r;
  2693. r = dm_pool_create_thin(pool->pmd, dev_id);
  2694. if (r) {
  2695. DMWARN("Creation of new thinly-provisioned device with id %s failed.",
  2696. argv[1]);
  2697. return r;
  2698. }
  2699. return 0;
  2700. }
  2701. static int process_create_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  2702. {
  2703. dm_thin_id dev_id;
  2704. dm_thin_id origin_dev_id;
  2705. int r;
  2706. r = check_arg_count(argc, 3);
  2707. if (r)
  2708. return r;
  2709. r = read_dev_id(argv[1], &dev_id, 1);
  2710. if (r)
  2711. return r;
  2712. r = read_dev_id(argv[2], &origin_dev_id, 1);
  2713. if (r)
  2714. return r;
  2715. r = dm_pool_create_snap(pool->pmd, dev_id, origin_dev_id);
  2716. if (r) {
  2717. DMWARN("Creation of new snapshot %s of device %s failed.",
  2718. argv[1], argv[2]);
  2719. return r;
  2720. }
  2721. return 0;
  2722. }
  2723. static int process_delete_mesg(unsigned argc, char **argv, struct pool *pool)
  2724. {
  2725. dm_thin_id dev_id;
  2726. int r;
  2727. r = check_arg_count(argc, 2);
  2728. if (r)
  2729. return r;
  2730. r = read_dev_id(argv[1], &dev_id, 1);
  2731. if (r)
  2732. return r;
  2733. r = dm_pool_delete_thin_device(pool->pmd, dev_id);
  2734. if (r)
  2735. DMWARN("Deletion of thin device %s failed.", argv[1]);
  2736. return r;
  2737. }
  2738. static int process_set_transaction_id_mesg(unsigned argc, char **argv, struct pool *pool)
  2739. {
  2740. dm_thin_id old_id, new_id;
  2741. int r;
  2742. r = check_arg_count(argc, 3);
  2743. if (r)
  2744. return r;
  2745. if (kstrtoull(argv[1], 10, (unsigned long long *)&old_id)) {
  2746. DMWARN("set_transaction_id message: Unrecognised id %s.", argv[1]);
  2747. return -EINVAL;
  2748. }
  2749. if (kstrtoull(argv[2], 10, (unsigned long long *)&new_id)) {
  2750. DMWARN("set_transaction_id message: Unrecognised new id %s.", argv[2]);
  2751. return -EINVAL;
  2752. }
  2753. r = dm_pool_set_metadata_transaction_id(pool->pmd, old_id, new_id);
  2754. if (r) {
  2755. DMWARN("Failed to change transaction id from %s to %s.",
  2756. argv[1], argv[2]);
  2757. return r;
  2758. }
  2759. return 0;
  2760. }
  2761. static int process_reserve_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  2762. {
  2763. int r;
  2764. r = check_arg_count(argc, 1);
  2765. if (r)
  2766. return r;
  2767. (void) commit(pool);
  2768. r = dm_pool_reserve_metadata_snap(pool->pmd);
  2769. if (r)
  2770. DMWARN("reserve_metadata_snap message failed.");
  2771. return r;
  2772. }
  2773. static int process_release_metadata_snap_mesg(unsigned argc, char **argv, struct pool *pool)
  2774. {
  2775. int r;
  2776. r = check_arg_count(argc, 1);
  2777. if (r)
  2778. return r;
  2779. r = dm_pool_release_metadata_snap(pool->pmd);
  2780. if (r)
  2781. DMWARN("release_metadata_snap message failed.");
  2782. return r;
  2783. }
  2784. /*
  2785. * Messages supported:
  2786. * create_thin <dev_id>
  2787. * create_snap <dev_id> <origin_id>
  2788. * delete <dev_id>
  2789. * set_transaction_id <current_trans_id> <new_trans_id>
  2790. * reserve_metadata_snap
  2791. * release_metadata_snap
  2792. */
  2793. static int pool_message(struct dm_target *ti, unsigned argc, char **argv)
  2794. {
  2795. int r = -EINVAL;
  2796. struct pool_c *pt = ti->private;
  2797. struct pool *pool = pt->pool;
  2798. if (!strcasecmp(argv[0], "create_thin"))
  2799. r = process_create_thin_mesg(argc, argv, pool);
  2800. else if (!strcasecmp(argv[0], "create_snap"))
  2801. r = process_create_snap_mesg(argc, argv, pool);
  2802. else if (!strcasecmp(argv[0], "delete"))
  2803. r = process_delete_mesg(argc, argv, pool);
  2804. else if (!strcasecmp(argv[0], "set_transaction_id"))
  2805. r = process_set_transaction_id_mesg(argc, argv, pool);
  2806. else if (!strcasecmp(argv[0], "reserve_metadata_snap"))
  2807. r = process_reserve_metadata_snap_mesg(argc, argv, pool);
  2808. else if (!strcasecmp(argv[0], "release_metadata_snap"))
  2809. r = process_release_metadata_snap_mesg(argc, argv, pool);
  2810. else
  2811. DMWARN("Unrecognised thin pool target message received: %s", argv[0]);
  2812. if (!r)
  2813. (void) commit(pool);
  2814. return r;
  2815. }
  2816. static void emit_flags(struct pool_features *pf, char *result,
  2817. unsigned sz, unsigned maxlen)
  2818. {
  2819. unsigned count = !pf->zero_new_blocks + !pf->discard_enabled +
  2820. !pf->discard_passdown + (pf->mode == PM_READ_ONLY) +
  2821. pf->error_if_no_space;
  2822. DMEMIT("%u ", count);
  2823. if (!pf->zero_new_blocks)
  2824. DMEMIT("skip_block_zeroing ");
  2825. if (!pf->discard_enabled)
  2826. DMEMIT("ignore_discard ");
  2827. if (!pf->discard_passdown)
  2828. DMEMIT("no_discard_passdown ");
  2829. if (pf->mode == PM_READ_ONLY)
  2830. DMEMIT("read_only ");
  2831. if (pf->error_if_no_space)
  2832. DMEMIT("error_if_no_space ");
  2833. }
  2834. /*
  2835. * Status line is:
  2836. * <transaction id> <used metadata sectors>/<total metadata sectors>
  2837. * <used data sectors>/<total data sectors> <held metadata root>
  2838. */
  2839. static void pool_status(struct dm_target *ti, status_type_t type,
  2840. unsigned status_flags, char *result, unsigned maxlen)
  2841. {
  2842. int r;
  2843. unsigned sz = 0;
  2844. uint64_t transaction_id;
  2845. dm_block_t nr_free_blocks_data;
  2846. dm_block_t nr_free_blocks_metadata;
  2847. dm_block_t nr_blocks_data;
  2848. dm_block_t nr_blocks_metadata;
  2849. dm_block_t held_root;
  2850. char buf[BDEVNAME_SIZE];
  2851. char buf2[BDEVNAME_SIZE];
  2852. struct pool_c *pt = ti->private;
  2853. struct pool *pool = pt->pool;
  2854. switch (type) {
  2855. case STATUSTYPE_INFO:
  2856. if (get_pool_mode(pool) == PM_FAIL) {
  2857. DMEMIT("Fail");
  2858. break;
  2859. }
  2860. /* Commit to ensure statistics aren't out-of-date */
  2861. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
  2862. (void) commit(pool);
  2863. r = dm_pool_get_metadata_transaction_id(pool->pmd, &transaction_id);
  2864. if (r) {
  2865. DMERR("%s: dm_pool_get_metadata_transaction_id returned %d",
  2866. dm_device_name(pool->pool_md), r);
  2867. goto err;
  2868. }
  2869. r = dm_pool_get_free_metadata_block_count(pool->pmd, &nr_free_blocks_metadata);
  2870. if (r) {
  2871. DMERR("%s: dm_pool_get_free_metadata_block_count returned %d",
  2872. dm_device_name(pool->pool_md), r);
  2873. goto err;
  2874. }
  2875. r = dm_pool_get_metadata_dev_size(pool->pmd, &nr_blocks_metadata);
  2876. if (r) {
  2877. DMERR("%s: dm_pool_get_metadata_dev_size returned %d",
  2878. dm_device_name(pool->pool_md), r);
  2879. goto err;
  2880. }
  2881. r = dm_pool_get_free_block_count(pool->pmd, &nr_free_blocks_data);
  2882. if (r) {
  2883. DMERR("%s: dm_pool_get_free_block_count returned %d",
  2884. dm_device_name(pool->pool_md), r);
  2885. goto err;
  2886. }
  2887. r = dm_pool_get_data_dev_size(pool->pmd, &nr_blocks_data);
  2888. if (r) {
  2889. DMERR("%s: dm_pool_get_data_dev_size returned %d",
  2890. dm_device_name(pool->pool_md), r);
  2891. goto err;
  2892. }
  2893. r = dm_pool_get_metadata_snap(pool->pmd, &held_root);
  2894. if (r) {
  2895. DMERR("%s: dm_pool_get_metadata_snap returned %d",
  2896. dm_device_name(pool->pool_md), r);
  2897. goto err;
  2898. }
  2899. DMEMIT("%llu %llu/%llu %llu/%llu ",
  2900. (unsigned long long)transaction_id,
  2901. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  2902. (unsigned long long)nr_blocks_metadata,
  2903. (unsigned long long)(nr_blocks_data - nr_free_blocks_data),
  2904. (unsigned long long)nr_blocks_data);
  2905. if (held_root)
  2906. DMEMIT("%llu ", held_root);
  2907. else
  2908. DMEMIT("- ");
  2909. if (pool->pf.mode == PM_OUT_OF_DATA_SPACE)
  2910. DMEMIT("out_of_data_space ");
  2911. else if (pool->pf.mode == PM_READ_ONLY)
  2912. DMEMIT("ro ");
  2913. else
  2914. DMEMIT("rw ");
  2915. if (!pool->pf.discard_enabled)
  2916. DMEMIT("ignore_discard ");
  2917. else if (pool->pf.discard_passdown)
  2918. DMEMIT("discard_passdown ");
  2919. else
  2920. DMEMIT("no_discard_passdown ");
  2921. if (pool->pf.error_if_no_space)
  2922. DMEMIT("error_if_no_space ");
  2923. else
  2924. DMEMIT("queue_if_no_space ");
  2925. break;
  2926. case STATUSTYPE_TABLE:
  2927. DMEMIT("%s %s %lu %llu ",
  2928. format_dev_t(buf, pt->metadata_dev->bdev->bd_dev),
  2929. format_dev_t(buf2, pt->data_dev->bdev->bd_dev),
  2930. (unsigned long)pool->sectors_per_block,
  2931. (unsigned long long)pt->low_water_blocks);
  2932. emit_flags(&pt->requested_pf, result, sz, maxlen);
  2933. break;
  2934. }
  2935. return;
  2936. err:
  2937. DMEMIT("Error");
  2938. }
  2939. static int pool_iterate_devices(struct dm_target *ti,
  2940. iterate_devices_callout_fn fn, void *data)
  2941. {
  2942. struct pool_c *pt = ti->private;
  2943. return fn(ti, pt->data_dev, 0, ti->len, data);
  2944. }
  2945. static int pool_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  2946. struct bio_vec *biovec, int max_size)
  2947. {
  2948. struct pool_c *pt = ti->private;
  2949. struct request_queue *q = bdev_get_queue(pt->data_dev->bdev);
  2950. if (!q->merge_bvec_fn)
  2951. return max_size;
  2952. bvm->bi_bdev = pt->data_dev->bdev;
  2953. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  2954. }
  2955. static void set_discard_limits(struct pool_c *pt, struct queue_limits *limits)
  2956. {
  2957. struct pool *pool = pt->pool;
  2958. struct queue_limits *data_limits;
  2959. limits->max_discard_sectors = pool->sectors_per_block;
  2960. /*
  2961. * discard_granularity is just a hint, and not enforced.
  2962. */
  2963. if (pt->adjusted_pf.discard_passdown) {
  2964. data_limits = &bdev_get_queue(pt->data_dev->bdev)->limits;
  2965. limits->discard_granularity = max(data_limits->discard_granularity,
  2966. pool->sectors_per_block << SECTOR_SHIFT);
  2967. } else
  2968. limits->discard_granularity = pool->sectors_per_block << SECTOR_SHIFT;
  2969. }
  2970. static void pool_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2971. {
  2972. struct pool_c *pt = ti->private;
  2973. struct pool *pool = pt->pool;
  2974. sector_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  2975. /*
  2976. * If max_sectors is smaller than pool->sectors_per_block adjust it
  2977. * to the highest possible power-of-2 factor of pool->sectors_per_block.
  2978. * This is especially beneficial when the pool's data device is a RAID
  2979. * device that has a full stripe width that matches pool->sectors_per_block
  2980. * -- because even though partial RAID stripe-sized IOs will be issued to a
  2981. * single RAID stripe; when aggregated they will end on a full RAID stripe
  2982. * boundary.. which avoids additional partial RAID stripe writes cascading
  2983. */
  2984. if (limits->max_sectors < pool->sectors_per_block) {
  2985. while (!is_factor(pool->sectors_per_block, limits->max_sectors)) {
  2986. if ((limits->max_sectors & (limits->max_sectors - 1)) == 0)
  2987. limits->max_sectors--;
  2988. limits->max_sectors = rounddown_pow_of_two(limits->max_sectors);
  2989. }
  2990. }
  2991. /*
  2992. * If the system-determined stacked limits are compatible with the
  2993. * pool's blocksize (io_opt is a factor) do not override them.
  2994. */
  2995. if (io_opt_sectors < pool->sectors_per_block ||
  2996. !is_factor(io_opt_sectors, pool->sectors_per_block)) {
  2997. if (is_factor(pool->sectors_per_block, limits->max_sectors))
  2998. blk_limits_io_min(limits, limits->max_sectors << SECTOR_SHIFT);
  2999. else
  3000. blk_limits_io_min(limits, pool->sectors_per_block << SECTOR_SHIFT);
  3001. blk_limits_io_opt(limits, pool->sectors_per_block << SECTOR_SHIFT);
  3002. }
  3003. /*
  3004. * pt->adjusted_pf is a staging area for the actual features to use.
  3005. * They get transferred to the live pool in bind_control_target()
  3006. * called from pool_preresume().
  3007. */
  3008. if (!pt->adjusted_pf.discard_enabled) {
  3009. /*
  3010. * Must explicitly disallow stacking discard limits otherwise the
  3011. * block layer will stack them if pool's data device has support.
  3012. * QUEUE_FLAG_DISCARD wouldn't be set but there is no way for the
  3013. * user to see that, so make sure to set all discard limits to 0.
  3014. */
  3015. limits->discard_granularity = 0;
  3016. return;
  3017. }
  3018. disable_passdown_if_not_supported(pt);
  3019. set_discard_limits(pt, limits);
  3020. }
  3021. static struct target_type pool_target = {
  3022. .name = "thin-pool",
  3023. .features = DM_TARGET_SINGLETON | DM_TARGET_ALWAYS_WRITEABLE |
  3024. DM_TARGET_IMMUTABLE,
  3025. .version = {1, 14, 0},
  3026. .module = THIS_MODULE,
  3027. .ctr = pool_ctr,
  3028. .dtr = pool_dtr,
  3029. .map = pool_map,
  3030. .presuspend = pool_presuspend,
  3031. .presuspend_undo = pool_presuspend_undo,
  3032. .postsuspend = pool_postsuspend,
  3033. .preresume = pool_preresume,
  3034. .resume = pool_resume,
  3035. .message = pool_message,
  3036. .status = pool_status,
  3037. .merge = pool_merge,
  3038. .iterate_devices = pool_iterate_devices,
  3039. .io_hints = pool_io_hints,
  3040. };
  3041. /*----------------------------------------------------------------
  3042. * Thin target methods
  3043. *--------------------------------------------------------------*/
  3044. static void thin_get(struct thin_c *tc)
  3045. {
  3046. atomic_inc(&tc->refcount);
  3047. }
  3048. static void thin_put(struct thin_c *tc)
  3049. {
  3050. if (atomic_dec_and_test(&tc->refcount))
  3051. complete(&tc->can_destroy);
  3052. }
  3053. static void thin_dtr(struct dm_target *ti)
  3054. {
  3055. struct thin_c *tc = ti->private;
  3056. unsigned long flags;
  3057. spin_lock_irqsave(&tc->pool->lock, flags);
  3058. list_del_rcu(&tc->list);
  3059. spin_unlock_irqrestore(&tc->pool->lock, flags);
  3060. synchronize_rcu();
  3061. thin_put(tc);
  3062. wait_for_completion(&tc->can_destroy);
  3063. mutex_lock(&dm_thin_pool_table.mutex);
  3064. __pool_dec(tc->pool);
  3065. dm_pool_close_thin_device(tc->td);
  3066. dm_put_device(ti, tc->pool_dev);
  3067. if (tc->origin_dev)
  3068. dm_put_device(ti, tc->origin_dev);
  3069. kfree(tc);
  3070. mutex_unlock(&dm_thin_pool_table.mutex);
  3071. }
  3072. /*
  3073. * Thin target parameters:
  3074. *
  3075. * <pool_dev> <dev_id> [origin_dev]
  3076. *
  3077. * pool_dev: the path to the pool (eg, /dev/mapper/my_pool)
  3078. * dev_id: the internal device identifier
  3079. * origin_dev: a device external to the pool that should act as the origin
  3080. *
  3081. * If the pool device has discards disabled, they get disabled for the thin
  3082. * device as well.
  3083. */
  3084. static int thin_ctr(struct dm_target *ti, unsigned argc, char **argv)
  3085. {
  3086. int r;
  3087. struct thin_c *tc;
  3088. struct dm_dev *pool_dev, *origin_dev;
  3089. struct mapped_device *pool_md;
  3090. unsigned long flags;
  3091. mutex_lock(&dm_thin_pool_table.mutex);
  3092. if (argc != 2 && argc != 3) {
  3093. ti->error = "Invalid argument count";
  3094. r = -EINVAL;
  3095. goto out_unlock;
  3096. }
  3097. tc = ti->private = kzalloc(sizeof(*tc), GFP_KERNEL);
  3098. if (!tc) {
  3099. ti->error = "Out of memory";
  3100. r = -ENOMEM;
  3101. goto out_unlock;
  3102. }
  3103. tc->thin_md = dm_table_get_md(ti->table);
  3104. spin_lock_init(&tc->lock);
  3105. INIT_LIST_HEAD(&tc->deferred_cells);
  3106. bio_list_init(&tc->deferred_bio_list);
  3107. bio_list_init(&tc->retry_on_resume_list);
  3108. tc->sort_bio_list = RB_ROOT;
  3109. if (argc == 3) {
  3110. r = dm_get_device(ti, argv[2], FMODE_READ, &origin_dev);
  3111. if (r) {
  3112. ti->error = "Error opening origin device";
  3113. goto bad_origin_dev;
  3114. }
  3115. tc->origin_dev = origin_dev;
  3116. }
  3117. r = dm_get_device(ti, argv[0], dm_table_get_mode(ti->table), &pool_dev);
  3118. if (r) {
  3119. ti->error = "Error opening pool device";
  3120. goto bad_pool_dev;
  3121. }
  3122. tc->pool_dev = pool_dev;
  3123. if (read_dev_id(argv[1], (unsigned long long *)&tc->dev_id, 0)) {
  3124. ti->error = "Invalid device id";
  3125. r = -EINVAL;
  3126. goto bad_common;
  3127. }
  3128. pool_md = dm_get_md(tc->pool_dev->bdev->bd_dev);
  3129. if (!pool_md) {
  3130. ti->error = "Couldn't get pool mapped device";
  3131. r = -EINVAL;
  3132. goto bad_common;
  3133. }
  3134. tc->pool = __pool_table_lookup(pool_md);
  3135. if (!tc->pool) {
  3136. ti->error = "Couldn't find pool object";
  3137. r = -EINVAL;
  3138. goto bad_pool_lookup;
  3139. }
  3140. __pool_inc(tc->pool);
  3141. if (get_pool_mode(tc->pool) == PM_FAIL) {
  3142. ti->error = "Couldn't open thin device, Pool is in fail mode";
  3143. r = -EINVAL;
  3144. goto bad_pool;
  3145. }
  3146. r = dm_pool_open_thin_device(tc->pool->pmd, tc->dev_id, &tc->td);
  3147. if (r) {
  3148. ti->error = "Couldn't open thin internal device";
  3149. goto bad_pool;
  3150. }
  3151. r = dm_set_target_max_io_len(ti, tc->pool->sectors_per_block);
  3152. if (r)
  3153. goto bad;
  3154. ti->num_flush_bios = 1;
  3155. ti->flush_supported = true;
  3156. ti->per_bio_data_size = sizeof(struct dm_thin_endio_hook);
  3157. /* In case the pool supports discards, pass them on. */
  3158. ti->discard_zeroes_data_unsupported = true;
  3159. if (tc->pool->pf.discard_enabled) {
  3160. ti->discards_supported = true;
  3161. ti->num_discard_bios = 1;
  3162. /* Discard bios must be split on a block boundary */
  3163. ti->split_discard_bios = true;
  3164. }
  3165. mutex_unlock(&dm_thin_pool_table.mutex);
  3166. spin_lock_irqsave(&tc->pool->lock, flags);
  3167. if (tc->pool->suspended) {
  3168. spin_unlock_irqrestore(&tc->pool->lock, flags);
  3169. mutex_lock(&dm_thin_pool_table.mutex); /* reacquire for __pool_dec */
  3170. ti->error = "Unable to activate thin device while pool is suspended";
  3171. r = -EINVAL;
  3172. goto bad;
  3173. }
  3174. atomic_set(&tc->refcount, 1);
  3175. init_completion(&tc->can_destroy);
  3176. list_add_tail_rcu(&tc->list, &tc->pool->active_thins);
  3177. spin_unlock_irqrestore(&tc->pool->lock, flags);
  3178. /*
  3179. * This synchronize_rcu() call is needed here otherwise we risk a
  3180. * wake_worker() call finding no bios to process (because the newly
  3181. * added tc isn't yet visible). So this reduces latency since we
  3182. * aren't then dependent on the periodic commit to wake_worker().
  3183. */
  3184. synchronize_rcu();
  3185. dm_put(pool_md);
  3186. return 0;
  3187. bad:
  3188. dm_pool_close_thin_device(tc->td);
  3189. bad_pool:
  3190. __pool_dec(tc->pool);
  3191. bad_pool_lookup:
  3192. dm_put(pool_md);
  3193. bad_common:
  3194. dm_put_device(ti, tc->pool_dev);
  3195. bad_pool_dev:
  3196. if (tc->origin_dev)
  3197. dm_put_device(ti, tc->origin_dev);
  3198. bad_origin_dev:
  3199. kfree(tc);
  3200. out_unlock:
  3201. mutex_unlock(&dm_thin_pool_table.mutex);
  3202. return r;
  3203. }
  3204. static int thin_map(struct dm_target *ti, struct bio *bio)
  3205. {
  3206. bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
  3207. return thin_bio_map(ti, bio);
  3208. }
  3209. static int thin_endio(struct dm_target *ti, struct bio *bio, int err)
  3210. {
  3211. unsigned long flags;
  3212. struct dm_thin_endio_hook *h = dm_per_bio_data(bio, sizeof(struct dm_thin_endio_hook));
  3213. struct list_head work;
  3214. struct dm_thin_new_mapping *m, *tmp;
  3215. struct pool *pool = h->tc->pool;
  3216. if (h->shared_read_entry) {
  3217. INIT_LIST_HEAD(&work);
  3218. dm_deferred_entry_dec(h->shared_read_entry, &work);
  3219. spin_lock_irqsave(&pool->lock, flags);
  3220. list_for_each_entry_safe(m, tmp, &work, list) {
  3221. list_del(&m->list);
  3222. __complete_mapping_preparation(m);
  3223. }
  3224. spin_unlock_irqrestore(&pool->lock, flags);
  3225. }
  3226. if (h->all_io_entry) {
  3227. INIT_LIST_HEAD(&work);
  3228. dm_deferred_entry_dec(h->all_io_entry, &work);
  3229. if (!list_empty(&work)) {
  3230. spin_lock_irqsave(&pool->lock, flags);
  3231. list_for_each_entry_safe(m, tmp, &work, list)
  3232. list_add_tail(&m->list, &pool->prepared_discards);
  3233. spin_unlock_irqrestore(&pool->lock, flags);
  3234. wake_worker(pool);
  3235. }
  3236. }
  3237. return 0;
  3238. }
  3239. static void thin_presuspend(struct dm_target *ti)
  3240. {
  3241. struct thin_c *tc = ti->private;
  3242. if (dm_noflush_suspending(ti))
  3243. noflush_work(tc, do_noflush_start);
  3244. }
  3245. static void thin_postsuspend(struct dm_target *ti)
  3246. {
  3247. struct thin_c *tc = ti->private;
  3248. /*
  3249. * The dm_noflush_suspending flag has been cleared by now, so
  3250. * unfortunately we must always run this.
  3251. */
  3252. noflush_work(tc, do_noflush_stop);
  3253. }
  3254. static int thin_preresume(struct dm_target *ti)
  3255. {
  3256. struct thin_c *tc = ti->private;
  3257. if (tc->origin_dev)
  3258. tc->origin_size = get_dev_size(tc->origin_dev->bdev);
  3259. return 0;
  3260. }
  3261. /*
  3262. * <nr mapped sectors> <highest mapped sector>
  3263. */
  3264. static void thin_status(struct dm_target *ti, status_type_t type,
  3265. unsigned status_flags, char *result, unsigned maxlen)
  3266. {
  3267. int r;
  3268. ssize_t sz = 0;
  3269. dm_block_t mapped, highest;
  3270. char buf[BDEVNAME_SIZE];
  3271. struct thin_c *tc = ti->private;
  3272. if (get_pool_mode(tc->pool) == PM_FAIL) {
  3273. DMEMIT("Fail");
  3274. return;
  3275. }
  3276. if (!tc->td)
  3277. DMEMIT("-");
  3278. else {
  3279. switch (type) {
  3280. case STATUSTYPE_INFO:
  3281. r = dm_thin_get_mapped_count(tc->td, &mapped);
  3282. if (r) {
  3283. DMERR("dm_thin_get_mapped_count returned %d", r);
  3284. goto err;
  3285. }
  3286. r = dm_thin_get_highest_mapped_block(tc->td, &highest);
  3287. if (r < 0) {
  3288. DMERR("dm_thin_get_highest_mapped_block returned %d", r);
  3289. goto err;
  3290. }
  3291. DMEMIT("%llu ", mapped * tc->pool->sectors_per_block);
  3292. if (r)
  3293. DMEMIT("%llu", ((highest + 1) *
  3294. tc->pool->sectors_per_block) - 1);
  3295. else
  3296. DMEMIT("-");
  3297. break;
  3298. case STATUSTYPE_TABLE:
  3299. DMEMIT("%s %lu",
  3300. format_dev_t(buf, tc->pool_dev->bdev->bd_dev),
  3301. (unsigned long) tc->dev_id);
  3302. if (tc->origin_dev)
  3303. DMEMIT(" %s", format_dev_t(buf, tc->origin_dev->bdev->bd_dev));
  3304. break;
  3305. }
  3306. }
  3307. return;
  3308. err:
  3309. DMEMIT("Error");
  3310. }
  3311. static int thin_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
  3312. struct bio_vec *biovec, int max_size)
  3313. {
  3314. struct thin_c *tc = ti->private;
  3315. struct request_queue *q = bdev_get_queue(tc->pool_dev->bdev);
  3316. if (!q->merge_bvec_fn)
  3317. return max_size;
  3318. bvm->bi_bdev = tc->pool_dev->bdev;
  3319. bvm->bi_sector = dm_target_offset(ti, bvm->bi_sector);
  3320. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  3321. }
  3322. static int thin_iterate_devices(struct dm_target *ti,
  3323. iterate_devices_callout_fn fn, void *data)
  3324. {
  3325. sector_t blocks;
  3326. struct thin_c *tc = ti->private;
  3327. struct pool *pool = tc->pool;
  3328. /*
  3329. * We can't call dm_pool_get_data_dev_size() since that blocks. So
  3330. * we follow a more convoluted path through to the pool's target.
  3331. */
  3332. if (!pool->ti)
  3333. return 0; /* nothing is bound */
  3334. blocks = pool->ti->len;
  3335. (void) sector_div(blocks, pool->sectors_per_block);
  3336. if (blocks)
  3337. return fn(ti, tc->pool_dev, 0, pool->sectors_per_block * blocks, data);
  3338. return 0;
  3339. }
  3340. static struct target_type thin_target = {
  3341. .name = "thin",
  3342. .version = {1, 14, 0},
  3343. .module = THIS_MODULE,
  3344. .ctr = thin_ctr,
  3345. .dtr = thin_dtr,
  3346. .map = thin_map,
  3347. .end_io = thin_endio,
  3348. .preresume = thin_preresume,
  3349. .presuspend = thin_presuspend,
  3350. .postsuspend = thin_postsuspend,
  3351. .status = thin_status,
  3352. .merge = thin_merge,
  3353. .iterate_devices = thin_iterate_devices,
  3354. };
  3355. /*----------------------------------------------------------------*/
  3356. static int __init dm_thin_init(void)
  3357. {
  3358. int r;
  3359. pool_table_init();
  3360. r = dm_register_target(&thin_target);
  3361. if (r)
  3362. return r;
  3363. r = dm_register_target(&pool_target);
  3364. if (r)
  3365. goto bad_pool_target;
  3366. r = -ENOMEM;
  3367. _new_mapping_cache = KMEM_CACHE(dm_thin_new_mapping, 0);
  3368. if (!_new_mapping_cache)
  3369. goto bad_new_mapping_cache;
  3370. return 0;
  3371. bad_new_mapping_cache:
  3372. dm_unregister_target(&pool_target);
  3373. bad_pool_target:
  3374. dm_unregister_target(&thin_target);
  3375. return r;
  3376. }
  3377. static void dm_thin_exit(void)
  3378. {
  3379. dm_unregister_target(&thin_target);
  3380. dm_unregister_target(&pool_target);
  3381. kmem_cache_destroy(_new_mapping_cache);
  3382. }
  3383. module_init(dm_thin_init);
  3384. module_exit(dm_thin_exit);
  3385. module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
  3386. MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
  3387. MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
  3388. MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
  3389. MODULE_LICENSE("GPL");