dm-thin.c 110 KB

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