block-map.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #include "block-map.h"
  6. #include <linux/bio.h>
  7. #include <linux/ratelimit.h>
  8. #include "errors.h"
  9. #include "logger.h"
  10. #include "memory-alloc.h"
  11. #include "permassert.h"
  12. #include "action-manager.h"
  13. #include "admin-state.h"
  14. #include "completion.h"
  15. #include "constants.h"
  16. #include "data-vio.h"
  17. #include "encodings.h"
  18. #include "io-submitter.h"
  19. #include "physical-zone.h"
  20. #include "recovery-journal.h"
  21. #include "slab-depot.h"
  22. #include "status-codes.h"
  23. #include "types.h"
  24. #include "vdo.h"
  25. #include "vio.h"
  26. #include "wait-queue.h"
  27. /**
  28. * DOC: Block map eras
  29. *
  30. * The block map era, or maximum age, is used as follows:
  31. *
  32. * Each block map page, when dirty, records the earliest recovery journal block sequence number of
  33. * the changes reflected in that dirty block. Sequence numbers are classified into eras: every
  34. * @maximum_age sequence numbers, we switch to a new era. Block map pages are assigned to eras
  35. * according to the sequence number they record.
  36. *
  37. * In the current (newest) era, block map pages are not written unless there is cache pressure. In
  38. * the next oldest era, each time a new journal block is written 1/@maximum_age of the pages in
  39. * this era are issued for write. In all older eras, pages are issued for write immediately.
  40. */
  41. struct page_descriptor {
  42. root_count_t root_index;
  43. height_t height;
  44. page_number_t page_index;
  45. slot_number_t slot;
  46. } __packed;
  47. union page_key {
  48. struct page_descriptor descriptor;
  49. u64 key;
  50. };
  51. struct write_if_not_dirtied_context {
  52. struct block_map_zone *zone;
  53. u8 generation;
  54. };
  55. struct block_map_tree_segment {
  56. struct tree_page *levels[VDO_BLOCK_MAP_TREE_HEIGHT];
  57. };
  58. struct block_map_tree {
  59. struct block_map_tree_segment *segments;
  60. };
  61. struct forest {
  62. struct block_map *map;
  63. size_t segments;
  64. struct boundary *boundaries;
  65. struct tree_page **pages;
  66. struct block_map_tree trees[];
  67. };
  68. struct cursor_level {
  69. page_number_t page_index;
  70. slot_number_t slot;
  71. };
  72. struct cursors;
  73. struct cursor {
  74. struct vdo_waiter waiter;
  75. struct block_map_tree *tree;
  76. height_t height;
  77. struct cursors *parent;
  78. struct boundary boundary;
  79. struct cursor_level levels[VDO_BLOCK_MAP_TREE_HEIGHT];
  80. struct pooled_vio *vio;
  81. };
  82. struct cursors {
  83. struct block_map_zone *zone;
  84. struct vio_pool *pool;
  85. vdo_entry_callback_fn entry_callback;
  86. struct vdo_completion *completion;
  87. root_count_t active_roots;
  88. struct cursor cursors[];
  89. };
  90. static const physical_block_number_t NO_PAGE = 0xFFFFFFFFFFFFFFFF;
  91. /* Used to indicate that the page holding the location of a tree root has been "loaded". */
  92. static const physical_block_number_t VDO_INVALID_PBN = 0xFFFFFFFFFFFFFFFF;
  93. const struct block_map_entry UNMAPPED_BLOCK_MAP_ENTRY = {
  94. .mapping_state = VDO_MAPPING_STATE_UNMAPPED & 0x0F,
  95. .pbn_high_nibble = 0,
  96. .pbn_low_word = __cpu_to_le32(VDO_ZERO_BLOCK & UINT_MAX),
  97. };
  98. #define LOG_INTERVAL 4000
  99. #define DISPLAY_INTERVAL 100000
  100. /*
  101. * For adjusting VDO page cache statistic fields which are only mutated on the logical zone thread.
  102. * Prevents any compiler shenanigans from affecting other threads reading those stats.
  103. */
  104. #define ADD_ONCE(value, delta) WRITE_ONCE(value, (value) + (delta))
  105. static inline bool is_dirty(const struct page_info *info)
  106. {
  107. return info->state == PS_DIRTY;
  108. }
  109. static inline bool is_present(const struct page_info *info)
  110. {
  111. return (info->state == PS_RESIDENT) || (info->state == PS_DIRTY);
  112. }
  113. static inline bool is_in_flight(const struct page_info *info)
  114. {
  115. return (info->state == PS_INCOMING) || (info->state == PS_OUTGOING);
  116. }
  117. static inline bool is_incoming(const struct page_info *info)
  118. {
  119. return info->state == PS_INCOMING;
  120. }
  121. static inline bool is_outgoing(const struct page_info *info)
  122. {
  123. return info->state == PS_OUTGOING;
  124. }
  125. static inline bool is_valid(const struct page_info *info)
  126. {
  127. return is_present(info) || is_outgoing(info);
  128. }
  129. static char *get_page_buffer(struct page_info *info)
  130. {
  131. struct vdo_page_cache *cache = info->cache;
  132. return &cache->pages[(info - cache->infos) * VDO_BLOCK_SIZE];
  133. }
  134. static inline struct vdo_page_completion *page_completion_from_waiter(struct vdo_waiter *waiter)
  135. {
  136. struct vdo_page_completion *completion;
  137. if (waiter == NULL)
  138. return NULL;
  139. completion = container_of(waiter, struct vdo_page_completion, waiter);
  140. vdo_assert_completion_type(&completion->completion, VDO_PAGE_COMPLETION);
  141. return completion;
  142. }
  143. /**
  144. * initialize_info() - Initialize all page info structures and put them on the free list.
  145. *
  146. * Return: VDO_SUCCESS or an error.
  147. */
  148. static int initialize_info(struct vdo_page_cache *cache)
  149. {
  150. struct page_info *info;
  151. INIT_LIST_HEAD(&cache->free_list);
  152. for (info = cache->infos; info < cache->infos + cache->page_count; info++) {
  153. int result;
  154. info->cache = cache;
  155. info->state = PS_FREE;
  156. info->pbn = NO_PAGE;
  157. result = create_metadata_vio(cache->vdo, VIO_TYPE_BLOCK_MAP,
  158. VIO_PRIORITY_METADATA, info,
  159. get_page_buffer(info), &info->vio);
  160. if (result != VDO_SUCCESS)
  161. return result;
  162. /* The thread ID should never change. */
  163. info->vio->completion.callback_thread_id = cache->zone->thread_id;
  164. INIT_LIST_HEAD(&info->state_entry);
  165. list_add_tail(&info->state_entry, &cache->free_list);
  166. INIT_LIST_HEAD(&info->lru_entry);
  167. }
  168. return VDO_SUCCESS;
  169. }
  170. /**
  171. * allocate_cache_components() - Allocate components of the cache which require their own
  172. * allocation.
  173. * @maximum_age: The number of journal blocks before a dirtied page is considered old and must be
  174. * written out.
  175. *
  176. * The caller is responsible for all clean up on errors.
  177. *
  178. * Return: VDO_SUCCESS or an error code.
  179. */
  180. static int __must_check allocate_cache_components(struct vdo_page_cache *cache)
  181. {
  182. u64 size = cache->page_count * (u64) VDO_BLOCK_SIZE;
  183. int result;
  184. result = vdo_allocate(cache->page_count, struct page_info, "page infos",
  185. &cache->infos);
  186. if (result != VDO_SUCCESS)
  187. return result;
  188. result = vdo_allocate_memory(size, VDO_BLOCK_SIZE, "cache pages", &cache->pages);
  189. if (result != VDO_SUCCESS)
  190. return result;
  191. result = vdo_int_map_create(cache->page_count, &cache->page_map);
  192. if (result != VDO_SUCCESS)
  193. return result;
  194. return initialize_info(cache);
  195. }
  196. /**
  197. * assert_on_cache_thread() - Assert that a function has been called on the VDO page cache's
  198. * thread.
  199. */
  200. static inline void assert_on_cache_thread(struct vdo_page_cache *cache,
  201. const char *function_name)
  202. {
  203. thread_id_t thread_id = vdo_get_callback_thread_id();
  204. VDO_ASSERT_LOG_ONLY((thread_id == cache->zone->thread_id),
  205. "%s() must only be called on cache thread %d, not thread %d",
  206. function_name, cache->zone->thread_id, thread_id);
  207. }
  208. /** assert_io_allowed() - Assert that a page cache may issue I/O. */
  209. static inline void assert_io_allowed(struct vdo_page_cache *cache)
  210. {
  211. VDO_ASSERT_LOG_ONLY(!vdo_is_state_quiescent(&cache->zone->state),
  212. "VDO page cache may issue I/O");
  213. }
  214. /** report_cache_pressure() - Log and, if enabled, report cache pressure. */
  215. static void report_cache_pressure(struct vdo_page_cache *cache)
  216. {
  217. ADD_ONCE(cache->stats.cache_pressure, 1);
  218. if (cache->waiter_count > cache->page_count) {
  219. if ((cache->pressure_report % LOG_INTERVAL) == 0)
  220. vdo_log_info("page cache pressure %u", cache->stats.cache_pressure);
  221. if (++cache->pressure_report >= DISPLAY_INTERVAL)
  222. cache->pressure_report = 0;
  223. }
  224. }
  225. /**
  226. * get_page_state_name() - Return the name of a page state.
  227. *
  228. * If the page state is invalid a static string is returned and the invalid state is logged.
  229. *
  230. * Return: A pointer to a static page state name.
  231. */
  232. static const char * __must_check get_page_state_name(enum vdo_page_buffer_state state)
  233. {
  234. int result;
  235. static const char * const state_names[] = {
  236. "FREE", "INCOMING", "FAILED", "RESIDENT", "DIRTY", "OUTGOING"
  237. };
  238. BUILD_BUG_ON(ARRAY_SIZE(state_names) != PAGE_STATE_COUNT);
  239. result = VDO_ASSERT(state < ARRAY_SIZE(state_names),
  240. "Unknown page_state value %d", state);
  241. if (result != VDO_SUCCESS)
  242. return "[UNKNOWN PAGE STATE]";
  243. return state_names[state];
  244. }
  245. /**
  246. * update_counter() - Update the counter associated with a given state.
  247. * @info: The page info to count.
  248. * @delta: The delta to apply to the counter.
  249. */
  250. static void update_counter(struct page_info *info, s32 delta)
  251. {
  252. struct block_map_statistics *stats = &info->cache->stats;
  253. switch (info->state) {
  254. case PS_FREE:
  255. ADD_ONCE(stats->free_pages, delta);
  256. return;
  257. case PS_INCOMING:
  258. ADD_ONCE(stats->incoming_pages, delta);
  259. return;
  260. case PS_OUTGOING:
  261. ADD_ONCE(stats->outgoing_pages, delta);
  262. return;
  263. case PS_FAILED:
  264. ADD_ONCE(stats->failed_pages, delta);
  265. return;
  266. case PS_RESIDENT:
  267. ADD_ONCE(stats->clean_pages, delta);
  268. return;
  269. case PS_DIRTY:
  270. ADD_ONCE(stats->dirty_pages, delta);
  271. return;
  272. default:
  273. return;
  274. }
  275. }
  276. /** update_lru() - Update the lru information for an active page. */
  277. static void update_lru(struct page_info *info)
  278. {
  279. if (info->cache->lru_list.prev != &info->lru_entry)
  280. list_move_tail(&info->lru_entry, &info->cache->lru_list);
  281. }
  282. /**
  283. * set_info_state() - Set the state of a page_info and put it on the right list, adjusting
  284. * counters.
  285. */
  286. static void set_info_state(struct page_info *info, enum vdo_page_buffer_state new_state)
  287. {
  288. if (new_state == info->state)
  289. return;
  290. update_counter(info, -1);
  291. info->state = new_state;
  292. update_counter(info, 1);
  293. switch (info->state) {
  294. case PS_FREE:
  295. case PS_FAILED:
  296. list_move_tail(&info->state_entry, &info->cache->free_list);
  297. return;
  298. case PS_OUTGOING:
  299. list_move_tail(&info->state_entry, &info->cache->outgoing_list);
  300. return;
  301. case PS_DIRTY:
  302. return;
  303. default:
  304. list_del_init(&info->state_entry);
  305. }
  306. }
  307. /** set_info_pbn() - Set the pbn for an info, updating the map as needed. */
  308. static int __must_check set_info_pbn(struct page_info *info, physical_block_number_t pbn)
  309. {
  310. struct vdo_page_cache *cache = info->cache;
  311. /* Either the new or the old page number must be NO_PAGE. */
  312. int result = VDO_ASSERT((pbn == NO_PAGE) || (info->pbn == NO_PAGE),
  313. "Must free a page before reusing it.");
  314. if (result != VDO_SUCCESS)
  315. return result;
  316. if (info->pbn != NO_PAGE)
  317. vdo_int_map_remove(cache->page_map, info->pbn);
  318. info->pbn = pbn;
  319. if (pbn != NO_PAGE) {
  320. result = vdo_int_map_put(cache->page_map, pbn, info, true, NULL);
  321. if (result != VDO_SUCCESS)
  322. return result;
  323. }
  324. return VDO_SUCCESS;
  325. }
  326. /** reset_page_info() - Reset page info to represent an unallocated page. */
  327. static int reset_page_info(struct page_info *info)
  328. {
  329. int result;
  330. result = VDO_ASSERT(info->busy == 0, "VDO Page must not be busy");
  331. if (result != VDO_SUCCESS)
  332. return result;
  333. result = VDO_ASSERT(!vdo_waitq_has_waiters(&info->waiting),
  334. "VDO Page must not have waiters");
  335. if (result != VDO_SUCCESS)
  336. return result;
  337. result = set_info_pbn(info, NO_PAGE);
  338. set_info_state(info, PS_FREE);
  339. list_del_init(&info->lru_entry);
  340. return result;
  341. }
  342. /**
  343. * find_free_page() - Find a free page.
  344. *
  345. * Return: A pointer to the page info structure (if found), NULL otherwise.
  346. */
  347. static struct page_info * __must_check find_free_page(struct vdo_page_cache *cache)
  348. {
  349. struct page_info *info;
  350. info = list_first_entry_or_null(&cache->free_list, struct page_info,
  351. state_entry);
  352. if (info != NULL)
  353. list_del_init(&info->state_entry);
  354. return info;
  355. }
  356. /**
  357. * find_page() - Find the page info (if any) associated with a given pbn.
  358. * @pbn: The absolute physical block number of the page.
  359. *
  360. * Return: The page info for the page if available, or NULL if not.
  361. */
  362. static struct page_info * __must_check find_page(struct vdo_page_cache *cache,
  363. physical_block_number_t pbn)
  364. {
  365. if ((cache->last_found != NULL) && (cache->last_found->pbn == pbn))
  366. return cache->last_found;
  367. cache->last_found = vdo_int_map_get(cache->page_map, pbn);
  368. return cache->last_found;
  369. }
  370. /**
  371. * select_lru_page() - Determine which page is least recently used.
  372. *
  373. * Picks the least recently used from among the non-busy entries at the front of each of the lru
  374. * ring. Since whenever we mark a page busy we also put it to the end of the ring it is unlikely
  375. * that the entries at the front are busy unless the queue is very short, but not impossible.
  376. *
  377. * Return: A pointer to the info structure for a relevant page, or NULL if no such page can be
  378. * found. The page can be dirty or resident.
  379. */
  380. static struct page_info * __must_check select_lru_page(struct vdo_page_cache *cache)
  381. {
  382. struct page_info *info;
  383. list_for_each_entry(info, &cache->lru_list, lru_entry)
  384. if ((info->busy == 0) && !is_in_flight(info))
  385. return info;
  386. return NULL;
  387. }
  388. /* ASYNCHRONOUS INTERFACE BEYOND THIS POINT */
  389. /**
  390. * complete_with_page() - Helper to complete the VDO Page Completion request successfully.
  391. * @info: The page info representing the result page.
  392. * @vdo_page_comp: The VDO page completion to complete.
  393. */
  394. static void complete_with_page(struct page_info *info,
  395. struct vdo_page_completion *vdo_page_comp)
  396. {
  397. bool available = vdo_page_comp->writable ? is_present(info) : is_valid(info);
  398. if (!available) {
  399. vdo_log_error_strerror(VDO_BAD_PAGE,
  400. "Requested cache page %llu in state %s is not %s",
  401. (unsigned long long) info->pbn,
  402. get_page_state_name(info->state),
  403. vdo_page_comp->writable ? "present" : "valid");
  404. vdo_fail_completion(&vdo_page_comp->completion, VDO_BAD_PAGE);
  405. return;
  406. }
  407. vdo_page_comp->info = info;
  408. vdo_page_comp->ready = true;
  409. vdo_finish_completion(&vdo_page_comp->completion);
  410. }
  411. /**
  412. * complete_waiter_with_error() - Complete a page completion with an error code.
  413. * @waiter: The page completion, as a waiter.
  414. * @result_ptr: A pointer to the error code.
  415. *
  416. * Implements waiter_callback_fn.
  417. */
  418. static void complete_waiter_with_error(struct vdo_waiter *waiter, void *result_ptr)
  419. {
  420. int *result = result_ptr;
  421. vdo_fail_completion(&page_completion_from_waiter(waiter)->completion, *result);
  422. }
  423. /**
  424. * complete_waiter_with_page() - Complete a page completion with a page.
  425. * @waiter: The page completion, as a waiter.
  426. * @page_info: The page info to complete with.
  427. *
  428. * Implements waiter_callback_fn.
  429. */
  430. static void complete_waiter_with_page(struct vdo_waiter *waiter, void *page_info)
  431. {
  432. complete_with_page(page_info, page_completion_from_waiter(waiter));
  433. }
  434. /**
  435. * distribute_page_over_waitq() - Complete a waitq of VDO page completions with a page result.
  436. *
  437. * Upon completion the waitq will be empty.
  438. *
  439. * Return: The number of pages distributed.
  440. */
  441. static unsigned int distribute_page_over_waitq(struct page_info *info,
  442. struct vdo_wait_queue *waitq)
  443. {
  444. size_t num_pages;
  445. update_lru(info);
  446. num_pages = vdo_waitq_num_waiters(waitq);
  447. /*
  448. * Increment the busy count once for each pending completion so that this page does not
  449. * stop being busy until all completions have been processed.
  450. */
  451. info->busy += num_pages;
  452. vdo_waitq_notify_all_waiters(waitq, complete_waiter_with_page, info);
  453. return num_pages;
  454. }
  455. /**
  456. * set_persistent_error() - Set a persistent error which all requests will receive in the future.
  457. * @context: A string describing what triggered the error.
  458. *
  459. * Once triggered, all enqueued completions will get this error. Any future requests will result in
  460. * this error as well.
  461. */
  462. static void set_persistent_error(struct vdo_page_cache *cache, const char *context,
  463. int result)
  464. {
  465. struct page_info *info;
  466. /* If we're already read-only, there's no need to log. */
  467. struct vdo *vdo = cache->vdo;
  468. if ((result != VDO_READ_ONLY) && !vdo_is_read_only(vdo)) {
  469. vdo_log_error_strerror(result, "VDO Page Cache persistent error: %s",
  470. context);
  471. vdo_enter_read_only_mode(vdo, result);
  472. }
  473. assert_on_cache_thread(cache, __func__);
  474. vdo_waitq_notify_all_waiters(&cache->free_waiters,
  475. complete_waiter_with_error, &result);
  476. cache->waiter_count = 0;
  477. for (info = cache->infos; info < cache->infos + cache->page_count; info++) {
  478. vdo_waitq_notify_all_waiters(&info->waiting,
  479. complete_waiter_with_error, &result);
  480. }
  481. }
  482. /**
  483. * validate_completed_page() - Check that a page completion which is being freed to the cache
  484. * referred to a valid page and is in a valid state.
  485. * @writable: Whether a writable page is required.
  486. *
  487. * Return: VDO_SUCCESS if the page was valid, otherwise as error
  488. */
  489. static int __must_check validate_completed_page(struct vdo_page_completion *completion,
  490. bool writable)
  491. {
  492. int result;
  493. result = VDO_ASSERT(completion->ready, "VDO Page completion not ready");
  494. if (result != VDO_SUCCESS)
  495. return result;
  496. result = VDO_ASSERT(completion->info != NULL,
  497. "VDO Page Completion must be complete");
  498. if (result != VDO_SUCCESS)
  499. return result;
  500. result = VDO_ASSERT(completion->info->pbn == completion->pbn,
  501. "VDO Page Completion pbn must be consistent");
  502. if (result != VDO_SUCCESS)
  503. return result;
  504. result = VDO_ASSERT(is_valid(completion->info),
  505. "VDO Page Completion page must be valid");
  506. if (result != VDO_SUCCESS)
  507. return result;
  508. if (writable) {
  509. result = VDO_ASSERT(completion->writable,
  510. "VDO Page Completion must be writable");
  511. if (result != VDO_SUCCESS)
  512. return result;
  513. }
  514. return VDO_SUCCESS;
  515. }
  516. static void check_for_drain_complete(struct block_map_zone *zone)
  517. {
  518. if (vdo_is_state_draining(&zone->state) &&
  519. (zone->active_lookups == 0) &&
  520. !vdo_waitq_has_waiters(&zone->flush_waiters) &&
  521. !is_vio_pool_busy(zone->vio_pool) &&
  522. (zone->page_cache.outstanding_reads == 0) &&
  523. (zone->page_cache.outstanding_writes == 0)) {
  524. vdo_finish_draining_with_result(&zone->state,
  525. (vdo_is_read_only(zone->block_map->vdo) ?
  526. VDO_READ_ONLY : VDO_SUCCESS));
  527. }
  528. }
  529. static void enter_zone_read_only_mode(struct block_map_zone *zone, int result)
  530. {
  531. vdo_enter_read_only_mode(zone->block_map->vdo, result);
  532. /*
  533. * We are in read-only mode, so we won't ever write any page out.
  534. * Just take all waiters off the waitq so the zone can drain.
  535. */
  536. vdo_waitq_init(&zone->flush_waiters);
  537. check_for_drain_complete(zone);
  538. }
  539. static bool __must_check
  540. validate_completed_page_or_enter_read_only_mode(struct vdo_page_completion *completion,
  541. bool writable)
  542. {
  543. int result = validate_completed_page(completion, writable);
  544. if (result == VDO_SUCCESS)
  545. return true;
  546. enter_zone_read_only_mode(completion->info->cache->zone, result);
  547. return false;
  548. }
  549. /**
  550. * handle_load_error() - Handle page load errors.
  551. * @completion: The page read vio.
  552. */
  553. static void handle_load_error(struct vdo_completion *completion)
  554. {
  555. int result = completion->result;
  556. struct page_info *info = completion->parent;
  557. struct vdo_page_cache *cache = info->cache;
  558. assert_on_cache_thread(cache, __func__);
  559. vio_record_metadata_io_error(as_vio(completion));
  560. vdo_enter_read_only_mode(cache->zone->block_map->vdo, result);
  561. ADD_ONCE(cache->stats.failed_reads, 1);
  562. set_info_state(info, PS_FAILED);
  563. vdo_waitq_notify_all_waiters(&info->waiting, complete_waiter_with_error, &result);
  564. reset_page_info(info);
  565. /*
  566. * Don't decrement until right before calling check_for_drain_complete() to
  567. * ensure that the above work can't cause the page cache to be freed out from under us.
  568. */
  569. cache->outstanding_reads--;
  570. check_for_drain_complete(cache->zone);
  571. }
  572. /**
  573. * page_is_loaded() - Callback used when a page has been loaded.
  574. * @completion: The vio which has loaded the page. Its parent is the page_info.
  575. */
  576. static void page_is_loaded(struct vdo_completion *completion)
  577. {
  578. struct page_info *info = completion->parent;
  579. struct vdo_page_cache *cache = info->cache;
  580. nonce_t nonce = info->cache->zone->block_map->nonce;
  581. struct block_map_page *page;
  582. enum block_map_page_validity validity;
  583. assert_on_cache_thread(cache, __func__);
  584. page = (struct block_map_page *) get_page_buffer(info);
  585. validity = vdo_validate_block_map_page(page, nonce, info->pbn);
  586. if (validity == VDO_BLOCK_MAP_PAGE_BAD) {
  587. physical_block_number_t pbn = vdo_get_block_map_page_pbn(page);
  588. int result = vdo_log_error_strerror(VDO_BAD_PAGE,
  589. "Expected page %llu but got page %llu instead",
  590. (unsigned long long) info->pbn,
  591. (unsigned long long) pbn);
  592. vdo_continue_completion(completion, result);
  593. return;
  594. }
  595. if (validity == VDO_BLOCK_MAP_PAGE_INVALID)
  596. vdo_format_block_map_page(page, nonce, info->pbn, false);
  597. info->recovery_lock = 0;
  598. set_info_state(info, PS_RESIDENT);
  599. distribute_page_over_waitq(info, &info->waiting);
  600. /*
  601. * Don't decrement until right before calling check_for_drain_complete() to
  602. * ensure that the above work can't cause the page cache to be freed out from under us.
  603. */
  604. cache->outstanding_reads--;
  605. check_for_drain_complete(cache->zone);
  606. }
  607. /**
  608. * handle_rebuild_read_error() - Handle a read error during a read-only rebuild.
  609. * @completion: The page load completion.
  610. */
  611. static void handle_rebuild_read_error(struct vdo_completion *completion)
  612. {
  613. struct page_info *info = completion->parent;
  614. struct vdo_page_cache *cache = info->cache;
  615. assert_on_cache_thread(cache, __func__);
  616. /*
  617. * We are doing a read-only rebuild, so treat this as a successful read
  618. * of an uninitialized page.
  619. */
  620. vio_record_metadata_io_error(as_vio(completion));
  621. ADD_ONCE(cache->stats.failed_reads, 1);
  622. memset(get_page_buffer(info), 0, VDO_BLOCK_SIZE);
  623. vdo_reset_completion(completion);
  624. page_is_loaded(completion);
  625. }
  626. static void load_cache_page_endio(struct bio *bio)
  627. {
  628. struct vio *vio = bio->bi_private;
  629. struct page_info *info = vio->completion.parent;
  630. continue_vio_after_io(vio, page_is_loaded, info->cache->zone->thread_id);
  631. }
  632. /**
  633. * launch_page_load() - Begin the process of loading a page.
  634. *
  635. * Return: VDO_SUCCESS or an error code.
  636. */
  637. static int __must_check launch_page_load(struct page_info *info,
  638. physical_block_number_t pbn)
  639. {
  640. int result;
  641. vdo_action_fn callback;
  642. struct vdo_page_cache *cache = info->cache;
  643. assert_io_allowed(cache);
  644. result = set_info_pbn(info, pbn);
  645. if (result != VDO_SUCCESS)
  646. return result;
  647. result = VDO_ASSERT((info->busy == 0), "Page is not busy before loading.");
  648. if (result != VDO_SUCCESS)
  649. return result;
  650. set_info_state(info, PS_INCOMING);
  651. cache->outstanding_reads++;
  652. ADD_ONCE(cache->stats.pages_loaded, 1);
  653. callback = (cache->rebuilding ? handle_rebuild_read_error : handle_load_error);
  654. vdo_submit_metadata_vio(info->vio, pbn, load_cache_page_endio,
  655. callback, REQ_OP_READ | REQ_PRIO);
  656. return VDO_SUCCESS;
  657. }
  658. static void write_pages(struct vdo_completion *completion);
  659. /** handle_flush_error() - Handle errors flushing the layer. */
  660. static void handle_flush_error(struct vdo_completion *completion)
  661. {
  662. struct page_info *info = completion->parent;
  663. vio_record_metadata_io_error(as_vio(completion));
  664. set_persistent_error(info->cache, "flush failed", completion->result);
  665. write_pages(completion);
  666. }
  667. static void flush_endio(struct bio *bio)
  668. {
  669. struct vio *vio = bio->bi_private;
  670. struct page_info *info = vio->completion.parent;
  671. continue_vio_after_io(vio, write_pages, info->cache->zone->thread_id);
  672. }
  673. /** save_pages() - Attempt to save the outgoing pages by first flushing the layer. */
  674. static void save_pages(struct vdo_page_cache *cache)
  675. {
  676. struct page_info *info;
  677. struct vio *vio;
  678. if ((cache->pages_in_flush > 0) || (cache->pages_to_flush == 0))
  679. return;
  680. assert_io_allowed(cache);
  681. info = list_first_entry(&cache->outgoing_list, struct page_info, state_entry);
  682. cache->pages_in_flush = cache->pages_to_flush;
  683. cache->pages_to_flush = 0;
  684. ADD_ONCE(cache->stats.flush_count, 1);
  685. vio = info->vio;
  686. /*
  687. * We must make sure that the recovery journal entries that changed these pages were
  688. * successfully persisted, and thus must issue a flush before each batch of pages is
  689. * written to ensure this.
  690. */
  691. vdo_submit_flush_vio(vio, flush_endio, handle_flush_error);
  692. }
  693. /**
  694. * schedule_page_save() - Add a page to the outgoing list of pages waiting to be saved.
  695. *
  696. * Once in the list, a page may not be used until it has been written out.
  697. */
  698. static void schedule_page_save(struct page_info *info)
  699. {
  700. if (info->busy > 0) {
  701. info->write_status = WRITE_STATUS_DEFERRED;
  702. return;
  703. }
  704. info->cache->pages_to_flush++;
  705. info->cache->outstanding_writes++;
  706. set_info_state(info, PS_OUTGOING);
  707. }
  708. /**
  709. * launch_page_save() - Add a page to outgoing pages waiting to be saved, and then start saving
  710. * pages if another save is not in progress.
  711. */
  712. static void launch_page_save(struct page_info *info)
  713. {
  714. schedule_page_save(info);
  715. save_pages(info->cache);
  716. }
  717. /**
  718. * completion_needs_page() - Determine whether a given vdo_page_completion (as a waiter) is
  719. * requesting a given page number.
  720. * @context: A pointer to the pbn of the desired page.
  721. *
  722. * Implements waiter_match_fn.
  723. *
  724. * Return: true if the page completion is for the desired page number.
  725. */
  726. static bool completion_needs_page(struct vdo_waiter *waiter, void *context)
  727. {
  728. physical_block_number_t *pbn = context;
  729. return (page_completion_from_waiter(waiter)->pbn == *pbn);
  730. }
  731. /**
  732. * allocate_free_page() - Allocate a free page to the first completion in the waiting queue, and
  733. * any other completions that match it in page number.
  734. */
  735. static void allocate_free_page(struct page_info *info)
  736. {
  737. int result;
  738. struct vdo_waiter *oldest_waiter;
  739. physical_block_number_t pbn;
  740. struct vdo_page_cache *cache = info->cache;
  741. assert_on_cache_thread(cache, __func__);
  742. if (!vdo_waitq_has_waiters(&cache->free_waiters)) {
  743. if (cache->stats.cache_pressure > 0) {
  744. vdo_log_info("page cache pressure relieved");
  745. WRITE_ONCE(cache->stats.cache_pressure, 0);
  746. }
  747. return;
  748. }
  749. result = reset_page_info(info);
  750. if (result != VDO_SUCCESS) {
  751. set_persistent_error(cache, "cannot reset page info", result);
  752. return;
  753. }
  754. oldest_waiter = vdo_waitq_get_first_waiter(&cache->free_waiters);
  755. pbn = page_completion_from_waiter(oldest_waiter)->pbn;
  756. /*
  757. * Remove all entries which match the page number in question and push them onto the page
  758. * info's waitq.
  759. */
  760. vdo_waitq_dequeue_matching_waiters(&cache->free_waiters, completion_needs_page,
  761. &pbn, &info->waiting);
  762. cache->waiter_count -= vdo_waitq_num_waiters(&info->waiting);
  763. result = launch_page_load(info, pbn);
  764. if (result != VDO_SUCCESS) {
  765. vdo_waitq_notify_all_waiters(&info->waiting,
  766. complete_waiter_with_error, &result);
  767. }
  768. }
  769. /**
  770. * discard_a_page() - Begin the process of discarding a page.
  771. *
  772. * If no page is discardable, increments a count of deferred frees so that the next release of a
  773. * page which is no longer busy will kick off another discard cycle. This is an indication that the
  774. * cache is not big enough.
  775. *
  776. * If the selected page is not dirty, immediately allocates the page to the oldest completion
  777. * waiting for a free page.
  778. */
  779. static void discard_a_page(struct vdo_page_cache *cache)
  780. {
  781. struct page_info *info = select_lru_page(cache);
  782. if (info == NULL) {
  783. report_cache_pressure(cache);
  784. return;
  785. }
  786. if (!is_dirty(info)) {
  787. allocate_free_page(info);
  788. return;
  789. }
  790. VDO_ASSERT_LOG_ONLY(!is_in_flight(info),
  791. "page selected for discard is not in flight");
  792. cache->discard_count++;
  793. info->write_status = WRITE_STATUS_DISCARD;
  794. launch_page_save(info);
  795. }
  796. /**
  797. * discard_page_for_completion() - Helper used to trigger a discard so that the completion can get
  798. * a different page.
  799. */
  800. static void discard_page_for_completion(struct vdo_page_completion *vdo_page_comp)
  801. {
  802. struct vdo_page_cache *cache = vdo_page_comp->cache;
  803. cache->waiter_count++;
  804. vdo_waitq_enqueue_waiter(&cache->free_waiters, &vdo_page_comp->waiter);
  805. discard_a_page(cache);
  806. }
  807. /**
  808. * discard_page_if_needed() - Helper used to trigger a discard if the cache needs another free
  809. * page.
  810. * @cache: The page cache.
  811. */
  812. static void discard_page_if_needed(struct vdo_page_cache *cache)
  813. {
  814. if (cache->waiter_count > cache->discard_count)
  815. discard_a_page(cache);
  816. }
  817. /**
  818. * write_has_finished() - Inform the cache that a write has finished (possibly with an error).
  819. * @info: The info structure for the page whose write just completed.
  820. *
  821. * Return: true if the page write was a discard.
  822. */
  823. static bool write_has_finished(struct page_info *info)
  824. {
  825. bool was_discard = (info->write_status == WRITE_STATUS_DISCARD);
  826. assert_on_cache_thread(info->cache, __func__);
  827. info->cache->outstanding_writes--;
  828. info->write_status = WRITE_STATUS_NORMAL;
  829. return was_discard;
  830. }
  831. /**
  832. * handle_page_write_error() - Handler for page write errors.
  833. * @completion: The page write vio.
  834. */
  835. static void handle_page_write_error(struct vdo_completion *completion)
  836. {
  837. int result = completion->result;
  838. struct page_info *info = completion->parent;
  839. struct vdo_page_cache *cache = info->cache;
  840. vio_record_metadata_io_error(as_vio(completion));
  841. /* If we're already read-only, write failures are to be expected. */
  842. if (result != VDO_READ_ONLY) {
  843. vdo_log_ratelimit(vdo_log_error,
  844. "failed to write block map page %llu",
  845. (unsigned long long) info->pbn);
  846. }
  847. set_info_state(info, PS_DIRTY);
  848. ADD_ONCE(cache->stats.failed_writes, 1);
  849. set_persistent_error(cache, "cannot write page", result);
  850. if (!write_has_finished(info))
  851. discard_page_if_needed(cache);
  852. check_for_drain_complete(cache->zone);
  853. }
  854. static void page_is_written_out(struct vdo_completion *completion);
  855. static void write_cache_page_endio(struct bio *bio)
  856. {
  857. struct vio *vio = bio->bi_private;
  858. struct page_info *info = vio->completion.parent;
  859. continue_vio_after_io(vio, page_is_written_out, info->cache->zone->thread_id);
  860. }
  861. /**
  862. * page_is_written_out() - Callback used when a page has been written out.
  863. * @completion: The vio which wrote the page. Its parent is a page_info.
  864. */
  865. static void page_is_written_out(struct vdo_completion *completion)
  866. {
  867. bool was_discard, reclaimed;
  868. u32 reclamations;
  869. struct page_info *info = completion->parent;
  870. struct vdo_page_cache *cache = info->cache;
  871. struct block_map_page *page = (struct block_map_page *) get_page_buffer(info);
  872. if (!page->header.initialized) {
  873. page->header.initialized = true;
  874. vdo_submit_metadata_vio(info->vio, info->pbn,
  875. write_cache_page_endio,
  876. handle_page_write_error,
  877. REQ_OP_WRITE | REQ_PRIO | REQ_PREFLUSH);
  878. return;
  879. }
  880. /* Handle journal updates and torn write protection. */
  881. vdo_release_recovery_journal_block_reference(cache->zone->block_map->journal,
  882. info->recovery_lock,
  883. VDO_ZONE_TYPE_LOGICAL,
  884. cache->zone->zone_number);
  885. info->recovery_lock = 0;
  886. was_discard = write_has_finished(info);
  887. reclaimed = (!was_discard || (info->busy > 0) || vdo_waitq_has_waiters(&info->waiting));
  888. set_info_state(info, PS_RESIDENT);
  889. reclamations = distribute_page_over_waitq(info, &info->waiting);
  890. ADD_ONCE(cache->stats.reclaimed, reclamations);
  891. if (was_discard)
  892. cache->discard_count--;
  893. if (reclaimed)
  894. discard_page_if_needed(cache);
  895. else
  896. allocate_free_page(info);
  897. check_for_drain_complete(cache->zone);
  898. }
  899. /**
  900. * write_pages() - Write the batch of pages which were covered by the layer flush which just
  901. * completed.
  902. * @flush_completion: The flush vio.
  903. *
  904. * This callback is registered in save_pages().
  905. */
  906. static void write_pages(struct vdo_completion *flush_completion)
  907. {
  908. struct vdo_page_cache *cache = ((struct page_info *) flush_completion->parent)->cache;
  909. /*
  910. * We need to cache these two values on the stack since it is possible for the last
  911. * page info to cause the page cache to get freed. Hence once we launch the last page,
  912. * it may be unsafe to dereference the cache.
  913. */
  914. bool has_unflushed_pages = (cache->pages_to_flush > 0);
  915. page_count_t pages_in_flush = cache->pages_in_flush;
  916. cache->pages_in_flush = 0;
  917. while (pages_in_flush-- > 0) {
  918. struct page_info *info =
  919. list_first_entry(&cache->outgoing_list, struct page_info,
  920. state_entry);
  921. list_del_init(&info->state_entry);
  922. if (vdo_is_read_only(info->cache->vdo)) {
  923. struct vdo_completion *completion = &info->vio->completion;
  924. vdo_reset_completion(completion);
  925. completion->callback = page_is_written_out;
  926. completion->error_handler = handle_page_write_error;
  927. vdo_fail_completion(completion, VDO_READ_ONLY);
  928. continue;
  929. }
  930. ADD_ONCE(info->cache->stats.pages_saved, 1);
  931. vdo_submit_metadata_vio(info->vio, info->pbn, write_cache_page_endio,
  932. handle_page_write_error, REQ_OP_WRITE | REQ_PRIO);
  933. }
  934. if (has_unflushed_pages) {
  935. /*
  936. * If there are unflushed pages, the cache can't have been freed, so this call is
  937. * safe.
  938. */
  939. save_pages(cache);
  940. }
  941. }
  942. /**
  943. * vdo_release_page_completion() - Release a VDO Page Completion.
  944. *
  945. * The page referenced by this completion (if any) will no longer be held busy by this completion.
  946. * If a page becomes discardable and there are completions awaiting free pages then a new round of
  947. * page discarding is started.
  948. */
  949. void vdo_release_page_completion(struct vdo_completion *completion)
  950. {
  951. struct page_info *discard_info = NULL;
  952. struct vdo_page_completion *page_completion = as_vdo_page_completion(completion);
  953. struct vdo_page_cache *cache;
  954. if (completion->result == VDO_SUCCESS) {
  955. if (!validate_completed_page_or_enter_read_only_mode(page_completion, false))
  956. return;
  957. if (--page_completion->info->busy == 0)
  958. discard_info = page_completion->info;
  959. }
  960. VDO_ASSERT_LOG_ONLY((page_completion->waiter.next_waiter == NULL),
  961. "Page being released after leaving all queues");
  962. page_completion->info = NULL;
  963. cache = page_completion->cache;
  964. assert_on_cache_thread(cache, __func__);
  965. if (discard_info != NULL) {
  966. if (discard_info->write_status == WRITE_STATUS_DEFERRED) {
  967. discard_info->write_status = WRITE_STATUS_NORMAL;
  968. launch_page_save(discard_info);
  969. }
  970. /*
  971. * if there are excess requests for pages (that have not already started discards)
  972. * we need to discard some page (which may be this one)
  973. */
  974. discard_page_if_needed(cache);
  975. }
  976. }
  977. /**
  978. * load_page_for_completion() - Helper function to load a page as described by a VDO Page
  979. * Completion.
  980. */
  981. static void load_page_for_completion(struct page_info *info,
  982. struct vdo_page_completion *vdo_page_comp)
  983. {
  984. int result;
  985. vdo_waitq_enqueue_waiter(&info->waiting, &vdo_page_comp->waiter);
  986. result = launch_page_load(info, vdo_page_comp->pbn);
  987. if (result != VDO_SUCCESS) {
  988. vdo_waitq_notify_all_waiters(&info->waiting,
  989. complete_waiter_with_error, &result);
  990. }
  991. }
  992. /**
  993. * vdo_get_page() - Initialize a page completion and get a block map page.
  994. * @page_completion: The vdo_page_completion to initialize.
  995. * @zone: The block map zone of the desired page.
  996. * @pbn: The absolute physical block of the desired page.
  997. * @writable: Whether the page can be modified.
  998. * @parent: The object to notify when the fetch is complete.
  999. * @callback: The notification callback.
  1000. * @error_handler: The handler for fetch errors.
  1001. * @requeue: Whether we must requeue when notifying the parent.
  1002. *
  1003. * May cause another page to be discarded (potentially writing a dirty page) and the one nominated
  1004. * by the completion to be loaded from disk. When the callback is invoked, the page will be
  1005. * resident in the cache and marked busy. All callers must call vdo_release_page_completion()
  1006. * when they are done with the page to clear the busy mark.
  1007. */
  1008. void vdo_get_page(struct vdo_page_completion *page_completion,
  1009. struct block_map_zone *zone, physical_block_number_t pbn,
  1010. bool writable, void *parent, vdo_action_fn callback,
  1011. vdo_action_fn error_handler, bool requeue)
  1012. {
  1013. struct vdo_page_cache *cache = &zone->page_cache;
  1014. struct vdo_completion *completion = &page_completion->completion;
  1015. struct page_info *info;
  1016. assert_on_cache_thread(cache, __func__);
  1017. VDO_ASSERT_LOG_ONLY((page_completion->waiter.next_waiter == NULL),
  1018. "New page completion was not already on a wait queue");
  1019. *page_completion = (struct vdo_page_completion) {
  1020. .pbn = pbn,
  1021. .writable = writable,
  1022. .cache = cache,
  1023. };
  1024. vdo_initialize_completion(completion, cache->vdo, VDO_PAGE_COMPLETION);
  1025. vdo_prepare_completion(completion, callback, error_handler,
  1026. cache->zone->thread_id, parent);
  1027. completion->requeue = requeue;
  1028. if (page_completion->writable && vdo_is_read_only(cache->vdo)) {
  1029. vdo_fail_completion(completion, VDO_READ_ONLY);
  1030. return;
  1031. }
  1032. if (page_completion->writable)
  1033. ADD_ONCE(cache->stats.write_count, 1);
  1034. else
  1035. ADD_ONCE(cache->stats.read_count, 1);
  1036. info = find_page(cache, page_completion->pbn);
  1037. if (info != NULL) {
  1038. /* The page is in the cache already. */
  1039. if ((info->write_status == WRITE_STATUS_DEFERRED) ||
  1040. is_incoming(info) ||
  1041. (is_outgoing(info) && page_completion->writable)) {
  1042. /* The page is unusable until it has finished I/O. */
  1043. ADD_ONCE(cache->stats.wait_for_page, 1);
  1044. vdo_waitq_enqueue_waiter(&info->waiting, &page_completion->waiter);
  1045. return;
  1046. }
  1047. if (is_valid(info)) {
  1048. /* The page is usable. */
  1049. ADD_ONCE(cache->stats.found_in_cache, 1);
  1050. if (!is_present(info))
  1051. ADD_ONCE(cache->stats.read_outgoing, 1);
  1052. update_lru(info);
  1053. info->busy++;
  1054. complete_with_page(info, page_completion);
  1055. return;
  1056. }
  1057. /* Something horrible has gone wrong. */
  1058. VDO_ASSERT_LOG_ONLY(false, "Info found in a usable state.");
  1059. }
  1060. /* The page must be fetched. */
  1061. info = find_free_page(cache);
  1062. if (info != NULL) {
  1063. ADD_ONCE(cache->stats.fetch_required, 1);
  1064. load_page_for_completion(info, page_completion);
  1065. return;
  1066. }
  1067. /* The page must wait for a page to be discarded. */
  1068. ADD_ONCE(cache->stats.discard_required, 1);
  1069. discard_page_for_completion(page_completion);
  1070. }
  1071. /**
  1072. * vdo_request_page_write() - Request that a VDO page be written out as soon as it is not busy.
  1073. * @completion: The vdo_page_completion containing the page.
  1074. */
  1075. void vdo_request_page_write(struct vdo_completion *completion)
  1076. {
  1077. struct page_info *info;
  1078. struct vdo_page_completion *vdo_page_comp = as_vdo_page_completion(completion);
  1079. if (!validate_completed_page_or_enter_read_only_mode(vdo_page_comp, true))
  1080. return;
  1081. info = vdo_page_comp->info;
  1082. set_info_state(info, PS_DIRTY);
  1083. launch_page_save(info);
  1084. }
  1085. /**
  1086. * vdo_get_cached_page() - Get the block map page from a page completion.
  1087. * @completion: A vdo page completion whose callback has been called.
  1088. * @page_ptr: A pointer to hold the page
  1089. *
  1090. * Return: VDO_SUCCESS or an error
  1091. */
  1092. int vdo_get_cached_page(struct vdo_completion *completion,
  1093. struct block_map_page **page_ptr)
  1094. {
  1095. int result;
  1096. struct vdo_page_completion *vpc;
  1097. vpc = as_vdo_page_completion(completion);
  1098. result = validate_completed_page(vpc, true);
  1099. if (result == VDO_SUCCESS)
  1100. *page_ptr = (struct block_map_page *) get_page_buffer(vpc->info);
  1101. return result;
  1102. }
  1103. /**
  1104. * vdo_invalidate_page_cache() - Invalidate all entries in the VDO page cache.
  1105. *
  1106. * There must not be any dirty pages in the cache.
  1107. *
  1108. * Return: A success or error code.
  1109. */
  1110. int vdo_invalidate_page_cache(struct vdo_page_cache *cache)
  1111. {
  1112. struct page_info *info;
  1113. assert_on_cache_thread(cache, __func__);
  1114. /* Make sure we don't throw away any dirty pages. */
  1115. for (info = cache->infos; info < cache->infos + cache->page_count; info++) {
  1116. int result = VDO_ASSERT(!is_dirty(info), "cache must have no dirty pages");
  1117. if (result != VDO_SUCCESS)
  1118. return result;
  1119. }
  1120. /* Reset the page map by re-allocating it. */
  1121. vdo_int_map_free(vdo_forget(cache->page_map));
  1122. return vdo_int_map_create(cache->page_count, &cache->page_map);
  1123. }
  1124. /**
  1125. * get_tree_page_by_index() - Get the tree page for a given height and page index.
  1126. *
  1127. * Return: The requested page.
  1128. */
  1129. static struct tree_page * __must_check get_tree_page_by_index(struct forest *forest,
  1130. root_count_t root_index,
  1131. height_t height,
  1132. page_number_t page_index)
  1133. {
  1134. page_number_t offset = 0;
  1135. size_t segment;
  1136. for (segment = 0; segment < forest->segments; segment++) {
  1137. page_number_t border = forest->boundaries[segment].levels[height - 1];
  1138. if (page_index < border) {
  1139. struct block_map_tree *tree = &forest->trees[root_index];
  1140. return &(tree->segments[segment].levels[height - 1][page_index - offset]);
  1141. }
  1142. offset = border;
  1143. }
  1144. return NULL;
  1145. }
  1146. /* Get the page referred to by the lock's tree slot at its current height. */
  1147. static inline struct tree_page *get_tree_page(const struct block_map_zone *zone,
  1148. const struct tree_lock *lock)
  1149. {
  1150. return get_tree_page_by_index(zone->block_map->forest, lock->root_index,
  1151. lock->height,
  1152. lock->tree_slots[lock->height].page_index);
  1153. }
  1154. /** vdo_copy_valid_page() - Validate and copy a buffer to a page. */
  1155. bool vdo_copy_valid_page(char *buffer, nonce_t nonce,
  1156. physical_block_number_t pbn,
  1157. struct block_map_page *page)
  1158. {
  1159. struct block_map_page *loaded = (struct block_map_page *) buffer;
  1160. enum block_map_page_validity validity =
  1161. vdo_validate_block_map_page(loaded, nonce, pbn);
  1162. if (validity == VDO_BLOCK_MAP_PAGE_VALID) {
  1163. memcpy(page, loaded, VDO_BLOCK_SIZE);
  1164. return true;
  1165. }
  1166. if (validity == VDO_BLOCK_MAP_PAGE_BAD) {
  1167. vdo_log_error_strerror(VDO_BAD_PAGE,
  1168. "Expected page %llu but got page %llu instead",
  1169. (unsigned long long) pbn,
  1170. (unsigned long long) vdo_get_block_map_page_pbn(loaded));
  1171. }
  1172. return false;
  1173. }
  1174. /**
  1175. * in_cyclic_range() - Check whether the given value is between the lower and upper bounds, within
  1176. * a cyclic range of values from 0 to (modulus - 1).
  1177. * @lower: The lowest value to accept.
  1178. * @value: The value to check.
  1179. * @upper: The highest value to accept.
  1180. * @modulus: The size of the cyclic space, no more than 2^15.
  1181. *
  1182. * The value and both bounds must be smaller than the modulus.
  1183. *
  1184. * Return: true if the value is in range.
  1185. */
  1186. static bool in_cyclic_range(u16 lower, u16 value, u16 upper, u16 modulus)
  1187. {
  1188. if (value < lower)
  1189. value += modulus;
  1190. if (upper < lower)
  1191. upper += modulus;
  1192. return (value <= upper);
  1193. }
  1194. /**
  1195. * is_not_older() - Check whether a generation is strictly older than some other generation in the
  1196. * context of a zone's current generation range.
  1197. * @zone: The zone in which to do the comparison.
  1198. * @a: The generation in question.
  1199. * @b: The generation to compare to.
  1200. *
  1201. * Return: true if generation @a is not strictly older than generation @b in the context of @zone
  1202. */
  1203. static bool __must_check is_not_older(struct block_map_zone *zone, u8 a, u8 b)
  1204. {
  1205. int result;
  1206. result = VDO_ASSERT((in_cyclic_range(zone->oldest_generation, a, zone->generation, 1 << 8) &&
  1207. in_cyclic_range(zone->oldest_generation, b, zone->generation, 1 << 8)),
  1208. "generation(s) %u, %u are out of range [%u, %u]",
  1209. a, b, zone->oldest_generation, zone->generation);
  1210. if (result != VDO_SUCCESS) {
  1211. enter_zone_read_only_mode(zone, result);
  1212. return true;
  1213. }
  1214. return in_cyclic_range(b, a, zone->generation, 1 << 8);
  1215. }
  1216. static void release_generation(struct block_map_zone *zone, u8 generation)
  1217. {
  1218. int result;
  1219. result = VDO_ASSERT((zone->dirty_page_counts[generation] > 0),
  1220. "dirty page count underflow for generation %u", generation);
  1221. if (result != VDO_SUCCESS) {
  1222. enter_zone_read_only_mode(zone, result);
  1223. return;
  1224. }
  1225. zone->dirty_page_counts[generation]--;
  1226. while ((zone->dirty_page_counts[zone->oldest_generation] == 0) &&
  1227. (zone->oldest_generation != zone->generation))
  1228. zone->oldest_generation++;
  1229. }
  1230. static void set_generation(struct block_map_zone *zone, struct tree_page *page,
  1231. u8 new_generation)
  1232. {
  1233. u32 new_count;
  1234. int result;
  1235. bool decrement_old = vdo_waiter_is_waiting(&page->waiter);
  1236. u8 old_generation = page->generation;
  1237. if (decrement_old && (old_generation == new_generation))
  1238. return;
  1239. page->generation = new_generation;
  1240. new_count = ++zone->dirty_page_counts[new_generation];
  1241. result = VDO_ASSERT((new_count != 0), "dirty page count overflow for generation %u",
  1242. new_generation);
  1243. if (result != VDO_SUCCESS) {
  1244. enter_zone_read_only_mode(zone, result);
  1245. return;
  1246. }
  1247. if (decrement_old)
  1248. release_generation(zone, old_generation);
  1249. }
  1250. static void write_page(struct tree_page *tree_page, struct pooled_vio *vio);
  1251. /* Implements waiter_callback_fn */
  1252. static void write_page_callback(struct vdo_waiter *waiter, void *context)
  1253. {
  1254. write_page(container_of(waiter, struct tree_page, waiter), context);
  1255. }
  1256. static void acquire_vio(struct vdo_waiter *waiter, struct block_map_zone *zone)
  1257. {
  1258. waiter->callback = write_page_callback;
  1259. acquire_vio_from_pool(zone->vio_pool, waiter);
  1260. }
  1261. /* Return: true if all possible generations were not already active */
  1262. static bool attempt_increment(struct block_map_zone *zone)
  1263. {
  1264. u8 generation = zone->generation + 1;
  1265. if (zone->oldest_generation == generation)
  1266. return false;
  1267. zone->generation = generation;
  1268. return true;
  1269. }
  1270. /* Launches a flush if one is not already in progress. */
  1271. static void enqueue_page(struct tree_page *page, struct block_map_zone *zone)
  1272. {
  1273. if ((zone->flusher == NULL) && attempt_increment(zone)) {
  1274. zone->flusher = page;
  1275. acquire_vio(&page->waiter, zone);
  1276. return;
  1277. }
  1278. vdo_waitq_enqueue_waiter(&zone->flush_waiters, &page->waiter);
  1279. }
  1280. static void write_page_if_not_dirtied(struct vdo_waiter *waiter, void *context)
  1281. {
  1282. struct tree_page *page = container_of(waiter, struct tree_page, waiter);
  1283. struct write_if_not_dirtied_context *write_context = context;
  1284. if (page->generation == write_context->generation) {
  1285. acquire_vio(waiter, write_context->zone);
  1286. return;
  1287. }
  1288. enqueue_page(page, write_context->zone);
  1289. }
  1290. static void return_to_pool(struct block_map_zone *zone, struct pooled_vio *vio)
  1291. {
  1292. return_vio_to_pool(zone->vio_pool, vio);
  1293. check_for_drain_complete(zone);
  1294. }
  1295. /* This callback is registered in write_initialized_page(). */
  1296. static void finish_page_write(struct vdo_completion *completion)
  1297. {
  1298. bool dirty;
  1299. struct vio *vio = as_vio(completion);
  1300. struct pooled_vio *pooled = container_of(vio, struct pooled_vio, vio);
  1301. struct tree_page *page = completion->parent;
  1302. struct block_map_zone *zone = pooled->context;
  1303. vdo_release_recovery_journal_block_reference(zone->block_map->journal,
  1304. page->writing_recovery_lock,
  1305. VDO_ZONE_TYPE_LOGICAL,
  1306. zone->zone_number);
  1307. dirty = (page->writing_generation != page->generation);
  1308. release_generation(zone, page->writing_generation);
  1309. page->writing = false;
  1310. if (zone->flusher == page) {
  1311. struct write_if_not_dirtied_context context = {
  1312. .zone = zone,
  1313. .generation = page->writing_generation,
  1314. };
  1315. vdo_waitq_notify_all_waiters(&zone->flush_waiters,
  1316. write_page_if_not_dirtied, &context);
  1317. if (dirty && attempt_increment(zone)) {
  1318. write_page(page, pooled);
  1319. return;
  1320. }
  1321. zone->flusher = NULL;
  1322. }
  1323. if (dirty) {
  1324. enqueue_page(page, zone);
  1325. } else if ((zone->flusher == NULL) && vdo_waitq_has_waiters(&zone->flush_waiters) &&
  1326. attempt_increment(zone)) {
  1327. zone->flusher = container_of(vdo_waitq_dequeue_waiter(&zone->flush_waiters),
  1328. struct tree_page, waiter);
  1329. write_page(zone->flusher, pooled);
  1330. return;
  1331. }
  1332. return_to_pool(zone, pooled);
  1333. }
  1334. static void handle_write_error(struct vdo_completion *completion)
  1335. {
  1336. int result = completion->result;
  1337. struct vio *vio = as_vio(completion);
  1338. struct pooled_vio *pooled = container_of(vio, struct pooled_vio, vio);
  1339. struct block_map_zone *zone = pooled->context;
  1340. vio_record_metadata_io_error(vio);
  1341. enter_zone_read_only_mode(zone, result);
  1342. return_to_pool(zone, pooled);
  1343. }
  1344. static void write_page_endio(struct bio *bio);
  1345. static void write_initialized_page(struct vdo_completion *completion)
  1346. {
  1347. struct vio *vio = as_vio(completion);
  1348. struct pooled_vio *pooled = container_of(vio, struct pooled_vio, vio);
  1349. struct block_map_zone *zone = pooled->context;
  1350. struct tree_page *tree_page = completion->parent;
  1351. struct block_map_page *page = (struct block_map_page *) vio->data;
  1352. blk_opf_t operation = REQ_OP_WRITE | REQ_PRIO;
  1353. /*
  1354. * Now that we know the page has been written at least once, mark the copy we are writing
  1355. * as initialized.
  1356. */
  1357. page->header.initialized = true;
  1358. if (zone->flusher == tree_page)
  1359. operation |= REQ_PREFLUSH;
  1360. vdo_submit_metadata_vio(vio, vdo_get_block_map_page_pbn(page),
  1361. write_page_endio, handle_write_error,
  1362. operation);
  1363. }
  1364. static void write_page_endio(struct bio *bio)
  1365. {
  1366. struct pooled_vio *vio = bio->bi_private;
  1367. struct block_map_zone *zone = vio->context;
  1368. struct block_map_page *page = (struct block_map_page *) vio->vio.data;
  1369. continue_vio_after_io(&vio->vio,
  1370. (page->header.initialized ?
  1371. finish_page_write : write_initialized_page),
  1372. zone->thread_id);
  1373. }
  1374. static void write_page(struct tree_page *tree_page, struct pooled_vio *vio)
  1375. {
  1376. struct vdo_completion *completion = &vio->vio.completion;
  1377. struct block_map_zone *zone = vio->context;
  1378. struct block_map_page *page = vdo_as_block_map_page(tree_page);
  1379. if ((zone->flusher != tree_page) &&
  1380. is_not_older(zone, tree_page->generation, zone->generation)) {
  1381. /*
  1382. * This page was re-dirtied after the last flush was issued, hence we need to do
  1383. * another flush.
  1384. */
  1385. enqueue_page(tree_page, zone);
  1386. return_to_pool(zone, vio);
  1387. return;
  1388. }
  1389. completion->parent = tree_page;
  1390. memcpy(vio->vio.data, tree_page->page_buffer, VDO_BLOCK_SIZE);
  1391. completion->callback_thread_id = zone->thread_id;
  1392. tree_page->writing = true;
  1393. tree_page->writing_generation = tree_page->generation;
  1394. tree_page->writing_recovery_lock = tree_page->recovery_lock;
  1395. /* Clear this now so that we know this page is not on any dirty list. */
  1396. tree_page->recovery_lock = 0;
  1397. /*
  1398. * We've already copied the page into the vio which will write it, so if it was not yet
  1399. * initialized, the first write will indicate that (for torn write protection). It is now
  1400. * safe to mark it as initialized in memory since if the write fails, the in memory state
  1401. * will become irrelevant.
  1402. */
  1403. if (page->header.initialized) {
  1404. write_initialized_page(completion);
  1405. return;
  1406. }
  1407. page->header.initialized = true;
  1408. vdo_submit_metadata_vio(&vio->vio, vdo_get_block_map_page_pbn(page),
  1409. write_page_endio, handle_write_error,
  1410. REQ_OP_WRITE | REQ_PRIO);
  1411. }
  1412. /* Release a lock on a page which was being loaded or allocated. */
  1413. static void release_page_lock(struct data_vio *data_vio, char *what)
  1414. {
  1415. struct block_map_zone *zone;
  1416. struct tree_lock *lock_holder;
  1417. struct tree_lock *lock = &data_vio->tree_lock;
  1418. VDO_ASSERT_LOG_ONLY(lock->locked,
  1419. "release of unlocked block map page %s for key %llu in tree %u",
  1420. what, (unsigned long long) lock->key, lock->root_index);
  1421. zone = data_vio->logical.zone->block_map_zone;
  1422. lock_holder = vdo_int_map_remove(zone->loading_pages, lock->key);
  1423. VDO_ASSERT_LOG_ONLY((lock_holder == lock),
  1424. "block map page %s mismatch for key %llu in tree %u",
  1425. what, (unsigned long long) lock->key, lock->root_index);
  1426. lock->locked = false;
  1427. }
  1428. static void finish_lookup(struct data_vio *data_vio, int result)
  1429. {
  1430. data_vio->tree_lock.height = 0;
  1431. --data_vio->logical.zone->block_map_zone->active_lookups;
  1432. set_data_vio_logical_callback(data_vio, continue_data_vio_with_block_map_slot);
  1433. data_vio->vio.completion.error_handler = handle_data_vio_error;
  1434. continue_data_vio_with_error(data_vio, result);
  1435. }
  1436. static void abort_lookup_for_waiter(struct vdo_waiter *waiter, void *context)
  1437. {
  1438. struct data_vio *data_vio = vdo_waiter_as_data_vio(waiter);
  1439. int result = *((int *) context);
  1440. if (!data_vio->write) {
  1441. if (result == VDO_NO_SPACE)
  1442. result = VDO_SUCCESS;
  1443. } else if (result != VDO_NO_SPACE) {
  1444. result = VDO_READ_ONLY;
  1445. }
  1446. finish_lookup(data_vio, result);
  1447. }
  1448. static void abort_lookup(struct data_vio *data_vio, int result, char *what)
  1449. {
  1450. if (result != VDO_NO_SPACE)
  1451. enter_zone_read_only_mode(data_vio->logical.zone->block_map_zone, result);
  1452. if (data_vio->tree_lock.locked) {
  1453. release_page_lock(data_vio, what);
  1454. vdo_waitq_notify_all_waiters(&data_vio->tree_lock.waiters,
  1455. abort_lookup_for_waiter,
  1456. &result);
  1457. }
  1458. finish_lookup(data_vio, result);
  1459. }
  1460. static void abort_load(struct data_vio *data_vio, int result)
  1461. {
  1462. abort_lookup(data_vio, result, "load");
  1463. }
  1464. static bool __must_check is_invalid_tree_entry(const struct vdo *vdo,
  1465. const struct data_location *mapping,
  1466. height_t height)
  1467. {
  1468. if (!vdo_is_valid_location(mapping) ||
  1469. vdo_is_state_compressed(mapping->state) ||
  1470. (vdo_is_mapped_location(mapping) && (mapping->pbn == VDO_ZERO_BLOCK)))
  1471. return true;
  1472. /* Roots aren't physical data blocks, so we can't check their PBNs. */
  1473. if (height == VDO_BLOCK_MAP_TREE_HEIGHT)
  1474. return false;
  1475. return !vdo_is_physical_data_block(vdo->depot, mapping->pbn);
  1476. }
  1477. static void load_block_map_page(struct block_map_zone *zone, struct data_vio *data_vio);
  1478. static void allocate_block_map_page(struct block_map_zone *zone,
  1479. struct data_vio *data_vio);
  1480. static void continue_with_loaded_page(struct data_vio *data_vio,
  1481. struct block_map_page *page)
  1482. {
  1483. struct tree_lock *lock = &data_vio->tree_lock;
  1484. struct block_map_tree_slot slot = lock->tree_slots[lock->height];
  1485. struct data_location mapping =
  1486. vdo_unpack_block_map_entry(&page->entries[slot.block_map_slot.slot]);
  1487. if (is_invalid_tree_entry(vdo_from_data_vio(data_vio), &mapping, lock->height)) {
  1488. vdo_log_error_strerror(VDO_BAD_MAPPING,
  1489. "Invalid block map tree PBN: %llu with state %u for page index %u at height %u",
  1490. (unsigned long long) mapping.pbn, mapping.state,
  1491. lock->tree_slots[lock->height - 1].page_index,
  1492. lock->height - 1);
  1493. abort_load(data_vio, VDO_BAD_MAPPING);
  1494. return;
  1495. }
  1496. if (!vdo_is_mapped_location(&mapping)) {
  1497. /* The page we need is unallocated */
  1498. allocate_block_map_page(data_vio->logical.zone->block_map_zone,
  1499. data_vio);
  1500. return;
  1501. }
  1502. lock->tree_slots[lock->height - 1].block_map_slot.pbn = mapping.pbn;
  1503. if (lock->height == 1) {
  1504. finish_lookup(data_vio, VDO_SUCCESS);
  1505. return;
  1506. }
  1507. /* We know what page we need to load next */
  1508. load_block_map_page(data_vio->logical.zone->block_map_zone, data_vio);
  1509. }
  1510. static void continue_load_for_waiter(struct vdo_waiter *waiter, void *context)
  1511. {
  1512. struct data_vio *data_vio = vdo_waiter_as_data_vio(waiter);
  1513. data_vio->tree_lock.height--;
  1514. continue_with_loaded_page(data_vio, context);
  1515. }
  1516. static void finish_block_map_page_load(struct vdo_completion *completion)
  1517. {
  1518. physical_block_number_t pbn;
  1519. struct tree_page *tree_page;
  1520. struct block_map_page *page;
  1521. nonce_t nonce;
  1522. struct vio *vio = as_vio(completion);
  1523. struct pooled_vio *pooled = vio_as_pooled_vio(vio);
  1524. struct data_vio *data_vio = completion->parent;
  1525. struct block_map_zone *zone = pooled->context;
  1526. struct tree_lock *tree_lock = &data_vio->tree_lock;
  1527. tree_lock->height--;
  1528. pbn = tree_lock->tree_slots[tree_lock->height].block_map_slot.pbn;
  1529. tree_page = get_tree_page(zone, tree_lock);
  1530. page = (struct block_map_page *) tree_page->page_buffer;
  1531. nonce = zone->block_map->nonce;
  1532. if (!vdo_copy_valid_page(vio->data, nonce, pbn, page))
  1533. vdo_format_block_map_page(page, nonce, pbn, false);
  1534. return_vio_to_pool(zone->vio_pool, pooled);
  1535. /* Release our claim to the load and wake any waiters */
  1536. release_page_lock(data_vio, "load");
  1537. vdo_waitq_notify_all_waiters(&tree_lock->waiters, continue_load_for_waiter, page);
  1538. continue_with_loaded_page(data_vio, page);
  1539. }
  1540. static void handle_io_error(struct vdo_completion *completion)
  1541. {
  1542. int result = completion->result;
  1543. struct vio *vio = as_vio(completion);
  1544. struct pooled_vio *pooled = container_of(vio, struct pooled_vio, vio);
  1545. struct data_vio *data_vio = completion->parent;
  1546. struct block_map_zone *zone = pooled->context;
  1547. vio_record_metadata_io_error(vio);
  1548. return_vio_to_pool(zone->vio_pool, pooled);
  1549. abort_load(data_vio, result);
  1550. }
  1551. static void load_page_endio(struct bio *bio)
  1552. {
  1553. struct vio *vio = bio->bi_private;
  1554. struct data_vio *data_vio = vio->completion.parent;
  1555. continue_vio_after_io(vio, finish_block_map_page_load,
  1556. data_vio->logical.zone->thread_id);
  1557. }
  1558. static void load_page(struct vdo_waiter *waiter, void *context)
  1559. {
  1560. struct pooled_vio *pooled = context;
  1561. struct data_vio *data_vio = vdo_waiter_as_data_vio(waiter);
  1562. struct tree_lock *lock = &data_vio->tree_lock;
  1563. physical_block_number_t pbn = lock->tree_slots[lock->height - 1].block_map_slot.pbn;
  1564. pooled->vio.completion.parent = data_vio;
  1565. vdo_submit_metadata_vio(&pooled->vio, pbn, load_page_endio,
  1566. handle_io_error, REQ_OP_READ | REQ_PRIO);
  1567. }
  1568. /*
  1569. * If the page is already locked, queue up to wait for the lock to be released. If the lock is
  1570. * acquired, @data_vio->tree_lock.locked will be true.
  1571. */
  1572. static int attempt_page_lock(struct block_map_zone *zone, struct data_vio *data_vio)
  1573. {
  1574. int result;
  1575. struct tree_lock *lock_holder;
  1576. struct tree_lock *lock = &data_vio->tree_lock;
  1577. height_t height = lock->height;
  1578. struct block_map_tree_slot tree_slot = lock->tree_slots[height];
  1579. union page_key key;
  1580. key.descriptor = (struct page_descriptor) {
  1581. .root_index = lock->root_index,
  1582. .height = height,
  1583. .page_index = tree_slot.page_index,
  1584. .slot = tree_slot.block_map_slot.slot,
  1585. };
  1586. lock->key = key.key;
  1587. result = vdo_int_map_put(zone->loading_pages, lock->key,
  1588. lock, false, (void **) &lock_holder);
  1589. if (result != VDO_SUCCESS)
  1590. return result;
  1591. if (lock_holder == NULL) {
  1592. /* We got the lock */
  1593. data_vio->tree_lock.locked = true;
  1594. return VDO_SUCCESS;
  1595. }
  1596. /* Someone else is loading or allocating the page we need */
  1597. vdo_waitq_enqueue_waiter(&lock_holder->waiters, &data_vio->waiter);
  1598. return VDO_SUCCESS;
  1599. }
  1600. /* Load a block map tree page from disk, for the next level in the data vio tree lock. */
  1601. static void load_block_map_page(struct block_map_zone *zone, struct data_vio *data_vio)
  1602. {
  1603. int result;
  1604. result = attempt_page_lock(zone, data_vio);
  1605. if (result != VDO_SUCCESS) {
  1606. abort_load(data_vio, result);
  1607. return;
  1608. }
  1609. if (data_vio->tree_lock.locked) {
  1610. data_vio->waiter.callback = load_page;
  1611. acquire_vio_from_pool(zone->vio_pool, &data_vio->waiter);
  1612. }
  1613. }
  1614. static void allocation_failure(struct vdo_completion *completion)
  1615. {
  1616. struct data_vio *data_vio = as_data_vio(completion);
  1617. if (vdo_requeue_completion_if_needed(completion,
  1618. data_vio->logical.zone->thread_id))
  1619. return;
  1620. abort_lookup(data_vio, completion->result, "allocation");
  1621. }
  1622. static void continue_allocation_for_waiter(struct vdo_waiter *waiter, void *context)
  1623. {
  1624. struct data_vio *data_vio = vdo_waiter_as_data_vio(waiter);
  1625. struct tree_lock *tree_lock = &data_vio->tree_lock;
  1626. physical_block_number_t pbn = *((physical_block_number_t *) context);
  1627. tree_lock->height--;
  1628. data_vio->tree_lock.tree_slots[tree_lock->height].block_map_slot.pbn = pbn;
  1629. if (tree_lock->height == 0) {
  1630. finish_lookup(data_vio, VDO_SUCCESS);
  1631. return;
  1632. }
  1633. allocate_block_map_page(data_vio->logical.zone->block_map_zone, data_vio);
  1634. }
  1635. /** expire_oldest_list() - Expire the oldest list. */
  1636. static void expire_oldest_list(struct dirty_lists *dirty_lists)
  1637. {
  1638. block_count_t i = dirty_lists->offset++;
  1639. dirty_lists->oldest_period++;
  1640. if (!list_empty(&dirty_lists->eras[i][VDO_TREE_PAGE])) {
  1641. list_splice_tail_init(&dirty_lists->eras[i][VDO_TREE_PAGE],
  1642. &dirty_lists->expired[VDO_TREE_PAGE]);
  1643. }
  1644. if (!list_empty(&dirty_lists->eras[i][VDO_CACHE_PAGE])) {
  1645. list_splice_tail_init(&dirty_lists->eras[i][VDO_CACHE_PAGE],
  1646. &dirty_lists->expired[VDO_CACHE_PAGE]);
  1647. }
  1648. if (dirty_lists->offset == dirty_lists->maximum_age)
  1649. dirty_lists->offset = 0;
  1650. }
  1651. /** update_period() - Update the dirty_lists period if necessary. */
  1652. static void update_period(struct dirty_lists *dirty, sequence_number_t period)
  1653. {
  1654. while (dirty->next_period <= period) {
  1655. if ((dirty->next_period - dirty->oldest_period) == dirty->maximum_age)
  1656. expire_oldest_list(dirty);
  1657. dirty->next_period++;
  1658. }
  1659. }
  1660. /** write_expired_elements() - Write out the expired list. */
  1661. static void write_expired_elements(struct block_map_zone *zone)
  1662. {
  1663. struct tree_page *page, *ttmp;
  1664. struct page_info *info, *ptmp;
  1665. struct list_head *expired;
  1666. u8 generation = zone->generation;
  1667. expired = &zone->dirty_lists->expired[VDO_TREE_PAGE];
  1668. list_for_each_entry_safe(page, ttmp, expired, entry) {
  1669. int result;
  1670. list_del_init(&page->entry);
  1671. result = VDO_ASSERT(!vdo_waiter_is_waiting(&page->waiter),
  1672. "Newly expired page not already waiting to write");
  1673. if (result != VDO_SUCCESS) {
  1674. enter_zone_read_only_mode(zone, result);
  1675. continue;
  1676. }
  1677. set_generation(zone, page, generation);
  1678. if (!page->writing)
  1679. enqueue_page(page, zone);
  1680. }
  1681. expired = &zone->dirty_lists->expired[VDO_CACHE_PAGE];
  1682. list_for_each_entry_safe(info, ptmp, expired, state_entry) {
  1683. list_del_init(&info->state_entry);
  1684. schedule_page_save(info);
  1685. }
  1686. save_pages(&zone->page_cache);
  1687. }
  1688. /**
  1689. * add_to_dirty_lists() - Add an element to the dirty lists.
  1690. * @zone: The zone in which we are operating.
  1691. * @entry: The list entry of the element to add.
  1692. * @type: The type of page.
  1693. * @old_period: The period in which the element was previously dirtied, or 0 if it was not dirty.
  1694. * @new_period: The period in which the element has now been dirtied, or 0 if it does not hold a
  1695. * lock.
  1696. */
  1697. static void add_to_dirty_lists(struct block_map_zone *zone,
  1698. struct list_head *entry,
  1699. enum block_map_page_type type,
  1700. sequence_number_t old_period,
  1701. sequence_number_t new_period)
  1702. {
  1703. struct dirty_lists *dirty_lists = zone->dirty_lists;
  1704. if ((old_period == new_period) || ((old_period != 0) && (old_period < new_period)))
  1705. return;
  1706. if (new_period < dirty_lists->oldest_period) {
  1707. list_move_tail(entry, &dirty_lists->expired[type]);
  1708. } else {
  1709. update_period(dirty_lists, new_period);
  1710. list_move_tail(entry,
  1711. &dirty_lists->eras[new_period % dirty_lists->maximum_age][type]);
  1712. }
  1713. write_expired_elements(zone);
  1714. }
  1715. /*
  1716. * Record the allocation in the tree and wake any waiters now that the write lock has been
  1717. * released.
  1718. */
  1719. static void finish_block_map_allocation(struct vdo_completion *completion)
  1720. {
  1721. physical_block_number_t pbn;
  1722. struct tree_page *tree_page;
  1723. struct block_map_page *page;
  1724. sequence_number_t old_lock;
  1725. struct data_vio *data_vio = as_data_vio(completion);
  1726. struct block_map_zone *zone = data_vio->logical.zone->block_map_zone;
  1727. struct tree_lock *tree_lock = &data_vio->tree_lock;
  1728. height_t height = tree_lock->height;
  1729. assert_data_vio_in_logical_zone(data_vio);
  1730. tree_page = get_tree_page(zone, tree_lock);
  1731. pbn = tree_lock->tree_slots[height - 1].block_map_slot.pbn;
  1732. /* Record the allocation. */
  1733. page = (struct block_map_page *) tree_page->page_buffer;
  1734. old_lock = tree_page->recovery_lock;
  1735. vdo_update_block_map_page(page, data_vio, pbn,
  1736. VDO_MAPPING_STATE_UNCOMPRESSED,
  1737. &tree_page->recovery_lock);
  1738. if (vdo_waiter_is_waiting(&tree_page->waiter)) {
  1739. /* This page is waiting to be written out. */
  1740. if (zone->flusher != tree_page) {
  1741. /*
  1742. * The outstanding flush won't cover the update we just made,
  1743. * so mark the page as needing another flush.
  1744. */
  1745. set_generation(zone, tree_page, zone->generation);
  1746. }
  1747. } else {
  1748. /* Put the page on a dirty list */
  1749. if (old_lock == 0)
  1750. INIT_LIST_HEAD(&tree_page->entry);
  1751. add_to_dirty_lists(zone, &tree_page->entry, VDO_TREE_PAGE,
  1752. old_lock, tree_page->recovery_lock);
  1753. }
  1754. tree_lock->height--;
  1755. if (height > 1) {
  1756. /* Format the interior node we just allocated (in memory). */
  1757. tree_page = get_tree_page(zone, tree_lock);
  1758. vdo_format_block_map_page(tree_page->page_buffer,
  1759. zone->block_map->nonce,
  1760. pbn, false);
  1761. }
  1762. /* Release our claim to the allocation and wake any waiters */
  1763. release_page_lock(data_vio, "allocation");
  1764. vdo_waitq_notify_all_waiters(&tree_lock->waiters,
  1765. continue_allocation_for_waiter, &pbn);
  1766. if (tree_lock->height == 0) {
  1767. finish_lookup(data_vio, VDO_SUCCESS);
  1768. return;
  1769. }
  1770. allocate_block_map_page(zone, data_vio);
  1771. }
  1772. static void release_block_map_write_lock(struct vdo_completion *completion)
  1773. {
  1774. struct data_vio *data_vio = as_data_vio(completion);
  1775. assert_data_vio_in_allocated_zone(data_vio);
  1776. release_data_vio_allocation_lock(data_vio, true);
  1777. launch_data_vio_logical_callback(data_vio, finish_block_map_allocation);
  1778. }
  1779. /*
  1780. * Newly allocated block map pages are set to have to MAXIMUM_REFERENCES after they are journaled,
  1781. * to prevent deduplication against the block after we release the write lock on it, but before we
  1782. * write out the page.
  1783. */
  1784. static void set_block_map_page_reference_count(struct vdo_completion *completion)
  1785. {
  1786. struct data_vio *data_vio = as_data_vio(completion);
  1787. assert_data_vio_in_allocated_zone(data_vio);
  1788. completion->callback = release_block_map_write_lock;
  1789. vdo_modify_reference_count(completion, &data_vio->increment_updater);
  1790. }
  1791. static void journal_block_map_allocation(struct vdo_completion *completion)
  1792. {
  1793. struct data_vio *data_vio = as_data_vio(completion);
  1794. assert_data_vio_in_journal_zone(data_vio);
  1795. set_data_vio_allocated_zone_callback(data_vio,
  1796. set_block_map_page_reference_count);
  1797. vdo_add_recovery_journal_entry(completion->vdo->recovery_journal, data_vio);
  1798. }
  1799. static void allocate_block(struct vdo_completion *completion)
  1800. {
  1801. struct data_vio *data_vio = as_data_vio(completion);
  1802. struct tree_lock *lock = &data_vio->tree_lock;
  1803. physical_block_number_t pbn;
  1804. assert_data_vio_in_allocated_zone(data_vio);
  1805. if (!vdo_allocate_block_in_zone(data_vio))
  1806. return;
  1807. pbn = data_vio->allocation.pbn;
  1808. lock->tree_slots[lock->height - 1].block_map_slot.pbn = pbn;
  1809. data_vio->increment_updater = (struct reference_updater) {
  1810. .operation = VDO_JOURNAL_BLOCK_MAP_REMAPPING,
  1811. .increment = true,
  1812. .zpbn = {
  1813. .pbn = pbn,
  1814. .state = VDO_MAPPING_STATE_UNCOMPRESSED,
  1815. },
  1816. .lock = data_vio->allocation.lock,
  1817. };
  1818. launch_data_vio_journal_callback(data_vio, journal_block_map_allocation);
  1819. }
  1820. static void allocate_block_map_page(struct block_map_zone *zone,
  1821. struct data_vio *data_vio)
  1822. {
  1823. int result;
  1824. if (!data_vio->write || data_vio->is_discard) {
  1825. /* This is a pure read or a discard, so there's nothing left to do here. */
  1826. finish_lookup(data_vio, VDO_SUCCESS);
  1827. return;
  1828. }
  1829. result = attempt_page_lock(zone, data_vio);
  1830. if (result != VDO_SUCCESS) {
  1831. abort_lookup(data_vio, result, "allocation");
  1832. return;
  1833. }
  1834. if (!data_vio->tree_lock.locked)
  1835. return;
  1836. data_vio_allocate_data_block(data_vio, VIO_BLOCK_MAP_WRITE_LOCK,
  1837. allocate_block, allocation_failure);
  1838. }
  1839. /**
  1840. * vdo_find_block_map_slot() - Find the block map slot in which the block map entry for a data_vio
  1841. * resides and cache that result in the data_vio.
  1842. *
  1843. * All ancestors in the tree will be allocated or loaded, as needed.
  1844. */
  1845. void vdo_find_block_map_slot(struct data_vio *data_vio)
  1846. {
  1847. page_number_t page_index;
  1848. struct block_map_tree_slot tree_slot;
  1849. struct data_location mapping;
  1850. struct block_map_page *page = NULL;
  1851. struct tree_lock *lock = &data_vio->tree_lock;
  1852. struct block_map_zone *zone = data_vio->logical.zone->block_map_zone;
  1853. zone->active_lookups++;
  1854. if (vdo_is_state_draining(&zone->state)) {
  1855. finish_lookup(data_vio, VDO_SHUTTING_DOWN);
  1856. return;
  1857. }
  1858. lock->tree_slots[0].block_map_slot.slot =
  1859. data_vio->logical.lbn % VDO_BLOCK_MAP_ENTRIES_PER_PAGE;
  1860. page_index = (lock->tree_slots[0].page_index / zone->block_map->root_count);
  1861. tree_slot = (struct block_map_tree_slot) {
  1862. .page_index = page_index / VDO_BLOCK_MAP_ENTRIES_PER_PAGE,
  1863. .block_map_slot = {
  1864. .pbn = 0,
  1865. .slot = page_index % VDO_BLOCK_MAP_ENTRIES_PER_PAGE,
  1866. },
  1867. };
  1868. for (lock->height = 1; lock->height <= VDO_BLOCK_MAP_TREE_HEIGHT; lock->height++) {
  1869. physical_block_number_t pbn;
  1870. lock->tree_slots[lock->height] = tree_slot;
  1871. page = (struct block_map_page *) (get_tree_page(zone, lock)->page_buffer);
  1872. pbn = vdo_get_block_map_page_pbn(page);
  1873. if (pbn != VDO_ZERO_BLOCK) {
  1874. lock->tree_slots[lock->height].block_map_slot.pbn = pbn;
  1875. break;
  1876. }
  1877. /* Calculate the index and slot for the next level. */
  1878. tree_slot.block_map_slot.slot =
  1879. tree_slot.page_index % VDO_BLOCK_MAP_ENTRIES_PER_PAGE;
  1880. tree_slot.page_index = tree_slot.page_index / VDO_BLOCK_MAP_ENTRIES_PER_PAGE;
  1881. }
  1882. /* The page at this height has been allocated and loaded. */
  1883. mapping = vdo_unpack_block_map_entry(&page->entries[tree_slot.block_map_slot.slot]);
  1884. if (is_invalid_tree_entry(vdo_from_data_vio(data_vio), &mapping, lock->height)) {
  1885. vdo_log_error_strerror(VDO_BAD_MAPPING,
  1886. "Invalid block map tree PBN: %llu with state %u for page index %u at height %u",
  1887. (unsigned long long) mapping.pbn, mapping.state,
  1888. lock->tree_slots[lock->height - 1].page_index,
  1889. lock->height - 1);
  1890. abort_load(data_vio, VDO_BAD_MAPPING);
  1891. return;
  1892. }
  1893. if (!vdo_is_mapped_location(&mapping)) {
  1894. /* The page we want one level down has not been allocated, so allocate it. */
  1895. allocate_block_map_page(zone, data_vio);
  1896. return;
  1897. }
  1898. lock->tree_slots[lock->height - 1].block_map_slot.pbn = mapping.pbn;
  1899. if (lock->height == 1) {
  1900. /* This is the ultimate block map page, so we're done */
  1901. finish_lookup(data_vio, VDO_SUCCESS);
  1902. return;
  1903. }
  1904. /* We know what page we need to load. */
  1905. load_block_map_page(zone, data_vio);
  1906. }
  1907. /*
  1908. * Find the PBN of a leaf block map page. This method may only be used after all allocated tree
  1909. * pages have been loaded, otherwise, it may give the wrong answer (0).
  1910. */
  1911. physical_block_number_t vdo_find_block_map_page_pbn(struct block_map *map,
  1912. page_number_t page_number)
  1913. {
  1914. struct data_location mapping;
  1915. struct tree_page *tree_page;
  1916. struct block_map_page *page;
  1917. root_count_t root_index = page_number % map->root_count;
  1918. page_number_t page_index = page_number / map->root_count;
  1919. slot_number_t slot = page_index % VDO_BLOCK_MAP_ENTRIES_PER_PAGE;
  1920. page_index /= VDO_BLOCK_MAP_ENTRIES_PER_PAGE;
  1921. tree_page = get_tree_page_by_index(map->forest, root_index, 1, page_index);
  1922. page = (struct block_map_page *) tree_page->page_buffer;
  1923. if (!page->header.initialized)
  1924. return VDO_ZERO_BLOCK;
  1925. mapping = vdo_unpack_block_map_entry(&page->entries[slot]);
  1926. if (!vdo_is_valid_location(&mapping) || vdo_is_state_compressed(mapping.state))
  1927. return VDO_ZERO_BLOCK;
  1928. return mapping.pbn;
  1929. }
  1930. /*
  1931. * Write a tree page or indicate that it has been re-dirtied if it is already being written. This
  1932. * method is used when correcting errors in the tree during read-only rebuild.
  1933. */
  1934. void vdo_write_tree_page(struct tree_page *page, struct block_map_zone *zone)
  1935. {
  1936. bool waiting = vdo_waiter_is_waiting(&page->waiter);
  1937. if (waiting && (zone->flusher == page))
  1938. return;
  1939. set_generation(zone, page, zone->generation);
  1940. if (waiting || page->writing)
  1941. return;
  1942. enqueue_page(page, zone);
  1943. }
  1944. static int make_segment(struct forest *old_forest, block_count_t new_pages,
  1945. struct boundary *new_boundary, struct forest *forest)
  1946. {
  1947. size_t index = (old_forest == NULL) ? 0 : old_forest->segments;
  1948. struct tree_page *page_ptr;
  1949. page_count_t segment_sizes[VDO_BLOCK_MAP_TREE_HEIGHT];
  1950. height_t height;
  1951. root_count_t root;
  1952. int result;
  1953. forest->segments = index + 1;
  1954. result = vdo_allocate(forest->segments, struct boundary,
  1955. "forest boundary array", &forest->boundaries);
  1956. if (result != VDO_SUCCESS)
  1957. return result;
  1958. result = vdo_allocate(forest->segments, struct tree_page *,
  1959. "forest page pointers", &forest->pages);
  1960. if (result != VDO_SUCCESS)
  1961. return result;
  1962. result = vdo_allocate(new_pages, struct tree_page,
  1963. "new forest pages", &forest->pages[index]);
  1964. if (result != VDO_SUCCESS)
  1965. return result;
  1966. if (index > 0) {
  1967. memcpy(forest->boundaries, old_forest->boundaries,
  1968. index * sizeof(struct boundary));
  1969. memcpy(forest->pages, old_forest->pages,
  1970. index * sizeof(struct tree_page *));
  1971. }
  1972. memcpy(&(forest->boundaries[index]), new_boundary, sizeof(struct boundary));
  1973. for (height = 0; height < VDO_BLOCK_MAP_TREE_HEIGHT; height++) {
  1974. segment_sizes[height] = new_boundary->levels[height];
  1975. if (index > 0)
  1976. segment_sizes[height] -= old_forest->boundaries[index - 1].levels[height];
  1977. }
  1978. page_ptr = forest->pages[index];
  1979. for (root = 0; root < forest->map->root_count; root++) {
  1980. struct block_map_tree_segment *segment;
  1981. struct block_map_tree *tree = &(forest->trees[root]);
  1982. height_t height;
  1983. int result = vdo_allocate(forest->segments,
  1984. struct block_map_tree_segment,
  1985. "tree root segments", &tree->segments);
  1986. if (result != VDO_SUCCESS)
  1987. return result;
  1988. if (index > 0) {
  1989. memcpy(tree->segments, old_forest->trees[root].segments,
  1990. index * sizeof(struct block_map_tree_segment));
  1991. }
  1992. segment = &(tree->segments[index]);
  1993. for (height = 0; height < VDO_BLOCK_MAP_TREE_HEIGHT; height++) {
  1994. if (segment_sizes[height] == 0)
  1995. continue;
  1996. segment->levels[height] = page_ptr;
  1997. if (height == (VDO_BLOCK_MAP_TREE_HEIGHT - 1)) {
  1998. /* Record the root. */
  1999. struct block_map_page *page =
  2000. vdo_format_block_map_page(page_ptr->page_buffer,
  2001. forest->map->nonce,
  2002. VDO_INVALID_PBN, true);
  2003. page->entries[0] =
  2004. vdo_pack_block_map_entry(forest->map->root_origin + root,
  2005. VDO_MAPPING_STATE_UNCOMPRESSED);
  2006. }
  2007. page_ptr += segment_sizes[height];
  2008. }
  2009. }
  2010. return VDO_SUCCESS;
  2011. }
  2012. static void deforest(struct forest *forest, size_t first_page_segment)
  2013. {
  2014. root_count_t root;
  2015. if (forest->pages != NULL) {
  2016. size_t segment;
  2017. for (segment = first_page_segment; segment < forest->segments; segment++)
  2018. vdo_free(forest->pages[segment]);
  2019. vdo_free(forest->pages);
  2020. }
  2021. for (root = 0; root < forest->map->root_count; root++)
  2022. vdo_free(forest->trees[root].segments);
  2023. vdo_free(forest->boundaries);
  2024. vdo_free(forest);
  2025. }
  2026. /**
  2027. * make_forest() - Make a collection of trees for a block_map, expanding the existing forest if
  2028. * there is one.
  2029. * @entries: The number of entries the block map will hold.
  2030. *
  2031. * Return: VDO_SUCCESS or an error.
  2032. */
  2033. static int make_forest(struct block_map *map, block_count_t entries)
  2034. {
  2035. struct forest *forest, *old_forest = map->forest;
  2036. struct boundary new_boundary, *old_boundary = NULL;
  2037. block_count_t new_pages;
  2038. int result;
  2039. if (old_forest != NULL)
  2040. old_boundary = &(old_forest->boundaries[old_forest->segments - 1]);
  2041. new_pages = vdo_compute_new_forest_pages(map->root_count, old_boundary,
  2042. entries, &new_boundary);
  2043. if (new_pages == 0) {
  2044. map->next_entry_count = entries;
  2045. return VDO_SUCCESS;
  2046. }
  2047. result = vdo_allocate_extended(struct forest, map->root_count,
  2048. struct block_map_tree, __func__,
  2049. &forest);
  2050. if (result != VDO_SUCCESS)
  2051. return result;
  2052. forest->map = map;
  2053. result = make_segment(old_forest, new_pages, &new_boundary, forest);
  2054. if (result != VDO_SUCCESS) {
  2055. deforest(forest, forest->segments - 1);
  2056. return result;
  2057. }
  2058. map->next_forest = forest;
  2059. map->next_entry_count = entries;
  2060. return VDO_SUCCESS;
  2061. }
  2062. /**
  2063. * replace_forest() - Replace a block_map's forest with the already-prepared larger forest.
  2064. */
  2065. static void replace_forest(struct block_map *map)
  2066. {
  2067. if (map->next_forest != NULL) {
  2068. if (map->forest != NULL)
  2069. deforest(map->forest, map->forest->segments);
  2070. map->forest = vdo_forget(map->next_forest);
  2071. }
  2072. map->entry_count = map->next_entry_count;
  2073. map->next_entry_count = 0;
  2074. }
  2075. /**
  2076. * finish_cursor() - Finish the traversal of a single tree. If it was the last cursor, finish the
  2077. * traversal.
  2078. */
  2079. static void finish_cursor(struct cursor *cursor)
  2080. {
  2081. struct cursors *cursors = cursor->parent;
  2082. struct vdo_completion *completion = cursors->completion;
  2083. return_vio_to_pool(cursors->pool, vdo_forget(cursor->vio));
  2084. if (--cursors->active_roots > 0)
  2085. return;
  2086. vdo_free(cursors);
  2087. vdo_finish_completion(completion);
  2088. }
  2089. static void traverse(struct cursor *cursor);
  2090. /**
  2091. * continue_traversal() - Continue traversing a block map tree.
  2092. * @completion: The VIO doing a read or write.
  2093. */
  2094. static void continue_traversal(struct vdo_completion *completion)
  2095. {
  2096. vio_record_metadata_io_error(as_vio(completion));
  2097. traverse(completion->parent);
  2098. }
  2099. /**
  2100. * finish_traversal_load() - Continue traversing a block map tree now that a page has been loaded.
  2101. * @completion: The VIO doing the read.
  2102. */
  2103. static void finish_traversal_load(struct vdo_completion *completion)
  2104. {
  2105. struct cursor *cursor = completion->parent;
  2106. height_t height = cursor->height;
  2107. struct cursor_level *level = &cursor->levels[height];
  2108. struct tree_page *tree_page =
  2109. &(cursor->tree->segments[0].levels[height][level->page_index]);
  2110. struct block_map_page *page = (struct block_map_page *) tree_page->page_buffer;
  2111. vdo_copy_valid_page(cursor->vio->vio.data,
  2112. cursor->parent->zone->block_map->nonce,
  2113. pbn_from_vio_bio(cursor->vio->vio.bio), page);
  2114. traverse(cursor);
  2115. }
  2116. static void traversal_endio(struct bio *bio)
  2117. {
  2118. struct vio *vio = bio->bi_private;
  2119. struct cursor *cursor = vio->completion.parent;
  2120. continue_vio_after_io(vio, finish_traversal_load,
  2121. cursor->parent->zone->thread_id);
  2122. }
  2123. /**
  2124. * traverse() - Traverse a single block map tree.
  2125. *
  2126. * This is the recursive heart of the traversal process.
  2127. */
  2128. static void traverse(struct cursor *cursor)
  2129. {
  2130. for (; cursor->height < VDO_BLOCK_MAP_TREE_HEIGHT; cursor->height++) {
  2131. height_t height = cursor->height;
  2132. struct cursor_level *level = &cursor->levels[height];
  2133. struct tree_page *tree_page =
  2134. &(cursor->tree->segments[0].levels[height][level->page_index]);
  2135. struct block_map_page *page = (struct block_map_page *) tree_page->page_buffer;
  2136. if (!page->header.initialized)
  2137. continue;
  2138. for (; level->slot < VDO_BLOCK_MAP_ENTRIES_PER_PAGE; level->slot++) {
  2139. struct cursor_level *next_level;
  2140. page_number_t entry_index =
  2141. (VDO_BLOCK_MAP_ENTRIES_PER_PAGE * level->page_index) + level->slot;
  2142. struct data_location location =
  2143. vdo_unpack_block_map_entry(&page->entries[level->slot]);
  2144. if (!vdo_is_valid_location(&location)) {
  2145. /* This entry is invalid, so remove it from the page. */
  2146. page->entries[level->slot] = UNMAPPED_BLOCK_MAP_ENTRY;
  2147. vdo_write_tree_page(tree_page, cursor->parent->zone);
  2148. continue;
  2149. }
  2150. if (!vdo_is_mapped_location(&location))
  2151. continue;
  2152. /* Erase mapped entries past the end of the logical space. */
  2153. if (entry_index >= cursor->boundary.levels[height]) {
  2154. page->entries[level->slot] = UNMAPPED_BLOCK_MAP_ENTRY;
  2155. vdo_write_tree_page(tree_page, cursor->parent->zone);
  2156. continue;
  2157. }
  2158. if (cursor->height < VDO_BLOCK_MAP_TREE_HEIGHT - 1) {
  2159. int result = cursor->parent->entry_callback(location.pbn,
  2160. cursor->parent->completion);
  2161. if (result != VDO_SUCCESS) {
  2162. page->entries[level->slot] = UNMAPPED_BLOCK_MAP_ENTRY;
  2163. vdo_write_tree_page(tree_page, cursor->parent->zone);
  2164. continue;
  2165. }
  2166. }
  2167. if (cursor->height == 0)
  2168. continue;
  2169. cursor->height--;
  2170. next_level = &cursor->levels[cursor->height];
  2171. next_level->page_index = entry_index;
  2172. next_level->slot = 0;
  2173. level->slot++;
  2174. vdo_submit_metadata_vio(&cursor->vio->vio, location.pbn,
  2175. traversal_endio, continue_traversal,
  2176. REQ_OP_READ | REQ_PRIO);
  2177. return;
  2178. }
  2179. }
  2180. finish_cursor(cursor);
  2181. }
  2182. /**
  2183. * launch_cursor() - Start traversing a single block map tree now that the cursor has a VIO with
  2184. * which to load pages.
  2185. * @context: The pooled_vio just acquired.
  2186. *
  2187. * Implements waiter_callback_fn.
  2188. */
  2189. static void launch_cursor(struct vdo_waiter *waiter, void *context)
  2190. {
  2191. struct cursor *cursor = container_of(waiter, struct cursor, waiter);
  2192. struct pooled_vio *pooled = context;
  2193. cursor->vio = pooled;
  2194. pooled->vio.completion.parent = cursor;
  2195. pooled->vio.completion.callback_thread_id = cursor->parent->zone->thread_id;
  2196. traverse(cursor);
  2197. }
  2198. /**
  2199. * compute_boundary() - Compute the number of pages used at each level of the given root's tree.
  2200. *
  2201. * Return: The list of page counts as a boundary structure.
  2202. */
  2203. static struct boundary compute_boundary(struct block_map *map, root_count_t root_index)
  2204. {
  2205. struct boundary boundary;
  2206. height_t height;
  2207. page_count_t leaf_pages = vdo_compute_block_map_page_count(map->entry_count);
  2208. /*
  2209. * Compute the leaf pages for this root. If the number of leaf pages does not distribute
  2210. * evenly, we must determine if this root gets an extra page. Extra pages are assigned to
  2211. * roots starting from tree 0.
  2212. */
  2213. page_count_t last_tree_root = (leaf_pages - 1) % map->root_count;
  2214. page_count_t level_pages = leaf_pages / map->root_count;
  2215. if (root_index <= last_tree_root)
  2216. level_pages++;
  2217. for (height = 0; height < VDO_BLOCK_MAP_TREE_HEIGHT - 1; height++) {
  2218. boundary.levels[height] = level_pages;
  2219. level_pages = DIV_ROUND_UP(level_pages, VDO_BLOCK_MAP_ENTRIES_PER_PAGE);
  2220. }
  2221. /* The root node always exists, even if the root is otherwise unused. */
  2222. boundary.levels[VDO_BLOCK_MAP_TREE_HEIGHT - 1] = 1;
  2223. return boundary;
  2224. }
  2225. /**
  2226. * vdo_traverse_forest() - Walk the entire forest of a block map.
  2227. * @callback: A function to call with the pbn of each allocated node in the forest.
  2228. * @completion: The completion to notify on each traversed PBN, and when traversal completes.
  2229. */
  2230. void vdo_traverse_forest(struct block_map *map, vdo_entry_callback_fn callback,
  2231. struct vdo_completion *completion)
  2232. {
  2233. root_count_t root;
  2234. struct cursors *cursors;
  2235. int result;
  2236. result = vdo_allocate_extended(struct cursors, map->root_count,
  2237. struct cursor, __func__, &cursors);
  2238. if (result != VDO_SUCCESS) {
  2239. vdo_fail_completion(completion, result);
  2240. return;
  2241. }
  2242. cursors->zone = &map->zones[0];
  2243. cursors->pool = cursors->zone->vio_pool;
  2244. cursors->entry_callback = callback;
  2245. cursors->completion = completion;
  2246. cursors->active_roots = map->root_count;
  2247. for (root = 0; root < map->root_count; root++) {
  2248. struct cursor *cursor = &cursors->cursors[root];
  2249. *cursor = (struct cursor) {
  2250. .tree = &map->forest->trees[root],
  2251. .height = VDO_BLOCK_MAP_TREE_HEIGHT - 1,
  2252. .parent = cursors,
  2253. .boundary = compute_boundary(map, root),
  2254. };
  2255. cursor->waiter.callback = launch_cursor;
  2256. acquire_vio_from_pool(cursors->pool, &cursor->waiter);
  2257. }
  2258. }
  2259. /**
  2260. * initialize_block_map_zone() - Initialize the per-zone portions of the block map.
  2261. * @maximum_age: The number of journal blocks before a dirtied page is considered old and must be
  2262. * written out.
  2263. */
  2264. static int __must_check initialize_block_map_zone(struct block_map *map,
  2265. zone_count_t zone_number,
  2266. page_count_t cache_size,
  2267. block_count_t maximum_age)
  2268. {
  2269. int result;
  2270. block_count_t i;
  2271. struct vdo *vdo = map->vdo;
  2272. struct block_map_zone *zone = &map->zones[zone_number];
  2273. BUILD_BUG_ON(sizeof(struct page_descriptor) != sizeof(u64));
  2274. zone->zone_number = zone_number;
  2275. zone->thread_id = vdo->thread_config.logical_threads[zone_number];
  2276. zone->block_map = map;
  2277. result = vdo_allocate_extended(struct dirty_lists, maximum_age,
  2278. dirty_era_t, __func__,
  2279. &zone->dirty_lists);
  2280. if (result != VDO_SUCCESS)
  2281. return result;
  2282. zone->dirty_lists->maximum_age = maximum_age;
  2283. INIT_LIST_HEAD(&zone->dirty_lists->expired[VDO_TREE_PAGE]);
  2284. INIT_LIST_HEAD(&zone->dirty_lists->expired[VDO_CACHE_PAGE]);
  2285. for (i = 0; i < maximum_age; i++) {
  2286. INIT_LIST_HEAD(&zone->dirty_lists->eras[i][VDO_TREE_PAGE]);
  2287. INIT_LIST_HEAD(&zone->dirty_lists->eras[i][VDO_CACHE_PAGE]);
  2288. }
  2289. result = vdo_int_map_create(VDO_LOCK_MAP_CAPACITY, &zone->loading_pages);
  2290. if (result != VDO_SUCCESS)
  2291. return result;
  2292. result = make_vio_pool(vdo, BLOCK_MAP_VIO_POOL_SIZE,
  2293. zone->thread_id, VIO_TYPE_BLOCK_MAP_INTERIOR,
  2294. VIO_PRIORITY_METADATA, zone, &zone->vio_pool);
  2295. if (result != VDO_SUCCESS)
  2296. return result;
  2297. vdo_set_admin_state_code(&zone->state, VDO_ADMIN_STATE_NORMAL_OPERATION);
  2298. zone->page_cache.zone = zone;
  2299. zone->page_cache.vdo = vdo;
  2300. zone->page_cache.page_count = cache_size / map->zone_count;
  2301. zone->page_cache.stats.free_pages = zone->page_cache.page_count;
  2302. result = allocate_cache_components(&zone->page_cache);
  2303. if (result != VDO_SUCCESS)
  2304. return result;
  2305. /* initialize empty circular queues */
  2306. INIT_LIST_HEAD(&zone->page_cache.lru_list);
  2307. INIT_LIST_HEAD(&zone->page_cache.outgoing_list);
  2308. return VDO_SUCCESS;
  2309. }
  2310. /* Implements vdo_zone_thread_getter_fn */
  2311. static thread_id_t get_block_map_zone_thread_id(void *context, zone_count_t zone_number)
  2312. {
  2313. struct block_map *map = context;
  2314. return map->zones[zone_number].thread_id;
  2315. }
  2316. /* Implements vdo_action_preamble_fn */
  2317. static void prepare_for_era_advance(void *context, struct vdo_completion *parent)
  2318. {
  2319. struct block_map *map = context;
  2320. map->current_era_point = map->pending_era_point;
  2321. vdo_finish_completion(parent);
  2322. }
  2323. /* Implements vdo_zone_action_fn */
  2324. static void advance_block_map_zone_era(void *context, zone_count_t zone_number,
  2325. struct vdo_completion *parent)
  2326. {
  2327. struct block_map *map = context;
  2328. struct block_map_zone *zone = &map->zones[zone_number];
  2329. update_period(zone->dirty_lists, map->current_era_point);
  2330. write_expired_elements(zone);
  2331. vdo_finish_completion(parent);
  2332. }
  2333. /*
  2334. * Schedule an era advance if necessary. This method should not be called directly. Rather, call
  2335. * vdo_schedule_default_action() on the block map's action manager.
  2336. *
  2337. * Implements vdo_action_scheduler_fn.
  2338. */
  2339. static bool schedule_era_advance(void *context)
  2340. {
  2341. struct block_map *map = context;
  2342. if (map->current_era_point == map->pending_era_point)
  2343. return false;
  2344. return vdo_schedule_action(map->action_manager, prepare_for_era_advance,
  2345. advance_block_map_zone_era, NULL, NULL);
  2346. }
  2347. static void uninitialize_block_map_zone(struct block_map_zone *zone)
  2348. {
  2349. struct vdo_page_cache *cache = &zone->page_cache;
  2350. vdo_free(vdo_forget(zone->dirty_lists));
  2351. free_vio_pool(vdo_forget(zone->vio_pool));
  2352. vdo_int_map_free(vdo_forget(zone->loading_pages));
  2353. if (cache->infos != NULL) {
  2354. struct page_info *info;
  2355. for (info = cache->infos; info < cache->infos + cache->page_count; info++)
  2356. free_vio(vdo_forget(info->vio));
  2357. }
  2358. vdo_int_map_free(vdo_forget(cache->page_map));
  2359. vdo_free(vdo_forget(cache->infos));
  2360. vdo_free(vdo_forget(cache->pages));
  2361. }
  2362. void vdo_free_block_map(struct block_map *map)
  2363. {
  2364. zone_count_t zone;
  2365. if (map == NULL)
  2366. return;
  2367. for (zone = 0; zone < map->zone_count; zone++)
  2368. uninitialize_block_map_zone(&map->zones[zone]);
  2369. vdo_abandon_block_map_growth(map);
  2370. if (map->forest != NULL)
  2371. deforest(vdo_forget(map->forest), 0);
  2372. vdo_free(vdo_forget(map->action_manager));
  2373. vdo_free(map);
  2374. }
  2375. /* @journal may be NULL. */
  2376. int vdo_decode_block_map(struct block_map_state_2_0 state, block_count_t logical_blocks,
  2377. struct vdo *vdo, struct recovery_journal *journal,
  2378. nonce_t nonce, page_count_t cache_size, block_count_t maximum_age,
  2379. struct block_map **map_ptr)
  2380. {
  2381. struct block_map *map;
  2382. int result;
  2383. zone_count_t zone = 0;
  2384. BUILD_BUG_ON(VDO_BLOCK_MAP_ENTRIES_PER_PAGE !=
  2385. ((VDO_BLOCK_SIZE - sizeof(struct block_map_page)) /
  2386. sizeof(struct block_map_entry)));
  2387. result = VDO_ASSERT(cache_size > 0, "block map cache size is specified");
  2388. if (result != VDO_SUCCESS)
  2389. return result;
  2390. result = vdo_allocate_extended(struct block_map,
  2391. vdo->thread_config.logical_zone_count,
  2392. struct block_map_zone, __func__, &map);
  2393. if (result != VDO_SUCCESS)
  2394. return result;
  2395. map->vdo = vdo;
  2396. map->root_origin = state.root_origin;
  2397. map->root_count = state.root_count;
  2398. map->entry_count = logical_blocks;
  2399. map->journal = journal;
  2400. map->nonce = nonce;
  2401. result = make_forest(map, map->entry_count);
  2402. if (result != VDO_SUCCESS) {
  2403. vdo_free_block_map(map);
  2404. return result;
  2405. }
  2406. replace_forest(map);
  2407. map->zone_count = vdo->thread_config.logical_zone_count;
  2408. for (zone = 0; zone < map->zone_count; zone++) {
  2409. result = initialize_block_map_zone(map, zone, cache_size, maximum_age);
  2410. if (result != VDO_SUCCESS) {
  2411. vdo_free_block_map(map);
  2412. return result;
  2413. }
  2414. }
  2415. result = vdo_make_action_manager(map->zone_count, get_block_map_zone_thread_id,
  2416. vdo_get_recovery_journal_thread_id(journal),
  2417. map, schedule_era_advance, vdo,
  2418. &map->action_manager);
  2419. if (result != VDO_SUCCESS) {
  2420. vdo_free_block_map(map);
  2421. return result;
  2422. }
  2423. *map_ptr = map;
  2424. return VDO_SUCCESS;
  2425. }
  2426. struct block_map_state_2_0 vdo_record_block_map(const struct block_map *map)
  2427. {
  2428. return (struct block_map_state_2_0) {
  2429. .flat_page_origin = VDO_BLOCK_MAP_FLAT_PAGE_ORIGIN,
  2430. /* This is the flat page count, which has turned out to always be 0. */
  2431. .flat_page_count = 0,
  2432. .root_origin = map->root_origin,
  2433. .root_count = map->root_count,
  2434. };
  2435. }
  2436. /* The block map needs to know the journals' sequence number to initialize the eras. */
  2437. void vdo_initialize_block_map_from_journal(struct block_map *map,
  2438. struct recovery_journal *journal)
  2439. {
  2440. zone_count_t z = 0;
  2441. map->current_era_point = vdo_get_recovery_journal_current_sequence_number(journal);
  2442. map->pending_era_point = map->current_era_point;
  2443. for (z = 0; z < map->zone_count; z++) {
  2444. struct dirty_lists *dirty_lists = map->zones[z].dirty_lists;
  2445. VDO_ASSERT_LOG_ONLY(dirty_lists->next_period == 0, "current period not set");
  2446. dirty_lists->oldest_period = map->current_era_point;
  2447. dirty_lists->next_period = map->current_era_point + 1;
  2448. dirty_lists->offset = map->current_era_point % dirty_lists->maximum_age;
  2449. }
  2450. }
  2451. /* Compute the logical zone for the LBN of a data vio. */
  2452. zone_count_t vdo_compute_logical_zone(struct data_vio *data_vio)
  2453. {
  2454. struct block_map *map = vdo_from_data_vio(data_vio)->block_map;
  2455. struct tree_lock *tree_lock = &data_vio->tree_lock;
  2456. page_number_t page_number = data_vio->logical.lbn / VDO_BLOCK_MAP_ENTRIES_PER_PAGE;
  2457. tree_lock->tree_slots[0].page_index = page_number;
  2458. tree_lock->root_index = page_number % map->root_count;
  2459. return (tree_lock->root_index % map->zone_count);
  2460. }
  2461. void vdo_advance_block_map_era(struct block_map *map,
  2462. sequence_number_t recovery_block_number)
  2463. {
  2464. if (map == NULL)
  2465. return;
  2466. map->pending_era_point = recovery_block_number;
  2467. vdo_schedule_default_action(map->action_manager);
  2468. }
  2469. /* Implements vdo_admin_initiator_fn */
  2470. static void initiate_drain(struct admin_state *state)
  2471. {
  2472. struct block_map_zone *zone = container_of(state, struct block_map_zone, state);
  2473. VDO_ASSERT_LOG_ONLY((zone->active_lookups == 0),
  2474. "%s() called with no active lookups", __func__);
  2475. if (!vdo_is_state_suspending(state)) {
  2476. while (zone->dirty_lists->oldest_period < zone->dirty_lists->next_period)
  2477. expire_oldest_list(zone->dirty_lists);
  2478. write_expired_elements(zone);
  2479. }
  2480. check_for_drain_complete(zone);
  2481. }
  2482. /* Implements vdo_zone_action_fn. */
  2483. static void drain_zone(void *context, zone_count_t zone_number,
  2484. struct vdo_completion *parent)
  2485. {
  2486. struct block_map *map = context;
  2487. struct block_map_zone *zone = &map->zones[zone_number];
  2488. vdo_start_draining(&zone->state,
  2489. vdo_get_current_manager_operation(map->action_manager),
  2490. parent, initiate_drain);
  2491. }
  2492. void vdo_drain_block_map(struct block_map *map, const struct admin_state_code *operation,
  2493. struct vdo_completion *parent)
  2494. {
  2495. vdo_schedule_operation(map->action_manager, operation, NULL, drain_zone, NULL,
  2496. parent);
  2497. }
  2498. /* Implements vdo_zone_action_fn. */
  2499. static void resume_block_map_zone(void *context, zone_count_t zone_number,
  2500. struct vdo_completion *parent)
  2501. {
  2502. struct block_map *map = context;
  2503. struct block_map_zone *zone = &map->zones[zone_number];
  2504. vdo_fail_completion(parent, vdo_resume_if_quiescent(&zone->state));
  2505. }
  2506. void vdo_resume_block_map(struct block_map *map, struct vdo_completion *parent)
  2507. {
  2508. vdo_schedule_operation(map->action_manager, VDO_ADMIN_STATE_RESUMING,
  2509. NULL, resume_block_map_zone, NULL, parent);
  2510. }
  2511. /* Allocate an expanded collection of trees, for a future growth. */
  2512. int vdo_prepare_to_grow_block_map(struct block_map *map,
  2513. block_count_t new_logical_blocks)
  2514. {
  2515. if (map->next_entry_count == new_logical_blocks)
  2516. return VDO_SUCCESS;
  2517. if (map->next_entry_count > 0)
  2518. vdo_abandon_block_map_growth(map);
  2519. if (new_logical_blocks < map->entry_count) {
  2520. map->next_entry_count = map->entry_count;
  2521. return VDO_SUCCESS;
  2522. }
  2523. return make_forest(map, new_logical_blocks);
  2524. }
  2525. /* Implements vdo_action_preamble_fn */
  2526. static void grow_forest(void *context, struct vdo_completion *completion)
  2527. {
  2528. replace_forest(context);
  2529. vdo_finish_completion(completion);
  2530. }
  2531. /* Requires vdo_prepare_to_grow_block_map() to have been previously called. */
  2532. void vdo_grow_block_map(struct block_map *map, struct vdo_completion *parent)
  2533. {
  2534. vdo_schedule_operation(map->action_manager,
  2535. VDO_ADMIN_STATE_SUSPENDED_OPERATION,
  2536. grow_forest, NULL, NULL, parent);
  2537. }
  2538. void vdo_abandon_block_map_growth(struct block_map *map)
  2539. {
  2540. struct forest *forest = vdo_forget(map->next_forest);
  2541. if (forest != NULL)
  2542. deforest(forest, forest->segments - 1);
  2543. map->next_entry_count = 0;
  2544. }
  2545. /* Release the page completion and then continue the requester. */
  2546. static inline void finish_processing_page(struct vdo_completion *completion, int result)
  2547. {
  2548. struct vdo_completion *parent = completion->parent;
  2549. vdo_release_page_completion(completion);
  2550. vdo_continue_completion(parent, result);
  2551. }
  2552. static void handle_page_error(struct vdo_completion *completion)
  2553. {
  2554. finish_processing_page(completion, completion->result);
  2555. }
  2556. /* Fetch the mapping page for a block map update, and call the provided handler when fetched. */
  2557. static void fetch_mapping_page(struct data_vio *data_vio, bool modifiable,
  2558. vdo_action_fn action)
  2559. {
  2560. struct block_map_zone *zone = data_vio->logical.zone->block_map_zone;
  2561. if (vdo_is_state_draining(&zone->state)) {
  2562. continue_data_vio_with_error(data_vio, VDO_SHUTTING_DOWN);
  2563. return;
  2564. }
  2565. vdo_get_page(&data_vio->page_completion, zone,
  2566. data_vio->tree_lock.tree_slots[0].block_map_slot.pbn,
  2567. modifiable, &data_vio->vio.completion,
  2568. action, handle_page_error, false);
  2569. }
  2570. /**
  2571. * clear_mapped_location() - Clear a data_vio's mapped block location, setting it to be unmapped.
  2572. *
  2573. * This indicates the block map entry for the logical block is either unmapped or corrupted.
  2574. */
  2575. static void clear_mapped_location(struct data_vio *data_vio)
  2576. {
  2577. data_vio->mapped = (struct zoned_pbn) {
  2578. .state = VDO_MAPPING_STATE_UNMAPPED,
  2579. };
  2580. }
  2581. /**
  2582. * set_mapped_location() - Decode and validate a block map entry, and set the mapped location of a
  2583. * data_vio.
  2584. *
  2585. * Return: VDO_SUCCESS or VDO_BAD_MAPPING if the map entry is invalid or an error code for any
  2586. * other failure
  2587. */
  2588. static int __must_check set_mapped_location(struct data_vio *data_vio,
  2589. const struct block_map_entry *entry)
  2590. {
  2591. /* Unpack the PBN for logging purposes even if the entry is invalid. */
  2592. struct data_location mapped = vdo_unpack_block_map_entry(entry);
  2593. if (vdo_is_valid_location(&mapped)) {
  2594. int result;
  2595. result = vdo_get_physical_zone(vdo_from_data_vio(data_vio),
  2596. mapped.pbn, &data_vio->mapped.zone);
  2597. if (result == VDO_SUCCESS) {
  2598. data_vio->mapped.pbn = mapped.pbn;
  2599. data_vio->mapped.state = mapped.state;
  2600. return VDO_SUCCESS;
  2601. }
  2602. /*
  2603. * Return all errors not specifically known to be errors from validating the
  2604. * location.
  2605. */
  2606. if ((result != VDO_OUT_OF_RANGE) && (result != VDO_BAD_MAPPING))
  2607. return result;
  2608. }
  2609. /*
  2610. * Log the corruption even if we wind up ignoring it for write VIOs, converting all cases
  2611. * to VDO_BAD_MAPPING.
  2612. */
  2613. vdo_log_error_strerror(VDO_BAD_MAPPING,
  2614. "PBN %llu with state %u read from the block map was invalid",
  2615. (unsigned long long) mapped.pbn, mapped.state);
  2616. /*
  2617. * A read VIO has no option but to report the bad mapping--reading zeros would be hiding
  2618. * known data loss.
  2619. */
  2620. if (!data_vio->write)
  2621. return VDO_BAD_MAPPING;
  2622. /*
  2623. * A write VIO only reads this mapping to decref the old block. Treat this as an unmapped
  2624. * entry rather than fail the write.
  2625. */
  2626. clear_mapped_location(data_vio);
  2627. return VDO_SUCCESS;
  2628. }
  2629. /* This callback is registered in vdo_get_mapped_block(). */
  2630. static void get_mapping_from_fetched_page(struct vdo_completion *completion)
  2631. {
  2632. int result;
  2633. struct vdo_page_completion *vpc = as_vdo_page_completion(completion);
  2634. const struct block_map_page *page;
  2635. const struct block_map_entry *entry;
  2636. struct data_vio *data_vio = as_data_vio(completion->parent);
  2637. struct block_map_tree_slot *tree_slot;
  2638. if (completion->result != VDO_SUCCESS) {
  2639. finish_processing_page(completion, completion->result);
  2640. return;
  2641. }
  2642. result = validate_completed_page(vpc, false);
  2643. if (result != VDO_SUCCESS) {
  2644. finish_processing_page(completion, result);
  2645. return;
  2646. }
  2647. page = (const struct block_map_page *) get_page_buffer(vpc->info);
  2648. tree_slot = &data_vio->tree_lock.tree_slots[0];
  2649. entry = &page->entries[tree_slot->block_map_slot.slot];
  2650. result = set_mapped_location(data_vio, entry);
  2651. finish_processing_page(completion, result);
  2652. }
  2653. void vdo_update_block_map_page(struct block_map_page *page, struct data_vio *data_vio,
  2654. physical_block_number_t pbn,
  2655. enum block_mapping_state mapping_state,
  2656. sequence_number_t *recovery_lock)
  2657. {
  2658. struct block_map_zone *zone = data_vio->logical.zone->block_map_zone;
  2659. struct block_map *block_map = zone->block_map;
  2660. struct recovery_journal *journal = block_map->journal;
  2661. sequence_number_t old_locked, new_locked;
  2662. struct tree_lock *tree_lock = &data_vio->tree_lock;
  2663. /* Encode the new mapping. */
  2664. page->entries[tree_lock->tree_slots[tree_lock->height].block_map_slot.slot] =
  2665. vdo_pack_block_map_entry(pbn, mapping_state);
  2666. /* Adjust references on the recovery journal blocks. */
  2667. old_locked = *recovery_lock;
  2668. new_locked = data_vio->recovery_sequence_number;
  2669. if ((old_locked == 0) || (old_locked > new_locked)) {
  2670. vdo_acquire_recovery_journal_block_reference(journal, new_locked,
  2671. VDO_ZONE_TYPE_LOGICAL,
  2672. zone->zone_number);
  2673. if (old_locked > 0) {
  2674. vdo_release_recovery_journal_block_reference(journal, old_locked,
  2675. VDO_ZONE_TYPE_LOGICAL,
  2676. zone->zone_number);
  2677. }
  2678. *recovery_lock = new_locked;
  2679. }
  2680. /*
  2681. * FIXME: explain this more
  2682. * Release the transferred lock from the data_vio.
  2683. */
  2684. vdo_release_journal_entry_lock(journal, new_locked);
  2685. data_vio->recovery_sequence_number = 0;
  2686. }
  2687. static void put_mapping_in_fetched_page(struct vdo_completion *completion)
  2688. {
  2689. struct data_vio *data_vio = as_data_vio(completion->parent);
  2690. sequence_number_t old_lock;
  2691. struct vdo_page_completion *vpc;
  2692. struct page_info *info;
  2693. int result;
  2694. if (completion->result != VDO_SUCCESS) {
  2695. finish_processing_page(completion, completion->result);
  2696. return;
  2697. }
  2698. vpc = as_vdo_page_completion(completion);
  2699. result = validate_completed_page(vpc, true);
  2700. if (result != VDO_SUCCESS) {
  2701. finish_processing_page(completion, result);
  2702. return;
  2703. }
  2704. info = vpc->info;
  2705. old_lock = info->recovery_lock;
  2706. vdo_update_block_map_page((struct block_map_page *) get_page_buffer(info),
  2707. data_vio, data_vio->new_mapped.pbn,
  2708. data_vio->new_mapped.state, &info->recovery_lock);
  2709. set_info_state(info, PS_DIRTY);
  2710. add_to_dirty_lists(info->cache->zone, &info->state_entry,
  2711. VDO_CACHE_PAGE, old_lock, info->recovery_lock);
  2712. finish_processing_page(completion, VDO_SUCCESS);
  2713. }
  2714. /* Read a stored block mapping into a data_vio. */
  2715. void vdo_get_mapped_block(struct data_vio *data_vio)
  2716. {
  2717. if (data_vio->tree_lock.tree_slots[0].block_map_slot.pbn == VDO_ZERO_BLOCK) {
  2718. /*
  2719. * We know that the block map page for this LBN has not been allocated, so the
  2720. * block must be unmapped.
  2721. */
  2722. clear_mapped_location(data_vio);
  2723. continue_data_vio(data_vio);
  2724. return;
  2725. }
  2726. fetch_mapping_page(data_vio, false, get_mapping_from_fetched_page);
  2727. }
  2728. /* Update a stored block mapping to reflect a data_vio's new mapping. */
  2729. void vdo_put_mapped_block(struct data_vio *data_vio)
  2730. {
  2731. fetch_mapping_page(data_vio, true, put_mapping_in_fetched_page);
  2732. }
  2733. struct block_map_statistics vdo_get_block_map_statistics(struct block_map *map)
  2734. {
  2735. zone_count_t zone = 0;
  2736. struct block_map_statistics totals;
  2737. memset(&totals, 0, sizeof(struct block_map_statistics));
  2738. for (zone = 0; zone < map->zone_count; zone++) {
  2739. const struct block_map_statistics *stats =
  2740. &(map->zones[zone].page_cache.stats);
  2741. totals.dirty_pages += READ_ONCE(stats->dirty_pages);
  2742. totals.clean_pages += READ_ONCE(stats->clean_pages);
  2743. totals.free_pages += READ_ONCE(stats->free_pages);
  2744. totals.failed_pages += READ_ONCE(stats->failed_pages);
  2745. totals.incoming_pages += READ_ONCE(stats->incoming_pages);
  2746. totals.outgoing_pages += READ_ONCE(stats->outgoing_pages);
  2747. totals.cache_pressure += READ_ONCE(stats->cache_pressure);
  2748. totals.read_count += READ_ONCE(stats->read_count);
  2749. totals.write_count += READ_ONCE(stats->write_count);
  2750. totals.failed_reads += READ_ONCE(stats->failed_reads);
  2751. totals.failed_writes += READ_ONCE(stats->failed_writes);
  2752. totals.reclaimed += READ_ONCE(stats->reclaimed);
  2753. totals.read_outgoing += READ_ONCE(stats->read_outgoing);
  2754. totals.found_in_cache += READ_ONCE(stats->found_in_cache);
  2755. totals.discard_required += READ_ONCE(stats->discard_required);
  2756. totals.wait_for_page += READ_ONCE(stats->wait_for_page);
  2757. totals.fetch_required += READ_ONCE(stats->fetch_required);
  2758. totals.pages_loaded += READ_ONCE(stats->pages_loaded);
  2759. totals.pages_saved += READ_ONCE(stats->pages_saved);
  2760. totals.flush_count += READ_ONCE(stats->flush_count);
  2761. }
  2762. return totals;
  2763. }