dm-vdo-target.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #include <linux/atomic.h>
  6. #include <linux/bitops.h>
  7. #include <linux/completion.h>
  8. #include <linux/delay.h>
  9. #include <linux/device-mapper.h>
  10. #include <linux/err.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/spinlock.h>
  14. #include "admin-state.h"
  15. #include "block-map.h"
  16. #include "completion.h"
  17. #include "constants.h"
  18. #include "data-vio.h"
  19. #include "dedupe.h"
  20. #include "dump.h"
  21. #include "encodings.h"
  22. #include "errors.h"
  23. #include "flush.h"
  24. #include "io-submitter.h"
  25. #include "logger.h"
  26. #include "memory-alloc.h"
  27. #include "message-stats.h"
  28. #include "recovery-journal.h"
  29. #include "repair.h"
  30. #include "slab-depot.h"
  31. #include "status-codes.h"
  32. #include "string-utils.h"
  33. #include "thread-device.h"
  34. #include "thread-registry.h"
  35. #include "thread-utils.h"
  36. #include "types.h"
  37. #include "vdo.h"
  38. #include "vio.h"
  39. enum admin_phases {
  40. GROW_LOGICAL_PHASE_START,
  41. GROW_LOGICAL_PHASE_GROW_BLOCK_MAP,
  42. GROW_LOGICAL_PHASE_END,
  43. GROW_LOGICAL_PHASE_ERROR,
  44. GROW_PHYSICAL_PHASE_START,
  45. GROW_PHYSICAL_PHASE_COPY_SUMMARY,
  46. GROW_PHYSICAL_PHASE_UPDATE_COMPONENTS,
  47. GROW_PHYSICAL_PHASE_USE_NEW_SLABS,
  48. GROW_PHYSICAL_PHASE_END,
  49. GROW_PHYSICAL_PHASE_ERROR,
  50. LOAD_PHASE_START,
  51. LOAD_PHASE_LOAD_DEPOT,
  52. LOAD_PHASE_MAKE_DIRTY,
  53. LOAD_PHASE_PREPARE_TO_ALLOCATE,
  54. LOAD_PHASE_SCRUB_SLABS,
  55. LOAD_PHASE_DATA_REDUCTION,
  56. LOAD_PHASE_FINISHED,
  57. LOAD_PHASE_DRAIN_JOURNAL,
  58. LOAD_PHASE_WAIT_FOR_READ_ONLY,
  59. PRE_LOAD_PHASE_START,
  60. PRE_LOAD_PHASE_LOAD_COMPONENTS,
  61. PRE_LOAD_PHASE_END,
  62. PREPARE_GROW_PHYSICAL_PHASE_START,
  63. RESUME_PHASE_START,
  64. RESUME_PHASE_ALLOW_READ_ONLY_MODE,
  65. RESUME_PHASE_DEDUPE,
  66. RESUME_PHASE_DEPOT,
  67. RESUME_PHASE_JOURNAL,
  68. RESUME_PHASE_BLOCK_MAP,
  69. RESUME_PHASE_LOGICAL_ZONES,
  70. RESUME_PHASE_PACKER,
  71. RESUME_PHASE_FLUSHER,
  72. RESUME_PHASE_DATA_VIOS,
  73. RESUME_PHASE_END,
  74. SUSPEND_PHASE_START,
  75. SUSPEND_PHASE_PACKER,
  76. SUSPEND_PHASE_DATA_VIOS,
  77. SUSPEND_PHASE_DEDUPE,
  78. SUSPEND_PHASE_FLUSHES,
  79. SUSPEND_PHASE_LOGICAL_ZONES,
  80. SUSPEND_PHASE_BLOCK_MAP,
  81. SUSPEND_PHASE_JOURNAL,
  82. SUSPEND_PHASE_DEPOT,
  83. SUSPEND_PHASE_READ_ONLY_WAIT,
  84. SUSPEND_PHASE_WRITE_SUPER_BLOCK,
  85. SUSPEND_PHASE_END,
  86. };
  87. static const char * const ADMIN_PHASE_NAMES[] = {
  88. "GROW_LOGICAL_PHASE_START",
  89. "GROW_LOGICAL_PHASE_GROW_BLOCK_MAP",
  90. "GROW_LOGICAL_PHASE_END",
  91. "GROW_LOGICAL_PHASE_ERROR",
  92. "GROW_PHYSICAL_PHASE_START",
  93. "GROW_PHYSICAL_PHASE_COPY_SUMMARY",
  94. "GROW_PHYSICAL_PHASE_UPDATE_COMPONENTS",
  95. "GROW_PHYSICAL_PHASE_USE_NEW_SLABS",
  96. "GROW_PHYSICAL_PHASE_END",
  97. "GROW_PHYSICAL_PHASE_ERROR",
  98. "LOAD_PHASE_START",
  99. "LOAD_PHASE_LOAD_DEPOT",
  100. "LOAD_PHASE_MAKE_DIRTY",
  101. "LOAD_PHASE_PREPARE_TO_ALLOCATE",
  102. "LOAD_PHASE_SCRUB_SLABS",
  103. "LOAD_PHASE_DATA_REDUCTION",
  104. "LOAD_PHASE_FINISHED",
  105. "LOAD_PHASE_DRAIN_JOURNAL",
  106. "LOAD_PHASE_WAIT_FOR_READ_ONLY",
  107. "PRE_LOAD_PHASE_START",
  108. "PRE_LOAD_PHASE_LOAD_COMPONENTS",
  109. "PRE_LOAD_PHASE_END",
  110. "PREPARE_GROW_PHYSICAL_PHASE_START",
  111. "RESUME_PHASE_START",
  112. "RESUME_PHASE_ALLOW_READ_ONLY_MODE",
  113. "RESUME_PHASE_DEDUPE",
  114. "RESUME_PHASE_DEPOT",
  115. "RESUME_PHASE_JOURNAL",
  116. "RESUME_PHASE_BLOCK_MAP",
  117. "RESUME_PHASE_LOGICAL_ZONES",
  118. "RESUME_PHASE_PACKER",
  119. "RESUME_PHASE_FLUSHER",
  120. "RESUME_PHASE_DATA_VIOS",
  121. "RESUME_PHASE_END",
  122. "SUSPEND_PHASE_START",
  123. "SUSPEND_PHASE_PACKER",
  124. "SUSPEND_PHASE_DATA_VIOS",
  125. "SUSPEND_PHASE_DEDUPE",
  126. "SUSPEND_PHASE_FLUSHES",
  127. "SUSPEND_PHASE_LOGICAL_ZONES",
  128. "SUSPEND_PHASE_BLOCK_MAP",
  129. "SUSPEND_PHASE_JOURNAL",
  130. "SUSPEND_PHASE_DEPOT",
  131. "SUSPEND_PHASE_READ_ONLY_WAIT",
  132. "SUSPEND_PHASE_WRITE_SUPER_BLOCK",
  133. "SUSPEND_PHASE_END",
  134. };
  135. /* If we bump this, update the arrays below */
  136. #define TABLE_VERSION 4
  137. /* arrays for handling different table versions */
  138. static const u8 REQUIRED_ARGC[] = { 10, 12, 9, 7, 6 };
  139. /* pool name no longer used. only here for verification of older versions */
  140. static const u8 POOL_NAME_ARG_INDEX[] = { 8, 10, 8 };
  141. /*
  142. * Track in-use instance numbers using a flat bit array.
  143. *
  144. * O(n) run time isn't ideal, but if we have 1000 VDO devices in use simultaneously we still only
  145. * need to scan 16 words, so it's not likely to be a big deal compared to other resource usage.
  146. */
  147. /*
  148. * This minimum size for the bit array creates a numbering space of 0-999, which allows
  149. * successive starts of the same volume to have different instance numbers in any
  150. * reasonably-sized test. Changing instances on restart allows vdoMonReport to detect that
  151. * the ephemeral stats have reset to zero.
  152. */
  153. #define BIT_COUNT_MINIMUM 1000
  154. /* Grow the bit array by this many bits when needed */
  155. #define BIT_COUNT_INCREMENT 100
  156. struct instance_tracker {
  157. unsigned int bit_count;
  158. unsigned long *words;
  159. unsigned int count;
  160. unsigned int next;
  161. };
  162. static DEFINE_MUTEX(instances_lock);
  163. static struct instance_tracker instances;
  164. /**
  165. * free_device_config() - Free a device config created by parse_device_config().
  166. * @config: The config to free.
  167. */
  168. static void free_device_config(struct device_config *config)
  169. {
  170. if (config == NULL)
  171. return;
  172. if (config->owned_device != NULL)
  173. dm_put_device(config->owning_target, config->owned_device);
  174. vdo_free(config->parent_device_name);
  175. vdo_free(config->original_string);
  176. /* Reduce the chance a use-after-free (as in BZ 1669960) happens to work. */
  177. memset(config, 0, sizeof(*config));
  178. vdo_free(config);
  179. }
  180. /**
  181. * get_version_number() - Decide the version number from argv.
  182. *
  183. * @argc: The number of table values.
  184. * @argv: The array of table values.
  185. * @error_ptr: A pointer to return a error string in.
  186. * @version_ptr: A pointer to return the version.
  187. *
  188. * Return: VDO_SUCCESS or an error code.
  189. */
  190. static int get_version_number(int argc, char **argv, char **error_ptr,
  191. unsigned int *version_ptr)
  192. {
  193. /* version, if it exists, is in a form of V<n> */
  194. if (sscanf(argv[0], "V%u", version_ptr) == 1) {
  195. if (*version_ptr < 1 || *version_ptr > TABLE_VERSION) {
  196. *error_ptr = "Unknown version number detected";
  197. return VDO_BAD_CONFIGURATION;
  198. }
  199. } else {
  200. /* V0 actually has no version number in the table string */
  201. *version_ptr = 0;
  202. }
  203. /*
  204. * V0 and V1 have no optional parameters. There will always be a parameter for thread
  205. * config, even if it's a "." to show it's an empty list.
  206. */
  207. if (*version_ptr <= 1) {
  208. if (argc != REQUIRED_ARGC[*version_ptr]) {
  209. *error_ptr = "Incorrect number of arguments for version";
  210. return VDO_BAD_CONFIGURATION;
  211. }
  212. } else if (argc < REQUIRED_ARGC[*version_ptr]) {
  213. *error_ptr = "Incorrect number of arguments for version";
  214. return VDO_BAD_CONFIGURATION;
  215. }
  216. if (*version_ptr != TABLE_VERSION) {
  217. vdo_log_warning("Detected version mismatch between kernel module and tools kernel: %d, tool: %d",
  218. TABLE_VERSION, *version_ptr);
  219. vdo_log_warning("Please consider upgrading management tools to match kernel.");
  220. }
  221. return VDO_SUCCESS;
  222. }
  223. /* Free a list of non-NULL string pointers, and then the list itself. */
  224. static void free_string_array(char **string_array)
  225. {
  226. unsigned int offset;
  227. for (offset = 0; string_array[offset] != NULL; offset++)
  228. vdo_free(string_array[offset]);
  229. vdo_free(string_array);
  230. }
  231. /*
  232. * Split the input string into substrings, separated at occurrences of the indicated character,
  233. * returning a null-terminated list of string pointers.
  234. *
  235. * The string pointers and the pointer array itself should both be freed with vdo_free() when no
  236. * longer needed. This can be done with vdo_free_string_array (below) if the pointers in the array
  237. * are not changed. Since the array and copied strings are allocated by this function, it may only
  238. * be used in contexts where allocation is permitted.
  239. *
  240. * Empty substrings are not ignored; that is, returned substrings may be empty strings if the
  241. * separator occurs twice in a row.
  242. */
  243. static int split_string(const char *string, char separator, char ***substring_array_ptr)
  244. {
  245. unsigned int current_substring = 0, substring_count = 1;
  246. const char *s;
  247. char **substrings;
  248. int result;
  249. ptrdiff_t length;
  250. for (s = string; *s != 0; s++) {
  251. if (*s == separator)
  252. substring_count++;
  253. }
  254. result = vdo_allocate(substring_count + 1, char *, "string-splitting array",
  255. &substrings);
  256. if (result != VDO_SUCCESS)
  257. return result;
  258. for (s = string; *s != 0; s++) {
  259. if (*s == separator) {
  260. ptrdiff_t length = s - string;
  261. result = vdo_allocate(length + 1, char, "split string",
  262. &substrings[current_substring]);
  263. if (result != VDO_SUCCESS) {
  264. free_string_array(substrings);
  265. return result;
  266. }
  267. /*
  268. * Trailing NUL is already in place after allocation; deal with the zero or
  269. * more non-NUL bytes in the string.
  270. */
  271. if (length > 0)
  272. memcpy(substrings[current_substring], string, length);
  273. string = s + 1;
  274. current_substring++;
  275. BUG_ON(current_substring >= substring_count);
  276. }
  277. }
  278. /* Process final string, with no trailing separator. */
  279. BUG_ON(current_substring != (substring_count - 1));
  280. length = strlen(string);
  281. result = vdo_allocate(length + 1, char, "split string",
  282. &substrings[current_substring]);
  283. if (result != VDO_SUCCESS) {
  284. free_string_array(substrings);
  285. return result;
  286. }
  287. memcpy(substrings[current_substring], string, length);
  288. current_substring++;
  289. /* substrings[current_substring] is NULL already */
  290. *substring_array_ptr = substrings;
  291. return VDO_SUCCESS;
  292. }
  293. /*
  294. * Join the input substrings into one string, joined with the indicated character, returning a
  295. * string. array_length is a bound on the number of valid elements in substring_array, in case it
  296. * is not NULL-terminated.
  297. */
  298. static int join_strings(char **substring_array, size_t array_length, char separator,
  299. char **string_ptr)
  300. {
  301. size_t string_length = 0;
  302. size_t i;
  303. int result;
  304. char *output, *current_position;
  305. for (i = 0; (i < array_length) && (substring_array[i] != NULL); i++)
  306. string_length += strlen(substring_array[i]) + 1;
  307. result = vdo_allocate(string_length, char, __func__, &output);
  308. if (result != VDO_SUCCESS)
  309. return result;
  310. current_position = &output[0];
  311. for (i = 0; (i < array_length) && (substring_array[i] != NULL); i++) {
  312. current_position = vdo_append_to_buffer(current_position,
  313. output + string_length, "%s",
  314. substring_array[i]);
  315. *current_position = separator;
  316. current_position++;
  317. }
  318. /* We output one too many separators; replace the last with a zero byte. */
  319. if (current_position != output)
  320. *(current_position - 1) = '\0';
  321. *string_ptr = output;
  322. return VDO_SUCCESS;
  323. }
  324. /**
  325. * parse_bool() - Parse a two-valued option into a bool.
  326. * @bool_str: The string value to convert to a bool.
  327. * @true_str: The string value which should be converted to true.
  328. * @false_str: The string value which should be converted to false.
  329. * @bool_ptr: A pointer to return the bool value in.
  330. *
  331. * Return: VDO_SUCCESS or an error if bool_str is neither true_str nor false_str.
  332. */
  333. static inline int __must_check parse_bool(const char *bool_str, const char *true_str,
  334. const char *false_str, bool *bool_ptr)
  335. {
  336. bool value = false;
  337. if (strcmp(bool_str, true_str) == 0)
  338. value = true;
  339. else if (strcmp(bool_str, false_str) == 0)
  340. value = false;
  341. else
  342. return VDO_BAD_CONFIGURATION;
  343. *bool_ptr = value;
  344. return VDO_SUCCESS;
  345. }
  346. /**
  347. * process_one_thread_config_spec() - Process one component of a thread parameter configuration
  348. * string and update the configuration data structure.
  349. * @thread_param_type: The type of thread specified.
  350. * @count: The thread count requested.
  351. * @config: The configuration data structure to update.
  352. *
  353. * If the thread count requested is invalid, a message is logged and -EINVAL returned. If the
  354. * thread name is unknown, a message is logged but no error is returned.
  355. *
  356. * Return: VDO_SUCCESS or -EINVAL
  357. */
  358. static int process_one_thread_config_spec(const char *thread_param_type,
  359. unsigned int count,
  360. struct thread_count_config *config)
  361. {
  362. /* Handle limited thread parameters */
  363. if (strcmp(thread_param_type, "bioRotationInterval") == 0) {
  364. if (count == 0) {
  365. vdo_log_error("thread config string error: 'bioRotationInterval' of at least 1 is required");
  366. return -EINVAL;
  367. } else if (count > VDO_BIO_ROTATION_INTERVAL_LIMIT) {
  368. vdo_log_error("thread config string error: 'bioRotationInterval' cannot be higher than %d",
  369. VDO_BIO_ROTATION_INTERVAL_LIMIT);
  370. return -EINVAL;
  371. }
  372. config->bio_rotation_interval = count;
  373. return VDO_SUCCESS;
  374. }
  375. if (strcmp(thread_param_type, "logical") == 0) {
  376. if (count > MAX_VDO_LOGICAL_ZONES) {
  377. vdo_log_error("thread config string error: at most %d 'logical' threads are allowed",
  378. MAX_VDO_LOGICAL_ZONES);
  379. return -EINVAL;
  380. }
  381. config->logical_zones = count;
  382. return VDO_SUCCESS;
  383. }
  384. if (strcmp(thread_param_type, "physical") == 0) {
  385. if (count > MAX_VDO_PHYSICAL_ZONES) {
  386. vdo_log_error("thread config string error: at most %d 'physical' threads are allowed",
  387. MAX_VDO_PHYSICAL_ZONES);
  388. return -EINVAL;
  389. }
  390. config->physical_zones = count;
  391. return VDO_SUCCESS;
  392. }
  393. /* Handle other thread count parameters */
  394. if (count > MAXIMUM_VDO_THREADS) {
  395. vdo_log_error("thread config string error: at most %d '%s' threads are allowed",
  396. MAXIMUM_VDO_THREADS, thread_param_type);
  397. return -EINVAL;
  398. }
  399. if (strcmp(thread_param_type, "hash") == 0) {
  400. config->hash_zones = count;
  401. return VDO_SUCCESS;
  402. }
  403. if (strcmp(thread_param_type, "cpu") == 0) {
  404. if (count == 0) {
  405. vdo_log_error("thread config string error: at least one 'cpu' thread required");
  406. return -EINVAL;
  407. }
  408. config->cpu_threads = count;
  409. return VDO_SUCCESS;
  410. }
  411. if (strcmp(thread_param_type, "ack") == 0) {
  412. config->bio_ack_threads = count;
  413. return VDO_SUCCESS;
  414. }
  415. if (strcmp(thread_param_type, "bio") == 0) {
  416. if (count == 0) {
  417. vdo_log_error("thread config string error: at least one 'bio' thread required");
  418. return -EINVAL;
  419. }
  420. config->bio_threads = count;
  421. return VDO_SUCCESS;
  422. }
  423. /*
  424. * Don't fail, just log. This will handle version mismatches between user mode tools and
  425. * kernel.
  426. */
  427. vdo_log_info("unknown thread parameter type \"%s\"", thread_param_type);
  428. return VDO_SUCCESS;
  429. }
  430. /**
  431. * parse_one_thread_config_spec() - Parse one component of a thread parameter configuration string
  432. * and update the configuration data structure.
  433. * @spec: The thread parameter specification string.
  434. * @config: The configuration data to be updated.
  435. */
  436. static int parse_one_thread_config_spec(const char *spec,
  437. struct thread_count_config *config)
  438. {
  439. unsigned int count;
  440. char **fields;
  441. int result;
  442. result = split_string(spec, '=', &fields);
  443. if (result != VDO_SUCCESS)
  444. return result;
  445. if ((fields[0] == NULL) || (fields[1] == NULL) || (fields[2] != NULL)) {
  446. vdo_log_error("thread config string error: expected thread parameter assignment, saw \"%s\"",
  447. spec);
  448. free_string_array(fields);
  449. return -EINVAL;
  450. }
  451. result = kstrtouint(fields[1], 10, &count);
  452. if (result) {
  453. vdo_log_error("thread config string error: integer value needed, found \"%s\"",
  454. fields[1]);
  455. free_string_array(fields);
  456. return result;
  457. }
  458. result = process_one_thread_config_spec(fields[0], count, config);
  459. free_string_array(fields);
  460. return result;
  461. }
  462. /**
  463. * parse_thread_config_string() - Parse the configuration string passed and update the specified
  464. * counts and other parameters of various types of threads to be
  465. * created.
  466. * @string: Thread parameter configuration string.
  467. * @config: The thread configuration data to update.
  468. *
  469. * The configuration string should contain one or more comma-separated specs of the form
  470. * "typename=number"; the supported type names are "cpu", "ack", "bio", "bioRotationInterval",
  471. * "logical", "physical", and "hash".
  472. *
  473. * If an error occurs during parsing of a single key/value pair, we deem it serious enough to stop
  474. * further parsing.
  475. *
  476. * This function can't set the "reason" value the caller wants to pass back, because we'd want to
  477. * format it to say which field was invalid, and we can't allocate the "reason" strings
  478. * dynamically. So if an error occurs, we'll log the details and pass back an error.
  479. *
  480. * Return: VDO_SUCCESS or -EINVAL or -ENOMEM
  481. */
  482. static int parse_thread_config_string(const char *string,
  483. struct thread_count_config *config)
  484. {
  485. int result = VDO_SUCCESS;
  486. char **specs;
  487. if (strcmp(".", string) != 0) {
  488. unsigned int i;
  489. result = split_string(string, ',', &specs);
  490. if (result != VDO_SUCCESS)
  491. return result;
  492. for (i = 0; specs[i] != NULL; i++) {
  493. result = parse_one_thread_config_spec(specs[i], config);
  494. if (result != VDO_SUCCESS)
  495. break;
  496. }
  497. free_string_array(specs);
  498. }
  499. return result;
  500. }
  501. /**
  502. * process_one_key_value_pair() - Process one component of an optional parameter string and update
  503. * the configuration data structure.
  504. * @key: The optional parameter key name.
  505. * @value: The optional parameter value.
  506. * @config: The configuration data structure to update.
  507. *
  508. * If the value requested is invalid, a message is logged and -EINVAL returned. If the key is
  509. * unknown, a message is logged but no error is returned.
  510. *
  511. * Return: VDO_SUCCESS or -EINVAL
  512. */
  513. static int process_one_key_value_pair(const char *key, unsigned int value,
  514. struct device_config *config)
  515. {
  516. /* Non thread optional parameters */
  517. if (strcmp(key, "maxDiscard") == 0) {
  518. if (value == 0) {
  519. vdo_log_error("optional parameter error: at least one max discard block required");
  520. return -EINVAL;
  521. }
  522. /* Max discard sectors in blkdev_issue_discard is UINT_MAX >> 9 */
  523. if (value > (UINT_MAX / VDO_BLOCK_SIZE)) {
  524. vdo_log_error("optional parameter error: at most %d max discard blocks are allowed",
  525. UINT_MAX / VDO_BLOCK_SIZE);
  526. return -EINVAL;
  527. }
  528. config->max_discard_blocks = value;
  529. return VDO_SUCCESS;
  530. }
  531. /* Handles unknown key names */
  532. return process_one_thread_config_spec(key, value, &config->thread_counts);
  533. }
  534. /**
  535. * parse_one_key_value_pair() - Parse one key/value pair and update the configuration data
  536. * structure.
  537. * @key: The optional key name.
  538. * @value: The optional value.
  539. * @config: The configuration data to be updated.
  540. *
  541. * Return: VDO_SUCCESS or error.
  542. */
  543. static int parse_one_key_value_pair(const char *key, const char *value,
  544. struct device_config *config)
  545. {
  546. unsigned int count;
  547. int result;
  548. if (strcmp(key, "deduplication") == 0)
  549. return parse_bool(value, "on", "off", &config->deduplication);
  550. if (strcmp(key, "compression") == 0)
  551. return parse_bool(value, "on", "off", &config->compression);
  552. /* The remaining arguments must have integral values. */
  553. result = kstrtouint(value, 10, &count);
  554. if (result) {
  555. vdo_log_error("optional config string error: integer value needed, found \"%s\"",
  556. value);
  557. return result;
  558. }
  559. return process_one_key_value_pair(key, count, config);
  560. }
  561. /**
  562. * parse_key_value_pairs() - Parse all key/value pairs from a list of arguments.
  563. * @argc: The total number of arguments in list.
  564. * @argv: The list of key/value pairs.
  565. * @config: The device configuration data to update.
  566. *
  567. * If an error occurs during parsing of a single key/value pair, we deem it serious enough to stop
  568. * further parsing.
  569. *
  570. * This function can't set the "reason" value the caller wants to pass back, because we'd want to
  571. * format it to say which field was invalid, and we can't allocate the "reason" strings
  572. * dynamically. So if an error occurs, we'll log the details and return the error.
  573. *
  574. * Return: VDO_SUCCESS or error
  575. */
  576. static int parse_key_value_pairs(int argc, char **argv, struct device_config *config)
  577. {
  578. int result = VDO_SUCCESS;
  579. while (argc) {
  580. result = parse_one_key_value_pair(argv[0], argv[1], config);
  581. if (result != VDO_SUCCESS)
  582. break;
  583. argc -= 2;
  584. argv += 2;
  585. }
  586. return result;
  587. }
  588. /**
  589. * parse_optional_arguments() - Parse the configuration string passed in for optional arguments.
  590. * @arg_set: The structure holding the arguments to parse.
  591. * @error_ptr: Pointer to a buffer to hold the error string.
  592. * @config: Pointer to device configuration data to update.
  593. *
  594. * For V0/V1 configurations, there will only be one optional parameter; the thread configuration.
  595. * The configuration string should contain one or more comma-separated specs of the form
  596. * "typename=number"; the supported type names are "cpu", "ack", "bio", "bioRotationInterval",
  597. * "logical", "physical", and "hash".
  598. *
  599. * For V2 configurations and beyond, there could be any number of arguments. They should contain
  600. * one or more key/value pairs separated by a space.
  601. *
  602. * Return: VDO_SUCCESS or error
  603. */
  604. static int parse_optional_arguments(struct dm_arg_set *arg_set, char **error_ptr,
  605. struct device_config *config)
  606. {
  607. int result = VDO_SUCCESS;
  608. if (config->version == 0 || config->version == 1) {
  609. result = parse_thread_config_string(arg_set->argv[0],
  610. &config->thread_counts);
  611. if (result != VDO_SUCCESS) {
  612. *error_ptr = "Invalid thread-count configuration";
  613. return VDO_BAD_CONFIGURATION;
  614. }
  615. } else {
  616. if ((arg_set->argc % 2) != 0) {
  617. *error_ptr = "Odd number of optional arguments given but they should be <key> <value> pairs";
  618. return VDO_BAD_CONFIGURATION;
  619. }
  620. result = parse_key_value_pairs(arg_set->argc, arg_set->argv, config);
  621. if (result != VDO_SUCCESS) {
  622. *error_ptr = "Invalid optional argument configuration";
  623. return VDO_BAD_CONFIGURATION;
  624. }
  625. }
  626. return result;
  627. }
  628. /**
  629. * handle_parse_error() - Handle a parsing error.
  630. * @config: The config to free.
  631. * @error_ptr: A place to store a constant string about the error.
  632. * @error_str: A constant string to store in error_ptr.
  633. */
  634. static void handle_parse_error(struct device_config *config, char **error_ptr,
  635. char *error_str)
  636. {
  637. free_device_config(config);
  638. *error_ptr = error_str;
  639. }
  640. /**
  641. * parse_device_config() - Convert the dmsetup table into a struct device_config.
  642. * @argc: The number of table values.
  643. * @argv: The array of table values.
  644. * @ti: The target structure for this table.
  645. * @config_ptr: A pointer to return the allocated config.
  646. *
  647. * Return: VDO_SUCCESS or an error code.
  648. */
  649. static int parse_device_config(int argc, char **argv, struct dm_target *ti,
  650. struct device_config **config_ptr)
  651. {
  652. bool enable_512e;
  653. size_t logical_bytes = to_bytes(ti->len);
  654. struct dm_arg_set arg_set;
  655. char **error_ptr = &ti->error;
  656. struct device_config *config = NULL;
  657. int result;
  658. if ((logical_bytes % VDO_BLOCK_SIZE) != 0) {
  659. handle_parse_error(config, error_ptr,
  660. "Logical size must be a multiple of 4096");
  661. return VDO_BAD_CONFIGURATION;
  662. }
  663. if (argc == 0) {
  664. handle_parse_error(config, error_ptr, "Incorrect number of arguments");
  665. return VDO_BAD_CONFIGURATION;
  666. }
  667. result = vdo_allocate(1, struct device_config, "device_config", &config);
  668. if (result != VDO_SUCCESS) {
  669. handle_parse_error(config, error_ptr,
  670. "Could not allocate config structure");
  671. return VDO_BAD_CONFIGURATION;
  672. }
  673. config->owning_target = ti;
  674. config->logical_blocks = logical_bytes / VDO_BLOCK_SIZE;
  675. INIT_LIST_HEAD(&config->config_list);
  676. /* Save the original string. */
  677. result = join_strings(argv, argc, ' ', &config->original_string);
  678. if (result != VDO_SUCCESS) {
  679. handle_parse_error(config, error_ptr, "Could not populate string");
  680. return VDO_BAD_CONFIGURATION;
  681. }
  682. vdo_log_info("table line: %s", config->original_string);
  683. config->thread_counts = (struct thread_count_config) {
  684. .bio_ack_threads = 1,
  685. .bio_threads = DEFAULT_VDO_BIO_SUBMIT_QUEUE_COUNT,
  686. .bio_rotation_interval = DEFAULT_VDO_BIO_SUBMIT_QUEUE_ROTATE_INTERVAL,
  687. .cpu_threads = 1,
  688. .logical_zones = 0,
  689. .physical_zones = 0,
  690. .hash_zones = 0,
  691. };
  692. config->max_discard_blocks = 1;
  693. config->deduplication = true;
  694. config->compression = false;
  695. arg_set.argc = argc;
  696. arg_set.argv = argv;
  697. result = get_version_number(argc, argv, error_ptr, &config->version);
  698. if (result != VDO_SUCCESS) {
  699. /* get_version_number sets error_ptr itself. */
  700. handle_parse_error(config, error_ptr, *error_ptr);
  701. return result;
  702. }
  703. /* Move the arg pointer forward only if the argument was there. */
  704. if (config->version >= 1)
  705. dm_shift_arg(&arg_set);
  706. result = vdo_duplicate_string(dm_shift_arg(&arg_set), "parent device name",
  707. &config->parent_device_name);
  708. if (result != VDO_SUCCESS) {
  709. handle_parse_error(config, error_ptr,
  710. "Could not copy parent device name");
  711. return VDO_BAD_CONFIGURATION;
  712. }
  713. /* Get the physical blocks, if known. */
  714. if (config->version >= 1) {
  715. result = kstrtoull(dm_shift_arg(&arg_set), 10, &config->physical_blocks);
  716. if (result != VDO_SUCCESS) {
  717. handle_parse_error(config, error_ptr,
  718. "Invalid physical block count");
  719. return VDO_BAD_CONFIGURATION;
  720. }
  721. }
  722. /* Get the logical block size and validate */
  723. result = parse_bool(dm_shift_arg(&arg_set), "512", "4096", &enable_512e);
  724. if (result != VDO_SUCCESS) {
  725. handle_parse_error(config, error_ptr, "Invalid logical block size");
  726. return VDO_BAD_CONFIGURATION;
  727. }
  728. config->logical_block_size = (enable_512e ? 512 : 4096);
  729. /* Skip past the two no longer used read cache options. */
  730. if (config->version <= 1)
  731. dm_consume_args(&arg_set, 2);
  732. /* Get the page cache size. */
  733. result = kstrtouint(dm_shift_arg(&arg_set), 10, &config->cache_size);
  734. if (result != VDO_SUCCESS) {
  735. handle_parse_error(config, error_ptr,
  736. "Invalid block map page cache size");
  737. return VDO_BAD_CONFIGURATION;
  738. }
  739. /* Get the block map era length. */
  740. result = kstrtouint(dm_shift_arg(&arg_set), 10, &config->block_map_maximum_age);
  741. if (result != VDO_SUCCESS) {
  742. handle_parse_error(config, error_ptr, "Invalid block map maximum age");
  743. return VDO_BAD_CONFIGURATION;
  744. }
  745. /* Skip past the no longer used MD RAID5 optimization mode */
  746. if (config->version <= 2)
  747. dm_consume_args(&arg_set, 1);
  748. /* Skip past the no longer used write policy setting */
  749. if (config->version <= 3)
  750. dm_consume_args(&arg_set, 1);
  751. /* Skip past the no longer used pool name for older table lines */
  752. if (config->version <= 2) {
  753. /*
  754. * Make sure the enum to get the pool name from argv directly is still in sync with
  755. * the parsing of the table line.
  756. */
  757. if (&arg_set.argv[0] != &argv[POOL_NAME_ARG_INDEX[config->version]]) {
  758. handle_parse_error(config, error_ptr,
  759. "Pool name not in expected location");
  760. return VDO_BAD_CONFIGURATION;
  761. }
  762. dm_shift_arg(&arg_set);
  763. }
  764. /* Get the optional arguments and validate. */
  765. result = parse_optional_arguments(&arg_set, error_ptr, config);
  766. if (result != VDO_SUCCESS) {
  767. /* parse_optional_arguments sets error_ptr itself. */
  768. handle_parse_error(config, error_ptr, *error_ptr);
  769. return result;
  770. }
  771. /*
  772. * Logical, physical, and hash zone counts can all be zero; then we get one thread doing
  773. * everything, our older configuration. If any zone count is non-zero, the others must be
  774. * as well.
  775. */
  776. if (((config->thread_counts.logical_zones == 0) !=
  777. (config->thread_counts.physical_zones == 0)) ||
  778. ((config->thread_counts.physical_zones == 0) !=
  779. (config->thread_counts.hash_zones == 0))) {
  780. handle_parse_error(config, error_ptr,
  781. "Logical, physical, and hash zones counts must all be zero or all non-zero");
  782. return VDO_BAD_CONFIGURATION;
  783. }
  784. if (config->cache_size <
  785. (2 * MAXIMUM_VDO_USER_VIOS * config->thread_counts.logical_zones)) {
  786. handle_parse_error(config, error_ptr,
  787. "Insufficient block map cache for logical zones");
  788. return VDO_BAD_CONFIGURATION;
  789. }
  790. result = dm_get_device(ti, config->parent_device_name,
  791. dm_table_get_mode(ti->table), &config->owned_device);
  792. if (result != 0) {
  793. vdo_log_error("couldn't open device \"%s\": error %d",
  794. config->parent_device_name, result);
  795. handle_parse_error(config, error_ptr, "Unable to open storage device");
  796. return VDO_BAD_CONFIGURATION;
  797. }
  798. if (config->version == 0) {
  799. u64 device_size = bdev_nr_bytes(config->owned_device->bdev);
  800. config->physical_blocks = device_size / VDO_BLOCK_SIZE;
  801. }
  802. *config_ptr = config;
  803. return result;
  804. }
  805. static struct vdo *get_vdo_for_target(struct dm_target *ti)
  806. {
  807. return ((struct device_config *) ti->private)->vdo;
  808. }
  809. static int vdo_map_bio(struct dm_target *ti, struct bio *bio)
  810. {
  811. struct vdo *vdo = get_vdo_for_target(ti);
  812. struct vdo_work_queue *current_work_queue;
  813. const struct admin_state_code *code = vdo_get_admin_state_code(&vdo->admin.state);
  814. VDO_ASSERT_LOG_ONLY(code->normal, "vdo should not receive bios while in state %s",
  815. code->name);
  816. /* Count all incoming bios. */
  817. vdo_count_bios(&vdo->stats.bios_in, bio);
  818. /* Handle empty bios. Empty flush bios are not associated with a vio. */
  819. if ((bio_op(bio) == REQ_OP_FLUSH) || ((bio->bi_opf & REQ_PREFLUSH) != 0)) {
  820. vdo_launch_flush(vdo, bio);
  821. return DM_MAPIO_SUBMITTED;
  822. }
  823. /* This could deadlock, */
  824. current_work_queue = vdo_get_current_work_queue();
  825. BUG_ON((current_work_queue != NULL) &&
  826. (vdo == vdo_get_work_queue_owner(current_work_queue)->vdo));
  827. vdo_launch_bio(vdo->data_vio_pool, bio);
  828. return DM_MAPIO_SUBMITTED;
  829. }
  830. static void vdo_io_hints(struct dm_target *ti, struct queue_limits *limits)
  831. {
  832. struct vdo *vdo = get_vdo_for_target(ti);
  833. limits->logical_block_size = vdo->device_config->logical_block_size;
  834. limits->physical_block_size = VDO_BLOCK_SIZE;
  835. /* The minimum io size for random io */
  836. limits->io_min = VDO_BLOCK_SIZE;
  837. /* The optimal io size for streamed/sequential io */
  838. limits->io_opt = VDO_BLOCK_SIZE;
  839. /*
  840. * Sets the maximum discard size that will be passed into VDO. This value comes from a
  841. * table line value passed in during dmsetup create.
  842. *
  843. * The value 1024 is the largest usable value on HD systems. A 2048 sector discard on a
  844. * busy HD system takes 31 seconds. We should use a value no higher than 1024, which takes
  845. * 15 to 16 seconds on a busy HD system. However, using large values results in 120 second
  846. * blocked task warnings in kernel logs. In order to avoid these warnings, we choose to
  847. * use the smallest reasonable value.
  848. *
  849. * The value is used by dm-thin to determine whether to pass down discards. The block layer
  850. * splits large discards on this boundary when this is set.
  851. */
  852. limits->max_hw_discard_sectors =
  853. (vdo->device_config->max_discard_blocks * VDO_SECTORS_PER_BLOCK);
  854. /*
  855. * Force discards to not begin or end with a partial block by stating the granularity is
  856. * 4k.
  857. */
  858. limits->discard_granularity = VDO_BLOCK_SIZE;
  859. }
  860. static int vdo_iterate_devices(struct dm_target *ti, iterate_devices_callout_fn fn,
  861. void *data)
  862. {
  863. struct device_config *config = get_vdo_for_target(ti)->device_config;
  864. return fn(ti, config->owned_device, 0,
  865. config->physical_blocks * VDO_SECTORS_PER_BLOCK, data);
  866. }
  867. /*
  868. * Status line is:
  869. * <device> <operating mode> <in recovery> <index state> <compression state>
  870. * <used physical blocks> <total physical blocks>
  871. */
  872. static void vdo_status(struct dm_target *ti, status_type_t status_type,
  873. unsigned int status_flags, char *result, unsigned int maxlen)
  874. {
  875. struct vdo *vdo = get_vdo_for_target(ti);
  876. struct vdo_statistics *stats;
  877. struct device_config *device_config;
  878. /* N.B.: The DMEMIT macro uses the variables named "sz", "result", "maxlen". */
  879. int sz = 0;
  880. switch (status_type) {
  881. case STATUSTYPE_INFO:
  882. /* Report info for dmsetup status */
  883. mutex_lock(&vdo->stats_mutex);
  884. vdo_fetch_statistics(vdo, &vdo->stats_buffer);
  885. stats = &vdo->stats_buffer;
  886. DMEMIT("/dev/%pg %s %s %s %s %llu %llu",
  887. vdo_get_backing_device(vdo), stats->mode,
  888. stats->in_recovery_mode ? "recovering" : "-",
  889. vdo_get_dedupe_index_state_name(vdo->hash_zones),
  890. vdo_get_compressing(vdo) ? "online" : "offline",
  891. stats->data_blocks_used + stats->overhead_blocks_used,
  892. stats->physical_blocks);
  893. mutex_unlock(&vdo->stats_mutex);
  894. break;
  895. case STATUSTYPE_TABLE:
  896. /* Report the string actually specified in the beginning. */
  897. device_config = (struct device_config *) ti->private;
  898. DMEMIT("%s", device_config->original_string);
  899. break;
  900. case STATUSTYPE_IMA:
  901. /* FIXME: We ought to be more detailed here, but this is what thin does. */
  902. *result = '\0';
  903. break;
  904. }
  905. }
  906. static block_count_t __must_check get_underlying_device_block_count(const struct vdo *vdo)
  907. {
  908. return bdev_nr_bytes(vdo_get_backing_device(vdo)) / VDO_BLOCK_SIZE;
  909. }
  910. static int __must_check process_vdo_message_locked(struct vdo *vdo, unsigned int argc,
  911. char **argv)
  912. {
  913. if ((argc == 2) && (strcasecmp(argv[0], "compression") == 0)) {
  914. if (strcasecmp(argv[1], "on") == 0) {
  915. vdo_set_compressing(vdo, true);
  916. return 0;
  917. }
  918. if (strcasecmp(argv[1], "off") == 0) {
  919. vdo_set_compressing(vdo, false);
  920. return 0;
  921. }
  922. vdo_log_warning("invalid argument '%s' to dmsetup compression message",
  923. argv[1]);
  924. return -EINVAL;
  925. }
  926. vdo_log_warning("unrecognized dmsetup message '%s' received", argv[0]);
  927. return -EINVAL;
  928. }
  929. /*
  930. * If the message is a dump, just do it. Otherwise, check that no other message is being processed,
  931. * and only proceed if so.
  932. * Returns -EBUSY if another message is being processed
  933. */
  934. static int __must_check process_vdo_message(struct vdo *vdo, unsigned int argc,
  935. char **argv)
  936. {
  937. int result;
  938. /*
  939. * All messages which may be processed in parallel with other messages should be handled
  940. * here before the atomic check below. Messages which should be exclusive should be
  941. * processed in process_vdo_message_locked().
  942. */
  943. /* Dump messages should always be processed */
  944. if (strcasecmp(argv[0], "dump") == 0)
  945. return vdo_dump(vdo, argc, argv, "dmsetup message");
  946. if (argc == 1) {
  947. if (strcasecmp(argv[0], "dump-on-shutdown") == 0) {
  948. vdo->dump_on_shutdown = true;
  949. return 0;
  950. }
  951. /* Index messages should always be processed */
  952. if ((strcasecmp(argv[0], "index-close") == 0) ||
  953. (strcasecmp(argv[0], "index-create") == 0) ||
  954. (strcasecmp(argv[0], "index-disable") == 0) ||
  955. (strcasecmp(argv[0], "index-enable") == 0))
  956. return vdo_message_dedupe_index(vdo->hash_zones, argv[0]);
  957. }
  958. if (atomic_cmpxchg(&vdo->processing_message, 0, 1) != 0)
  959. return -EBUSY;
  960. result = process_vdo_message_locked(vdo, argc, argv);
  961. /* Pairs with the implicit barrier in cmpxchg just above */
  962. smp_wmb();
  963. atomic_set(&vdo->processing_message, 0);
  964. return result;
  965. }
  966. static int vdo_message(struct dm_target *ti, unsigned int argc, char **argv,
  967. char *result_buffer, unsigned int maxlen)
  968. {
  969. struct registered_thread allocating_thread, instance_thread;
  970. struct vdo *vdo;
  971. int result;
  972. if (argc == 0) {
  973. vdo_log_warning("unspecified dmsetup message");
  974. return -EINVAL;
  975. }
  976. vdo = get_vdo_for_target(ti);
  977. vdo_register_allocating_thread(&allocating_thread, NULL);
  978. vdo_register_thread_device_id(&instance_thread, &vdo->instance);
  979. /*
  980. * Must be done here so we don't map return codes. The code in dm-ioctl expects a 1 for a
  981. * return code to look at the buffer and see if it is full or not.
  982. */
  983. if ((argc == 1) && (strcasecmp(argv[0], "stats") == 0)) {
  984. vdo_write_stats(vdo, result_buffer, maxlen);
  985. result = 1;
  986. } else if ((argc == 1) && (strcasecmp(argv[0], "config") == 0)) {
  987. vdo_write_config(vdo, &result_buffer, &maxlen);
  988. result = 1;
  989. } else {
  990. result = vdo_status_to_errno(process_vdo_message(vdo, argc, argv));
  991. }
  992. vdo_unregister_thread_device_id();
  993. vdo_unregister_allocating_thread();
  994. return result;
  995. }
  996. static void configure_target_capabilities(struct dm_target *ti)
  997. {
  998. ti->discards_supported = 1;
  999. ti->flush_supported = true;
  1000. ti->num_discard_bios = 1;
  1001. ti->num_flush_bios = 1;
  1002. /*
  1003. * If this value changes, please make sure to update the value for max_discard_sectors
  1004. * accordingly.
  1005. */
  1006. BUG_ON(dm_set_target_max_io_len(ti, VDO_SECTORS_PER_BLOCK) != 0);
  1007. }
  1008. /*
  1009. * Implements vdo_filter_fn.
  1010. */
  1011. static bool vdo_uses_device(struct vdo *vdo, const void *context)
  1012. {
  1013. const struct device_config *config = context;
  1014. return vdo_get_backing_device(vdo)->bd_dev == config->owned_device->bdev->bd_dev;
  1015. }
  1016. /**
  1017. * get_thread_id_for_phase() - Get the thread id for the current phase of the admin operation in
  1018. * progress.
  1019. * @vdo: The vdo.
  1020. */
  1021. static thread_id_t __must_check get_thread_id_for_phase(struct vdo *vdo)
  1022. {
  1023. switch (vdo->admin.phase) {
  1024. case RESUME_PHASE_PACKER:
  1025. case RESUME_PHASE_FLUSHER:
  1026. case SUSPEND_PHASE_PACKER:
  1027. case SUSPEND_PHASE_FLUSHES:
  1028. return vdo->thread_config.packer_thread;
  1029. case RESUME_PHASE_DATA_VIOS:
  1030. case SUSPEND_PHASE_DATA_VIOS:
  1031. return vdo->thread_config.cpu_thread;
  1032. case LOAD_PHASE_DRAIN_JOURNAL:
  1033. case RESUME_PHASE_JOURNAL:
  1034. case SUSPEND_PHASE_JOURNAL:
  1035. return vdo->thread_config.journal_thread;
  1036. default:
  1037. return vdo->thread_config.admin_thread;
  1038. }
  1039. }
  1040. static struct vdo_completion *prepare_admin_completion(struct vdo *vdo,
  1041. vdo_action_fn callback,
  1042. vdo_action_fn error_handler)
  1043. {
  1044. struct vdo_completion *completion = &vdo->admin.completion;
  1045. /*
  1046. * We can't use vdo_prepare_completion_for_requeue() here because we don't want to reset
  1047. * any error in the completion.
  1048. */
  1049. completion->callback = callback;
  1050. completion->error_handler = error_handler;
  1051. completion->callback_thread_id = get_thread_id_for_phase(vdo);
  1052. completion->requeue = true;
  1053. return completion;
  1054. }
  1055. /**
  1056. * advance_phase() - Increment the phase of the current admin operation and prepare the admin
  1057. * completion to run on the thread for the next phase.
  1058. * @vdo: The vdo on which an admin operation is being performed.
  1059. *
  1060. * Return: The current phase.
  1061. */
  1062. static u32 advance_phase(struct vdo *vdo)
  1063. {
  1064. u32 phase = vdo->admin.phase++;
  1065. vdo->admin.completion.callback_thread_id = get_thread_id_for_phase(vdo);
  1066. vdo->admin.completion.requeue = true;
  1067. return phase;
  1068. }
  1069. /*
  1070. * Perform an administrative operation (load, suspend, grow logical, or grow physical). This method
  1071. * should not be called from vdo threads.
  1072. */
  1073. static int perform_admin_operation(struct vdo *vdo, u32 starting_phase,
  1074. vdo_action_fn callback, vdo_action_fn error_handler,
  1075. const char *type)
  1076. {
  1077. int result;
  1078. struct vdo_administrator *admin = &vdo->admin;
  1079. if (atomic_cmpxchg(&admin->busy, 0, 1) != 0) {
  1080. return vdo_log_error_strerror(VDO_COMPONENT_BUSY,
  1081. "Can't start %s operation, another operation is already in progress",
  1082. type);
  1083. }
  1084. admin->phase = starting_phase;
  1085. reinit_completion(&admin->callback_sync);
  1086. vdo_reset_completion(&admin->completion);
  1087. vdo_launch_completion(prepare_admin_completion(vdo, callback, error_handler));
  1088. /*
  1089. * Using the "interruptible" interface means that Linux will not log a message when we wait
  1090. * for more than 120 seconds.
  1091. */
  1092. while (wait_for_completion_interruptible(&admin->callback_sync)) {
  1093. /* However, if we get a signal in a user-mode process, we could spin... */
  1094. fsleep(1000);
  1095. }
  1096. result = admin->completion.result;
  1097. /* pairs with implicit barrier in cmpxchg above */
  1098. smp_wmb();
  1099. atomic_set(&admin->busy, 0);
  1100. return result;
  1101. }
  1102. /* Assert that we are operating on the correct thread for the current phase. */
  1103. static void assert_admin_phase_thread(struct vdo *vdo, const char *what)
  1104. {
  1105. VDO_ASSERT_LOG_ONLY(vdo_get_callback_thread_id() == get_thread_id_for_phase(vdo),
  1106. "%s on correct thread for %s", what,
  1107. ADMIN_PHASE_NAMES[vdo->admin.phase]);
  1108. }
  1109. /**
  1110. * finish_operation_callback() - Callback to finish an admin operation.
  1111. * @completion: The admin_completion.
  1112. */
  1113. static void finish_operation_callback(struct vdo_completion *completion)
  1114. {
  1115. struct vdo_administrator *admin = &completion->vdo->admin;
  1116. vdo_finish_operation(&admin->state, completion->result);
  1117. complete(&admin->callback_sync);
  1118. }
  1119. /**
  1120. * decode_from_super_block() - Decode the VDO state from the super block and validate that it is
  1121. * correct.
  1122. * @vdo: The vdo being loaded.
  1123. *
  1124. * On error from this method, the component states must be destroyed explicitly. If this method
  1125. * returns successfully, the component states must not be destroyed.
  1126. *
  1127. * Return: VDO_SUCCESS or an error.
  1128. */
  1129. static int __must_check decode_from_super_block(struct vdo *vdo)
  1130. {
  1131. const struct device_config *config = vdo->device_config;
  1132. int result;
  1133. result = vdo_decode_component_states(vdo->super_block.buffer, &vdo->geometry,
  1134. &vdo->states);
  1135. if (result != VDO_SUCCESS)
  1136. return result;
  1137. vdo_set_state(vdo, vdo->states.vdo.state);
  1138. vdo->load_state = vdo->states.vdo.state;
  1139. /*
  1140. * If the device config specifies a larger logical size than was recorded in the super
  1141. * block, just accept it.
  1142. */
  1143. if (vdo->states.vdo.config.logical_blocks < config->logical_blocks) {
  1144. vdo_log_warning("Growing logical size: a logical size of %llu blocks was specified, but that differs from the %llu blocks configured in the vdo super block",
  1145. (unsigned long long) config->logical_blocks,
  1146. (unsigned long long) vdo->states.vdo.config.logical_blocks);
  1147. vdo->states.vdo.config.logical_blocks = config->logical_blocks;
  1148. }
  1149. result = vdo_validate_component_states(&vdo->states, vdo->geometry.nonce,
  1150. config->physical_blocks,
  1151. config->logical_blocks);
  1152. if (result != VDO_SUCCESS)
  1153. return result;
  1154. vdo->layout = vdo->states.layout;
  1155. return VDO_SUCCESS;
  1156. }
  1157. /**
  1158. * decode_vdo() - Decode the component data portion of a super block and fill in the corresponding
  1159. * portions of the vdo being loaded.
  1160. * @vdo: The vdo being loaded.
  1161. *
  1162. * This will also allocate the recovery journal and slab depot. If this method is called with an
  1163. * asynchronous layer (i.e. a thread config which specifies at least one base thread), the block
  1164. * map and packer will be constructed as well.
  1165. *
  1166. * Return: VDO_SUCCESS or an error.
  1167. */
  1168. static int __must_check decode_vdo(struct vdo *vdo)
  1169. {
  1170. block_count_t maximum_age, journal_length;
  1171. struct partition *partition;
  1172. int result;
  1173. result = decode_from_super_block(vdo);
  1174. if (result != VDO_SUCCESS) {
  1175. vdo_destroy_component_states(&vdo->states);
  1176. return result;
  1177. }
  1178. maximum_age = vdo_convert_maximum_age(vdo->device_config->block_map_maximum_age);
  1179. journal_length =
  1180. vdo_get_recovery_journal_length(vdo->states.vdo.config.recovery_journal_size);
  1181. if (maximum_age > (journal_length / 2)) {
  1182. return vdo_log_error_strerror(VDO_BAD_CONFIGURATION,
  1183. "maximum age: %llu exceeds limit %llu",
  1184. (unsigned long long) maximum_age,
  1185. (unsigned long long) (journal_length / 2));
  1186. }
  1187. if (maximum_age == 0) {
  1188. return vdo_log_error_strerror(VDO_BAD_CONFIGURATION,
  1189. "maximum age must be greater than 0");
  1190. }
  1191. result = vdo_enable_read_only_entry(vdo);
  1192. if (result != VDO_SUCCESS)
  1193. return result;
  1194. partition = vdo_get_known_partition(&vdo->layout,
  1195. VDO_RECOVERY_JOURNAL_PARTITION);
  1196. result = vdo_decode_recovery_journal(vdo->states.recovery_journal,
  1197. vdo->states.vdo.nonce, vdo, partition,
  1198. vdo->states.vdo.complete_recoveries,
  1199. vdo->states.vdo.config.recovery_journal_size,
  1200. &vdo->recovery_journal);
  1201. if (result != VDO_SUCCESS)
  1202. return result;
  1203. partition = vdo_get_known_partition(&vdo->layout, VDO_SLAB_SUMMARY_PARTITION);
  1204. result = vdo_decode_slab_depot(vdo->states.slab_depot, vdo, partition,
  1205. &vdo->depot);
  1206. if (result != VDO_SUCCESS)
  1207. return result;
  1208. result = vdo_decode_block_map(vdo->states.block_map,
  1209. vdo->states.vdo.config.logical_blocks, vdo,
  1210. vdo->recovery_journal, vdo->states.vdo.nonce,
  1211. vdo->device_config->cache_size, maximum_age,
  1212. &vdo->block_map);
  1213. if (result != VDO_SUCCESS)
  1214. return result;
  1215. result = vdo_make_physical_zones(vdo, &vdo->physical_zones);
  1216. if (result != VDO_SUCCESS)
  1217. return result;
  1218. /* The logical zones depend on the physical zones already existing. */
  1219. result = vdo_make_logical_zones(vdo, &vdo->logical_zones);
  1220. if (result != VDO_SUCCESS)
  1221. return result;
  1222. return vdo_make_hash_zones(vdo, &vdo->hash_zones);
  1223. }
  1224. /**
  1225. * pre_load_callback() - Callback to initiate a pre-load, registered in vdo_initialize().
  1226. * @completion: The admin completion.
  1227. */
  1228. static void pre_load_callback(struct vdo_completion *completion)
  1229. {
  1230. struct vdo *vdo = completion->vdo;
  1231. int result;
  1232. assert_admin_phase_thread(vdo, __func__);
  1233. switch (advance_phase(vdo)) {
  1234. case PRE_LOAD_PHASE_START:
  1235. result = vdo_start_operation(&vdo->admin.state,
  1236. VDO_ADMIN_STATE_PRE_LOADING);
  1237. if (result != VDO_SUCCESS) {
  1238. vdo_continue_completion(completion, result);
  1239. return;
  1240. }
  1241. vdo_load_super_block(vdo, completion);
  1242. return;
  1243. case PRE_LOAD_PHASE_LOAD_COMPONENTS:
  1244. vdo_continue_completion(completion, decode_vdo(vdo));
  1245. return;
  1246. case PRE_LOAD_PHASE_END:
  1247. break;
  1248. default:
  1249. vdo_set_completion_result(completion, UDS_BAD_STATE);
  1250. }
  1251. finish_operation_callback(completion);
  1252. }
  1253. static void release_instance(unsigned int instance)
  1254. {
  1255. mutex_lock(&instances_lock);
  1256. if (instance >= instances.bit_count) {
  1257. VDO_ASSERT_LOG_ONLY(false,
  1258. "instance number %u must be less than bit count %u",
  1259. instance, instances.bit_count);
  1260. } else if (test_bit(instance, instances.words) == 0) {
  1261. VDO_ASSERT_LOG_ONLY(false, "instance number %u must be allocated", instance);
  1262. } else {
  1263. __clear_bit(instance, instances.words);
  1264. instances.count -= 1;
  1265. }
  1266. mutex_unlock(&instances_lock);
  1267. }
  1268. static void set_device_config(struct dm_target *ti, struct vdo *vdo,
  1269. struct device_config *config)
  1270. {
  1271. list_del_init(&config->config_list);
  1272. list_add_tail(&config->config_list, &vdo->device_config_list);
  1273. config->vdo = vdo;
  1274. ti->private = config;
  1275. configure_target_capabilities(ti);
  1276. }
  1277. static int vdo_initialize(struct dm_target *ti, unsigned int instance,
  1278. struct device_config *config)
  1279. {
  1280. struct vdo *vdo;
  1281. int result;
  1282. u64 block_size = VDO_BLOCK_SIZE;
  1283. u64 logical_size = to_bytes(ti->len);
  1284. block_count_t logical_blocks = logical_size / block_size;
  1285. vdo_log_info("loading device '%s'", vdo_get_device_name(ti));
  1286. vdo_log_debug("Logical block size = %llu", (u64) config->logical_block_size);
  1287. vdo_log_debug("Logical blocks = %llu", logical_blocks);
  1288. vdo_log_debug("Physical block size = %llu", (u64) block_size);
  1289. vdo_log_debug("Physical blocks = %llu", config->physical_blocks);
  1290. vdo_log_debug("Block map cache blocks = %u", config->cache_size);
  1291. vdo_log_debug("Block map maximum age = %u", config->block_map_maximum_age);
  1292. vdo_log_debug("Deduplication = %s", (config->deduplication ? "on" : "off"));
  1293. vdo_log_debug("Compression = %s", (config->compression ? "on" : "off"));
  1294. vdo = vdo_find_matching(vdo_uses_device, config);
  1295. if (vdo != NULL) {
  1296. vdo_log_error("Existing vdo already uses device %s",
  1297. vdo->device_config->parent_device_name);
  1298. ti->error = "Cannot share storage device with already-running VDO";
  1299. return VDO_BAD_CONFIGURATION;
  1300. }
  1301. result = vdo_make(instance, config, &ti->error, &vdo);
  1302. if (result != VDO_SUCCESS) {
  1303. vdo_log_error("Could not create VDO device. (VDO error %d, message %s)",
  1304. result, ti->error);
  1305. vdo_destroy(vdo);
  1306. return result;
  1307. }
  1308. result = perform_admin_operation(vdo, PRE_LOAD_PHASE_START, pre_load_callback,
  1309. finish_operation_callback, "pre-load");
  1310. if (result != VDO_SUCCESS) {
  1311. ti->error = ((result == VDO_INVALID_ADMIN_STATE) ?
  1312. "Pre-load is only valid immediately after initialization" :
  1313. "Cannot load metadata from device");
  1314. vdo_log_error("Could not start VDO device. (VDO error %d, message %s)",
  1315. result, ti->error);
  1316. vdo_destroy(vdo);
  1317. return result;
  1318. }
  1319. set_device_config(ti, vdo, config);
  1320. vdo->device_config = config;
  1321. return VDO_SUCCESS;
  1322. }
  1323. /* Implements vdo_filter_fn. */
  1324. static bool __must_check vdo_is_named(struct vdo *vdo, const void *context)
  1325. {
  1326. struct dm_target *ti = vdo->device_config->owning_target;
  1327. const char *device_name = vdo_get_device_name(ti);
  1328. return strcmp(device_name, context) == 0;
  1329. }
  1330. /**
  1331. * get_bit_array_size() - Return the number of bytes needed to store a bit array of the specified
  1332. * capacity in an array of unsigned longs.
  1333. * @bit_count: The number of bits the array must hold.
  1334. *
  1335. * Return: the number of bytes needed for the array representation.
  1336. */
  1337. static size_t get_bit_array_size(unsigned int bit_count)
  1338. {
  1339. /* Round up to a multiple of the word size and convert to a byte count. */
  1340. return (BITS_TO_LONGS(bit_count) * sizeof(unsigned long));
  1341. }
  1342. /**
  1343. * grow_bit_array() - Re-allocate the bitmap word array so there will more instance numbers that
  1344. * can be allocated.
  1345. *
  1346. * Since the array is initially NULL, this also initializes the array the first time we allocate an
  1347. * instance number.
  1348. *
  1349. * Return: VDO_SUCCESS or an error code from the allocation
  1350. */
  1351. static int grow_bit_array(void)
  1352. {
  1353. unsigned int new_count = max(instances.bit_count + BIT_COUNT_INCREMENT,
  1354. (unsigned int) BIT_COUNT_MINIMUM);
  1355. unsigned long *new_words;
  1356. int result;
  1357. result = vdo_reallocate_memory(instances.words,
  1358. get_bit_array_size(instances.bit_count),
  1359. get_bit_array_size(new_count),
  1360. "instance number bit array", &new_words);
  1361. if (result != VDO_SUCCESS)
  1362. return result;
  1363. instances.bit_count = new_count;
  1364. instances.words = new_words;
  1365. return VDO_SUCCESS;
  1366. }
  1367. /**
  1368. * allocate_instance() - Allocate an instance number.
  1369. * @instance_ptr: A point to hold the instance number
  1370. *
  1371. * Return: VDO_SUCCESS or an error code
  1372. *
  1373. * This function must be called while holding the instances lock.
  1374. */
  1375. static int allocate_instance(unsigned int *instance_ptr)
  1376. {
  1377. unsigned int instance;
  1378. int result;
  1379. /* If there are no unallocated instances, grow the bit array. */
  1380. if (instances.count >= instances.bit_count) {
  1381. result = grow_bit_array();
  1382. if (result != VDO_SUCCESS)
  1383. return result;
  1384. }
  1385. /*
  1386. * There must be a zero bit somewhere now. Find it, starting just after the last instance
  1387. * allocated.
  1388. */
  1389. instance = find_next_zero_bit(instances.words, instances.bit_count,
  1390. instances.next);
  1391. if (instance >= instances.bit_count) {
  1392. /* Nothing free after next, so wrap around to instance zero. */
  1393. instance = find_first_zero_bit(instances.words, instances.bit_count);
  1394. result = VDO_ASSERT(instance < instances.bit_count,
  1395. "impossibly, no zero bit found");
  1396. if (result != VDO_SUCCESS)
  1397. return result;
  1398. }
  1399. __set_bit(instance, instances.words);
  1400. instances.count++;
  1401. instances.next = instance + 1;
  1402. *instance_ptr = instance;
  1403. return VDO_SUCCESS;
  1404. }
  1405. static int construct_new_vdo_registered(struct dm_target *ti, unsigned int argc,
  1406. char **argv, unsigned int instance)
  1407. {
  1408. int result;
  1409. struct device_config *config;
  1410. result = parse_device_config(argc, argv, ti, &config);
  1411. if (result != VDO_SUCCESS) {
  1412. vdo_log_error_strerror(result, "parsing failed: %s", ti->error);
  1413. release_instance(instance);
  1414. return -EINVAL;
  1415. }
  1416. /* Beyond this point, the instance number will be cleaned up for us if needed */
  1417. result = vdo_initialize(ti, instance, config);
  1418. if (result != VDO_SUCCESS) {
  1419. release_instance(instance);
  1420. free_device_config(config);
  1421. return vdo_status_to_errno(result);
  1422. }
  1423. return VDO_SUCCESS;
  1424. }
  1425. static int construct_new_vdo(struct dm_target *ti, unsigned int argc, char **argv)
  1426. {
  1427. int result;
  1428. unsigned int instance;
  1429. struct registered_thread instance_thread;
  1430. mutex_lock(&instances_lock);
  1431. result = allocate_instance(&instance);
  1432. mutex_unlock(&instances_lock);
  1433. if (result != VDO_SUCCESS)
  1434. return -ENOMEM;
  1435. vdo_register_thread_device_id(&instance_thread, &instance);
  1436. result = construct_new_vdo_registered(ti, argc, argv, instance);
  1437. vdo_unregister_thread_device_id();
  1438. return result;
  1439. }
  1440. /**
  1441. * check_may_grow_physical() - Callback to check that we're not in recovery mode, used in
  1442. * vdo_prepare_to_grow_physical().
  1443. * @completion: The admin completion.
  1444. */
  1445. static void check_may_grow_physical(struct vdo_completion *completion)
  1446. {
  1447. struct vdo *vdo = completion->vdo;
  1448. assert_admin_phase_thread(vdo, __func__);
  1449. /* These checks can only be done from a vdo thread. */
  1450. if (vdo_is_read_only(vdo))
  1451. vdo_set_completion_result(completion, VDO_READ_ONLY);
  1452. if (vdo_in_recovery_mode(vdo))
  1453. vdo_set_completion_result(completion, VDO_RETRY_AFTER_REBUILD);
  1454. finish_operation_callback(completion);
  1455. }
  1456. static block_count_t get_partition_size(struct layout *layout, enum partition_id id)
  1457. {
  1458. return vdo_get_known_partition(layout, id)->count;
  1459. }
  1460. /**
  1461. * grow_layout() - Make the layout for growing a vdo.
  1462. * @vdo: The vdo preparing to grow.
  1463. * @old_size: The current size of the vdo.
  1464. * @new_size: The size to which the vdo will be grown.
  1465. *
  1466. * Return: VDO_SUCCESS or an error code.
  1467. */
  1468. static int grow_layout(struct vdo *vdo, block_count_t old_size, block_count_t new_size)
  1469. {
  1470. int result;
  1471. block_count_t min_new_size;
  1472. if (vdo->next_layout.size == new_size) {
  1473. /* We are already prepared to grow to the new size, so we're done. */
  1474. return VDO_SUCCESS;
  1475. }
  1476. /* Make a copy completion if there isn't one */
  1477. if (vdo->partition_copier == NULL) {
  1478. vdo->partition_copier = dm_kcopyd_client_create(NULL);
  1479. if (IS_ERR(vdo->partition_copier)) {
  1480. result = PTR_ERR(vdo->partition_copier);
  1481. vdo->partition_copier = NULL;
  1482. return result;
  1483. }
  1484. }
  1485. /* Free any unused preparation. */
  1486. vdo_uninitialize_layout(&vdo->next_layout);
  1487. /*
  1488. * Make a new layout with the existing partition sizes for everything but the slab depot
  1489. * partition.
  1490. */
  1491. result = vdo_initialize_layout(new_size, vdo->layout.start,
  1492. get_partition_size(&vdo->layout,
  1493. VDO_BLOCK_MAP_PARTITION),
  1494. get_partition_size(&vdo->layout,
  1495. VDO_RECOVERY_JOURNAL_PARTITION),
  1496. get_partition_size(&vdo->layout,
  1497. VDO_SLAB_SUMMARY_PARTITION),
  1498. &vdo->next_layout);
  1499. if (result != VDO_SUCCESS) {
  1500. dm_kcopyd_client_destroy(vdo_forget(vdo->partition_copier));
  1501. return result;
  1502. }
  1503. /* Ensure the new journal and summary are entirely within the added blocks. */
  1504. min_new_size = (old_size +
  1505. get_partition_size(&vdo->next_layout,
  1506. VDO_SLAB_SUMMARY_PARTITION) +
  1507. get_partition_size(&vdo->next_layout,
  1508. VDO_RECOVERY_JOURNAL_PARTITION));
  1509. if (min_new_size > new_size) {
  1510. /* Copying the journal and summary would destroy some old metadata. */
  1511. vdo_uninitialize_layout(&vdo->next_layout);
  1512. dm_kcopyd_client_destroy(vdo_forget(vdo->partition_copier));
  1513. return VDO_INCREMENT_TOO_SMALL;
  1514. }
  1515. return VDO_SUCCESS;
  1516. }
  1517. static int prepare_to_grow_physical(struct vdo *vdo, block_count_t new_physical_blocks)
  1518. {
  1519. int result;
  1520. block_count_t current_physical_blocks = vdo->states.vdo.config.physical_blocks;
  1521. vdo_log_info("Preparing to resize physical to %llu",
  1522. (unsigned long long) new_physical_blocks);
  1523. VDO_ASSERT_LOG_ONLY((new_physical_blocks > current_physical_blocks),
  1524. "New physical size is larger than current physical size");
  1525. result = perform_admin_operation(vdo, PREPARE_GROW_PHYSICAL_PHASE_START,
  1526. check_may_grow_physical,
  1527. finish_operation_callback,
  1528. "prepare grow-physical");
  1529. if (result != VDO_SUCCESS)
  1530. return result;
  1531. result = grow_layout(vdo, current_physical_blocks, new_physical_blocks);
  1532. if (result != VDO_SUCCESS)
  1533. return result;
  1534. result = vdo_prepare_to_grow_slab_depot(vdo->depot,
  1535. vdo_get_known_partition(&vdo->next_layout,
  1536. VDO_SLAB_DEPOT_PARTITION));
  1537. if (result != VDO_SUCCESS) {
  1538. vdo_uninitialize_layout(&vdo->next_layout);
  1539. return result;
  1540. }
  1541. vdo_log_info("Done preparing to resize physical");
  1542. return VDO_SUCCESS;
  1543. }
  1544. /**
  1545. * validate_new_device_config() - Check whether a new device config represents a valid modification
  1546. * to an existing config.
  1547. * @to_validate: The new config to validate.
  1548. * @config: The existing config.
  1549. * @may_grow: Set to true if growing the logical and physical size of the vdo is currently
  1550. * permitted.
  1551. * @error_ptr: A pointer to hold the reason for any error.
  1552. *
  1553. * Return: VDO_SUCCESS or an error.
  1554. */
  1555. static int validate_new_device_config(struct device_config *to_validate,
  1556. struct device_config *config, bool may_grow,
  1557. char **error_ptr)
  1558. {
  1559. if (to_validate->owning_target->begin != config->owning_target->begin) {
  1560. *error_ptr = "Starting sector cannot change";
  1561. return VDO_PARAMETER_MISMATCH;
  1562. }
  1563. if (to_validate->logical_block_size != config->logical_block_size) {
  1564. *error_ptr = "Logical block size cannot change";
  1565. return VDO_PARAMETER_MISMATCH;
  1566. }
  1567. if (to_validate->logical_blocks < config->logical_blocks) {
  1568. *error_ptr = "Can't shrink VDO logical size";
  1569. return VDO_PARAMETER_MISMATCH;
  1570. }
  1571. if (to_validate->cache_size != config->cache_size) {
  1572. *error_ptr = "Block map cache size cannot change";
  1573. return VDO_PARAMETER_MISMATCH;
  1574. }
  1575. if (to_validate->block_map_maximum_age != config->block_map_maximum_age) {
  1576. *error_ptr = "Block map maximum age cannot change";
  1577. return VDO_PARAMETER_MISMATCH;
  1578. }
  1579. if (memcmp(&to_validate->thread_counts, &config->thread_counts,
  1580. sizeof(struct thread_count_config)) != 0) {
  1581. *error_ptr = "Thread configuration cannot change";
  1582. return VDO_PARAMETER_MISMATCH;
  1583. }
  1584. if (to_validate->physical_blocks < config->physical_blocks) {
  1585. *error_ptr = "Removing physical storage from a VDO is not supported";
  1586. return VDO_NOT_IMPLEMENTED;
  1587. }
  1588. if (!may_grow && (to_validate->physical_blocks > config->physical_blocks)) {
  1589. *error_ptr = "VDO physical size may not grow in current state";
  1590. return VDO_NOT_IMPLEMENTED;
  1591. }
  1592. return VDO_SUCCESS;
  1593. }
  1594. static int prepare_to_modify(struct dm_target *ti, struct device_config *config,
  1595. struct vdo *vdo)
  1596. {
  1597. int result;
  1598. bool may_grow = (vdo_get_admin_state(vdo) != VDO_ADMIN_STATE_PRE_LOADED);
  1599. result = validate_new_device_config(config, vdo->device_config, may_grow,
  1600. &ti->error);
  1601. if (result != VDO_SUCCESS)
  1602. return -EINVAL;
  1603. if (config->logical_blocks > vdo->device_config->logical_blocks) {
  1604. block_count_t logical_blocks = vdo->states.vdo.config.logical_blocks;
  1605. vdo_log_info("Preparing to resize logical to %llu",
  1606. (unsigned long long) config->logical_blocks);
  1607. VDO_ASSERT_LOG_ONLY((config->logical_blocks > logical_blocks),
  1608. "New logical size is larger than current size");
  1609. result = vdo_prepare_to_grow_block_map(vdo->block_map,
  1610. config->logical_blocks);
  1611. if (result != VDO_SUCCESS) {
  1612. ti->error = "Device vdo_prepare_to_grow_logical failed";
  1613. return result;
  1614. }
  1615. vdo_log_info("Done preparing to resize logical");
  1616. }
  1617. if (config->physical_blocks > vdo->device_config->physical_blocks) {
  1618. result = prepare_to_grow_physical(vdo, config->physical_blocks);
  1619. if (result != VDO_SUCCESS) {
  1620. if (result == VDO_PARAMETER_MISMATCH) {
  1621. /*
  1622. * If we don't trap this case, vdo_status_to_errno() will remap
  1623. * it to -EIO, which is misleading and ahistorical.
  1624. */
  1625. result = -EINVAL;
  1626. }
  1627. if (result == VDO_TOO_MANY_SLABS)
  1628. ti->error = "Device vdo_prepare_to_grow_physical failed (specified physical size too big based on formatted slab size)";
  1629. else
  1630. ti->error = "Device vdo_prepare_to_grow_physical failed";
  1631. return result;
  1632. }
  1633. }
  1634. if (strcmp(config->parent_device_name, vdo->device_config->parent_device_name) != 0) {
  1635. const char *device_name = vdo_get_device_name(config->owning_target);
  1636. vdo_log_info("Updating backing device of %s from %s to %s", device_name,
  1637. vdo->device_config->parent_device_name,
  1638. config->parent_device_name);
  1639. }
  1640. return VDO_SUCCESS;
  1641. }
  1642. static int update_existing_vdo(const char *device_name, struct dm_target *ti,
  1643. unsigned int argc, char **argv, struct vdo *vdo)
  1644. {
  1645. int result;
  1646. struct device_config *config;
  1647. result = parse_device_config(argc, argv, ti, &config);
  1648. if (result != VDO_SUCCESS)
  1649. return -EINVAL;
  1650. vdo_log_info("preparing to modify device '%s'", device_name);
  1651. result = prepare_to_modify(ti, config, vdo);
  1652. if (result != VDO_SUCCESS) {
  1653. free_device_config(config);
  1654. return vdo_status_to_errno(result);
  1655. }
  1656. set_device_config(ti, vdo, config);
  1657. return VDO_SUCCESS;
  1658. }
  1659. static int vdo_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1660. {
  1661. int result;
  1662. struct registered_thread allocating_thread, instance_thread;
  1663. const char *device_name;
  1664. struct vdo *vdo;
  1665. vdo_register_allocating_thread(&allocating_thread, NULL);
  1666. device_name = vdo_get_device_name(ti);
  1667. vdo = vdo_find_matching(vdo_is_named, device_name);
  1668. if (vdo == NULL) {
  1669. result = construct_new_vdo(ti, argc, argv);
  1670. } else {
  1671. vdo_register_thread_device_id(&instance_thread, &vdo->instance);
  1672. result = update_existing_vdo(device_name, ti, argc, argv, vdo);
  1673. vdo_unregister_thread_device_id();
  1674. }
  1675. vdo_unregister_allocating_thread();
  1676. return result;
  1677. }
  1678. static void vdo_dtr(struct dm_target *ti)
  1679. {
  1680. struct device_config *config = ti->private;
  1681. struct vdo *vdo = vdo_forget(config->vdo);
  1682. list_del_init(&config->config_list);
  1683. if (list_empty(&vdo->device_config_list)) {
  1684. const char *device_name;
  1685. /* This was the last config referencing the VDO. Free it. */
  1686. unsigned int instance = vdo->instance;
  1687. struct registered_thread allocating_thread, instance_thread;
  1688. vdo_register_thread_device_id(&instance_thread, &instance);
  1689. vdo_register_allocating_thread(&allocating_thread, NULL);
  1690. device_name = vdo_get_device_name(ti);
  1691. vdo_log_info("stopping device '%s'", device_name);
  1692. if (vdo->dump_on_shutdown)
  1693. vdo_dump_all(vdo, "device shutdown");
  1694. vdo_destroy(vdo_forget(vdo));
  1695. vdo_log_info("device '%s' stopped", device_name);
  1696. vdo_unregister_thread_device_id();
  1697. vdo_unregister_allocating_thread();
  1698. release_instance(instance);
  1699. } else if (config == vdo->device_config) {
  1700. /*
  1701. * The VDO still references this config. Give it a reference to a config that isn't
  1702. * being destroyed.
  1703. */
  1704. vdo->device_config = list_first_entry(&vdo->device_config_list,
  1705. struct device_config, config_list);
  1706. }
  1707. free_device_config(config);
  1708. ti->private = NULL;
  1709. }
  1710. static void vdo_presuspend(struct dm_target *ti)
  1711. {
  1712. get_vdo_for_target(ti)->suspend_type =
  1713. (dm_noflush_suspending(ti) ? VDO_ADMIN_STATE_SUSPENDING : VDO_ADMIN_STATE_SAVING);
  1714. }
  1715. /**
  1716. * write_super_block_for_suspend() - Update the VDO state and save the super block.
  1717. * @completion: The admin completion
  1718. */
  1719. static void write_super_block_for_suspend(struct vdo_completion *completion)
  1720. {
  1721. struct vdo *vdo = completion->vdo;
  1722. switch (vdo_get_state(vdo)) {
  1723. case VDO_DIRTY:
  1724. case VDO_NEW:
  1725. vdo_set_state(vdo, VDO_CLEAN);
  1726. break;
  1727. case VDO_CLEAN:
  1728. case VDO_READ_ONLY_MODE:
  1729. case VDO_FORCE_REBUILD:
  1730. case VDO_RECOVERING:
  1731. case VDO_REBUILD_FOR_UPGRADE:
  1732. break;
  1733. case VDO_REPLAYING:
  1734. default:
  1735. vdo_continue_completion(completion, UDS_BAD_STATE);
  1736. return;
  1737. }
  1738. vdo_save_components(vdo, completion);
  1739. }
  1740. /**
  1741. * suspend_callback() - Callback to initiate a suspend, registered in vdo_postsuspend().
  1742. * @completion: The sub-task completion.
  1743. */
  1744. static void suspend_callback(struct vdo_completion *completion)
  1745. {
  1746. struct vdo *vdo = completion->vdo;
  1747. struct admin_state *state = &vdo->admin.state;
  1748. int result;
  1749. assert_admin_phase_thread(vdo, __func__);
  1750. switch (advance_phase(vdo)) {
  1751. case SUSPEND_PHASE_START:
  1752. if (vdo_get_admin_state_code(state)->quiescent) {
  1753. /* Already suspended */
  1754. break;
  1755. }
  1756. vdo_continue_completion(completion,
  1757. vdo_start_operation(state, vdo->suspend_type));
  1758. return;
  1759. case SUSPEND_PHASE_PACKER:
  1760. /*
  1761. * If the VDO was already resumed from a prior suspend while read-only, some of the
  1762. * components may not have been resumed. By setting a read-only error here, we
  1763. * guarantee that the result of this suspend will be VDO_READ_ONLY and not
  1764. * VDO_INVALID_ADMIN_STATE in that case.
  1765. */
  1766. if (vdo_in_read_only_mode(vdo))
  1767. vdo_set_completion_result(completion, VDO_READ_ONLY);
  1768. vdo_drain_packer(vdo->packer, completion);
  1769. return;
  1770. case SUSPEND_PHASE_DATA_VIOS:
  1771. drain_data_vio_pool(vdo->data_vio_pool, completion);
  1772. return;
  1773. case SUSPEND_PHASE_DEDUPE:
  1774. vdo_drain_hash_zones(vdo->hash_zones, completion);
  1775. return;
  1776. case SUSPEND_PHASE_FLUSHES:
  1777. vdo_drain_flusher(vdo->flusher, completion);
  1778. return;
  1779. case SUSPEND_PHASE_LOGICAL_ZONES:
  1780. /*
  1781. * Attempt to flush all I/O before completing post suspend work. We believe a
  1782. * suspended device is expected to have persisted all data written before the
  1783. * suspend, even if it hasn't been flushed yet.
  1784. */
  1785. result = vdo_synchronous_flush(vdo);
  1786. if (result != VDO_SUCCESS)
  1787. vdo_enter_read_only_mode(vdo, result);
  1788. vdo_drain_logical_zones(vdo->logical_zones,
  1789. vdo_get_admin_state_code(state), completion);
  1790. return;
  1791. case SUSPEND_PHASE_BLOCK_MAP:
  1792. vdo_drain_block_map(vdo->block_map, vdo_get_admin_state_code(state),
  1793. completion);
  1794. return;
  1795. case SUSPEND_PHASE_JOURNAL:
  1796. vdo_drain_recovery_journal(vdo->recovery_journal,
  1797. vdo_get_admin_state_code(state), completion);
  1798. return;
  1799. case SUSPEND_PHASE_DEPOT:
  1800. vdo_drain_slab_depot(vdo->depot, vdo_get_admin_state_code(state),
  1801. completion);
  1802. return;
  1803. case SUSPEND_PHASE_READ_ONLY_WAIT:
  1804. vdo_wait_until_not_entering_read_only_mode(completion);
  1805. return;
  1806. case SUSPEND_PHASE_WRITE_SUPER_BLOCK:
  1807. if (vdo_is_state_suspending(state) || (completion->result != VDO_SUCCESS)) {
  1808. /* If we didn't save the VDO or there was an error, we're done. */
  1809. break;
  1810. }
  1811. write_super_block_for_suspend(completion);
  1812. return;
  1813. case SUSPEND_PHASE_END:
  1814. break;
  1815. default:
  1816. vdo_set_completion_result(completion, UDS_BAD_STATE);
  1817. }
  1818. finish_operation_callback(completion);
  1819. }
  1820. static void vdo_postsuspend(struct dm_target *ti)
  1821. {
  1822. struct vdo *vdo = get_vdo_for_target(ti);
  1823. struct registered_thread instance_thread;
  1824. const char *device_name;
  1825. int result;
  1826. vdo_register_thread_device_id(&instance_thread, &vdo->instance);
  1827. device_name = vdo_get_device_name(vdo->device_config->owning_target);
  1828. vdo_log_info("suspending device '%s'", device_name);
  1829. /*
  1830. * It's important to note any error here does not actually stop device-mapper from
  1831. * suspending the device. All this work is done post suspend.
  1832. */
  1833. result = perform_admin_operation(vdo, SUSPEND_PHASE_START, suspend_callback,
  1834. suspend_callback, "suspend");
  1835. if ((result == VDO_SUCCESS) || (result == VDO_READ_ONLY)) {
  1836. /*
  1837. * Treat VDO_READ_ONLY as a success since a read-only suspension still leaves the
  1838. * VDO suspended.
  1839. */
  1840. vdo_log_info("device '%s' suspended", device_name);
  1841. } else if (result == VDO_INVALID_ADMIN_STATE) {
  1842. vdo_log_error("Suspend invoked while in unexpected state: %s",
  1843. vdo_get_admin_state(vdo)->name);
  1844. } else {
  1845. vdo_log_error_strerror(result, "Suspend of device '%s' failed",
  1846. device_name);
  1847. }
  1848. vdo_unregister_thread_device_id();
  1849. }
  1850. /**
  1851. * was_new() - Check whether the vdo was new when it was loaded.
  1852. * @vdo: The vdo to query.
  1853. *
  1854. * Return: true if the vdo was new.
  1855. */
  1856. static bool was_new(const struct vdo *vdo)
  1857. {
  1858. return (vdo->load_state == VDO_NEW);
  1859. }
  1860. /**
  1861. * requires_repair() - Check whether a vdo requires recovery or rebuild.
  1862. * @vdo: The vdo to query.
  1863. *
  1864. * Return: true if the vdo must be repaired.
  1865. */
  1866. static bool __must_check requires_repair(const struct vdo *vdo)
  1867. {
  1868. switch (vdo_get_state(vdo)) {
  1869. case VDO_DIRTY:
  1870. case VDO_FORCE_REBUILD:
  1871. case VDO_REPLAYING:
  1872. case VDO_REBUILD_FOR_UPGRADE:
  1873. return true;
  1874. default:
  1875. return false;
  1876. }
  1877. }
  1878. /**
  1879. * get_load_type() - Determine how the slab depot was loaded.
  1880. * @vdo: The vdo.
  1881. *
  1882. * Return: How the depot was loaded.
  1883. */
  1884. static enum slab_depot_load_type get_load_type(struct vdo *vdo)
  1885. {
  1886. if (vdo_state_requires_read_only_rebuild(vdo->load_state))
  1887. return VDO_SLAB_DEPOT_REBUILD_LOAD;
  1888. if (vdo_state_requires_recovery(vdo->load_state))
  1889. return VDO_SLAB_DEPOT_RECOVERY_LOAD;
  1890. return VDO_SLAB_DEPOT_NORMAL_LOAD;
  1891. }
  1892. /**
  1893. * load_callback() - Callback to do the destructive parts of loading a VDO.
  1894. * @completion: The sub-task completion.
  1895. */
  1896. static void load_callback(struct vdo_completion *completion)
  1897. {
  1898. struct vdo *vdo = completion->vdo;
  1899. int result;
  1900. assert_admin_phase_thread(vdo, __func__);
  1901. switch (advance_phase(vdo)) {
  1902. case LOAD_PHASE_START:
  1903. result = vdo_start_operation(&vdo->admin.state, VDO_ADMIN_STATE_LOADING);
  1904. if (result != VDO_SUCCESS) {
  1905. vdo_continue_completion(completion, result);
  1906. return;
  1907. }
  1908. /* Prepare the recovery journal for new entries. */
  1909. vdo_open_recovery_journal(vdo->recovery_journal, vdo->depot,
  1910. vdo->block_map);
  1911. vdo_allow_read_only_mode_entry(completion);
  1912. return;
  1913. case LOAD_PHASE_LOAD_DEPOT:
  1914. vdo_set_dedupe_state_normal(vdo->hash_zones);
  1915. if (vdo_is_read_only(vdo)) {
  1916. /*
  1917. * In read-only mode we don't use the allocator and it may not even be
  1918. * readable, so don't bother trying to load it.
  1919. */
  1920. vdo_set_completion_result(completion, VDO_READ_ONLY);
  1921. break;
  1922. }
  1923. if (requires_repair(vdo)) {
  1924. vdo_repair(completion);
  1925. return;
  1926. }
  1927. vdo_load_slab_depot(vdo->depot,
  1928. (was_new(vdo) ? VDO_ADMIN_STATE_FORMATTING :
  1929. VDO_ADMIN_STATE_LOADING),
  1930. completion, NULL);
  1931. return;
  1932. case LOAD_PHASE_MAKE_DIRTY:
  1933. vdo_set_state(vdo, VDO_DIRTY);
  1934. vdo_save_components(vdo, completion);
  1935. return;
  1936. case LOAD_PHASE_PREPARE_TO_ALLOCATE:
  1937. vdo_initialize_block_map_from_journal(vdo->block_map,
  1938. vdo->recovery_journal);
  1939. vdo_prepare_slab_depot_to_allocate(vdo->depot, get_load_type(vdo),
  1940. completion);
  1941. return;
  1942. case LOAD_PHASE_SCRUB_SLABS:
  1943. if (vdo_state_requires_recovery(vdo->load_state))
  1944. vdo_enter_recovery_mode(vdo);
  1945. vdo_scrub_all_unrecovered_slabs(vdo->depot, completion);
  1946. return;
  1947. case LOAD_PHASE_DATA_REDUCTION:
  1948. WRITE_ONCE(vdo->compressing, vdo->device_config->compression);
  1949. if (vdo->device_config->deduplication) {
  1950. /*
  1951. * Don't try to load or rebuild the index first (and log scary error
  1952. * messages) if this is known to be a newly-formatted volume.
  1953. */
  1954. vdo_start_dedupe_index(vdo->hash_zones, was_new(vdo));
  1955. }
  1956. vdo->allocations_allowed = false;
  1957. fallthrough;
  1958. case LOAD_PHASE_FINISHED:
  1959. break;
  1960. case LOAD_PHASE_DRAIN_JOURNAL:
  1961. vdo_drain_recovery_journal(vdo->recovery_journal, VDO_ADMIN_STATE_SAVING,
  1962. completion);
  1963. return;
  1964. case LOAD_PHASE_WAIT_FOR_READ_ONLY:
  1965. /* Avoid an infinite loop */
  1966. completion->error_handler = NULL;
  1967. vdo->admin.phase = LOAD_PHASE_FINISHED;
  1968. vdo_wait_until_not_entering_read_only_mode(completion);
  1969. return;
  1970. default:
  1971. vdo_set_completion_result(completion, UDS_BAD_STATE);
  1972. }
  1973. finish_operation_callback(completion);
  1974. }
  1975. /**
  1976. * handle_load_error() - Handle an error during the load operation.
  1977. * @completion: The admin completion.
  1978. *
  1979. * If at all possible, brings the vdo online in read-only mode. This handler is registered in
  1980. * vdo_preresume_registered().
  1981. */
  1982. static void handle_load_error(struct vdo_completion *completion)
  1983. {
  1984. struct vdo *vdo = completion->vdo;
  1985. if (vdo_requeue_completion_if_needed(completion,
  1986. vdo->thread_config.admin_thread))
  1987. return;
  1988. if (vdo_state_requires_read_only_rebuild(vdo->load_state) &&
  1989. (vdo->admin.phase == LOAD_PHASE_MAKE_DIRTY)) {
  1990. vdo_log_error_strerror(completion->result, "aborting load");
  1991. vdo->admin.phase = LOAD_PHASE_DRAIN_JOURNAL;
  1992. load_callback(vdo_forget(completion));
  1993. return;
  1994. }
  1995. if ((completion->result == VDO_UNSUPPORTED_VERSION) &&
  1996. (vdo->admin.phase == LOAD_PHASE_MAKE_DIRTY)) {
  1997. vdo_log_error("Aborting load due to unsupported version");
  1998. vdo->admin.phase = LOAD_PHASE_FINISHED;
  1999. load_callback(completion);
  2000. return;
  2001. }
  2002. vdo_log_error_strerror(completion->result,
  2003. "Entering read-only mode due to load error");
  2004. vdo->admin.phase = LOAD_PHASE_WAIT_FOR_READ_ONLY;
  2005. vdo_enter_read_only_mode(vdo, completion->result);
  2006. completion->result = VDO_READ_ONLY;
  2007. load_callback(completion);
  2008. }
  2009. /**
  2010. * write_super_block_for_resume() - Update the VDO state and save the super block.
  2011. * @completion: The admin completion
  2012. */
  2013. static void write_super_block_for_resume(struct vdo_completion *completion)
  2014. {
  2015. struct vdo *vdo = completion->vdo;
  2016. switch (vdo_get_state(vdo)) {
  2017. case VDO_CLEAN:
  2018. case VDO_NEW:
  2019. vdo_set_state(vdo, VDO_DIRTY);
  2020. vdo_save_components(vdo, completion);
  2021. return;
  2022. case VDO_DIRTY:
  2023. case VDO_READ_ONLY_MODE:
  2024. case VDO_FORCE_REBUILD:
  2025. case VDO_RECOVERING:
  2026. case VDO_REBUILD_FOR_UPGRADE:
  2027. /* No need to write the super block in these cases */
  2028. vdo_launch_completion(completion);
  2029. return;
  2030. case VDO_REPLAYING:
  2031. default:
  2032. vdo_continue_completion(completion, UDS_BAD_STATE);
  2033. }
  2034. }
  2035. /**
  2036. * resume_callback() - Callback to resume a VDO.
  2037. * @completion: The admin completion.
  2038. */
  2039. static void resume_callback(struct vdo_completion *completion)
  2040. {
  2041. struct vdo *vdo = completion->vdo;
  2042. int result;
  2043. assert_admin_phase_thread(vdo, __func__);
  2044. switch (advance_phase(vdo)) {
  2045. case RESUME_PHASE_START:
  2046. result = vdo_start_operation(&vdo->admin.state,
  2047. VDO_ADMIN_STATE_RESUMING);
  2048. if (result != VDO_SUCCESS) {
  2049. vdo_continue_completion(completion, result);
  2050. return;
  2051. }
  2052. write_super_block_for_resume(completion);
  2053. return;
  2054. case RESUME_PHASE_ALLOW_READ_ONLY_MODE:
  2055. vdo_allow_read_only_mode_entry(completion);
  2056. return;
  2057. case RESUME_PHASE_DEDUPE:
  2058. vdo_resume_hash_zones(vdo->hash_zones, completion);
  2059. return;
  2060. case RESUME_PHASE_DEPOT:
  2061. vdo_resume_slab_depot(vdo->depot, completion);
  2062. return;
  2063. case RESUME_PHASE_JOURNAL:
  2064. vdo_resume_recovery_journal(vdo->recovery_journal, completion);
  2065. return;
  2066. case RESUME_PHASE_BLOCK_MAP:
  2067. vdo_resume_block_map(vdo->block_map, completion);
  2068. return;
  2069. case RESUME_PHASE_LOGICAL_ZONES:
  2070. vdo_resume_logical_zones(vdo->logical_zones, completion);
  2071. return;
  2072. case RESUME_PHASE_PACKER:
  2073. {
  2074. bool was_enabled = vdo_get_compressing(vdo);
  2075. bool enable = vdo->device_config->compression;
  2076. if (enable != was_enabled)
  2077. WRITE_ONCE(vdo->compressing, enable);
  2078. vdo_log_info("compression is %s", (enable ? "enabled" : "disabled"));
  2079. vdo_resume_packer(vdo->packer, completion);
  2080. return;
  2081. }
  2082. case RESUME_PHASE_FLUSHER:
  2083. vdo_resume_flusher(vdo->flusher, completion);
  2084. return;
  2085. case RESUME_PHASE_DATA_VIOS:
  2086. resume_data_vio_pool(vdo->data_vio_pool, completion);
  2087. return;
  2088. case RESUME_PHASE_END:
  2089. break;
  2090. default:
  2091. vdo_set_completion_result(completion, UDS_BAD_STATE);
  2092. }
  2093. finish_operation_callback(completion);
  2094. }
  2095. /**
  2096. * grow_logical_callback() - Callback to initiate a grow logical.
  2097. * @completion: The admin completion.
  2098. *
  2099. * Registered in perform_grow_logical().
  2100. */
  2101. static void grow_logical_callback(struct vdo_completion *completion)
  2102. {
  2103. struct vdo *vdo = completion->vdo;
  2104. int result;
  2105. assert_admin_phase_thread(vdo, __func__);
  2106. switch (advance_phase(vdo)) {
  2107. case GROW_LOGICAL_PHASE_START:
  2108. if (vdo_is_read_only(vdo)) {
  2109. vdo_log_error_strerror(VDO_READ_ONLY,
  2110. "Can't grow logical size of a read-only VDO");
  2111. vdo_set_completion_result(completion, VDO_READ_ONLY);
  2112. break;
  2113. }
  2114. result = vdo_start_operation(&vdo->admin.state,
  2115. VDO_ADMIN_STATE_SUSPENDED_OPERATION);
  2116. if (result != VDO_SUCCESS) {
  2117. vdo_continue_completion(completion, result);
  2118. return;
  2119. }
  2120. vdo->states.vdo.config.logical_blocks = vdo->block_map->next_entry_count;
  2121. vdo_save_components(vdo, completion);
  2122. return;
  2123. case GROW_LOGICAL_PHASE_GROW_BLOCK_MAP:
  2124. vdo_grow_block_map(vdo->block_map, completion);
  2125. return;
  2126. case GROW_LOGICAL_PHASE_END:
  2127. break;
  2128. case GROW_LOGICAL_PHASE_ERROR:
  2129. vdo_enter_read_only_mode(vdo, completion->result);
  2130. break;
  2131. default:
  2132. vdo_set_completion_result(completion, UDS_BAD_STATE);
  2133. }
  2134. finish_operation_callback(completion);
  2135. }
  2136. /**
  2137. * handle_logical_growth_error() - Handle an error during the grow physical process.
  2138. * @completion: The admin completion.
  2139. */
  2140. static void handle_logical_growth_error(struct vdo_completion *completion)
  2141. {
  2142. struct vdo *vdo = completion->vdo;
  2143. if (vdo->admin.phase == GROW_LOGICAL_PHASE_GROW_BLOCK_MAP) {
  2144. /*
  2145. * We've failed to write the new size in the super block, so set our in memory
  2146. * config back to the old size.
  2147. */
  2148. vdo->states.vdo.config.logical_blocks = vdo->block_map->entry_count;
  2149. vdo_abandon_block_map_growth(vdo->block_map);
  2150. }
  2151. vdo->admin.phase = GROW_LOGICAL_PHASE_ERROR;
  2152. grow_logical_callback(completion);
  2153. }
  2154. /**
  2155. * perform_grow_logical() - Grow the logical size of the vdo.
  2156. * @vdo: The vdo to grow.
  2157. * @new_logical_blocks: The size to which the vdo should be grown.
  2158. *
  2159. * Context: This method may only be called when the vdo has been suspended and must not be called
  2160. * from a base thread.
  2161. *
  2162. * Return: VDO_SUCCESS or an error.
  2163. */
  2164. static int perform_grow_logical(struct vdo *vdo, block_count_t new_logical_blocks)
  2165. {
  2166. int result;
  2167. if (vdo->device_config->logical_blocks == new_logical_blocks) {
  2168. /*
  2169. * A table was loaded for which we prepared to grow, but a table without that
  2170. * growth was what we are resuming with.
  2171. */
  2172. vdo_abandon_block_map_growth(vdo->block_map);
  2173. return VDO_SUCCESS;
  2174. }
  2175. vdo_log_info("Resizing logical to %llu",
  2176. (unsigned long long) new_logical_blocks);
  2177. if (vdo->block_map->next_entry_count != new_logical_blocks)
  2178. return VDO_PARAMETER_MISMATCH;
  2179. result = perform_admin_operation(vdo, GROW_LOGICAL_PHASE_START,
  2180. grow_logical_callback,
  2181. handle_logical_growth_error, "grow logical");
  2182. if (result != VDO_SUCCESS)
  2183. return result;
  2184. vdo_log_info("Logical blocks now %llu", (unsigned long long) new_logical_blocks);
  2185. return VDO_SUCCESS;
  2186. }
  2187. static void copy_callback(int read_err, unsigned long write_err, void *context)
  2188. {
  2189. struct vdo_completion *completion = context;
  2190. int result = (((read_err == 0) && (write_err == 0)) ? VDO_SUCCESS : -EIO);
  2191. vdo_continue_completion(completion, result);
  2192. }
  2193. static void partition_to_region(struct partition *partition, struct vdo *vdo,
  2194. struct dm_io_region *region)
  2195. {
  2196. physical_block_number_t pbn = partition->offset - vdo->geometry.bio_offset;
  2197. *region = (struct dm_io_region) {
  2198. .bdev = vdo_get_backing_device(vdo),
  2199. .sector = pbn * VDO_SECTORS_PER_BLOCK,
  2200. .count = partition->count * VDO_SECTORS_PER_BLOCK,
  2201. };
  2202. }
  2203. /**
  2204. * copy_partition() - Copy a partition from the location specified in the current layout to that in
  2205. * the next layout.
  2206. * @vdo: The vdo preparing to grow.
  2207. * @id: The ID of the partition to copy.
  2208. * @parent: The completion to notify when the copy is complete.
  2209. */
  2210. static void copy_partition(struct vdo *vdo, enum partition_id id,
  2211. struct vdo_completion *parent)
  2212. {
  2213. struct dm_io_region read_region, write_regions[1];
  2214. struct partition *from = vdo_get_known_partition(&vdo->layout, id);
  2215. struct partition *to = vdo_get_known_partition(&vdo->next_layout, id);
  2216. partition_to_region(from, vdo, &read_region);
  2217. partition_to_region(to, vdo, &write_regions[0]);
  2218. dm_kcopyd_copy(vdo->partition_copier, &read_region, 1, write_regions, 0,
  2219. copy_callback, parent);
  2220. }
  2221. /**
  2222. * grow_physical_callback() - Callback to initiate a grow physical.
  2223. * @completion: The admin completion.
  2224. *
  2225. * Registered in perform_grow_physical().
  2226. */
  2227. static void grow_physical_callback(struct vdo_completion *completion)
  2228. {
  2229. struct vdo *vdo = completion->vdo;
  2230. int result;
  2231. assert_admin_phase_thread(vdo, __func__);
  2232. switch (advance_phase(vdo)) {
  2233. case GROW_PHYSICAL_PHASE_START:
  2234. if (vdo_is_read_only(vdo)) {
  2235. vdo_log_error_strerror(VDO_READ_ONLY,
  2236. "Can't grow physical size of a read-only VDO");
  2237. vdo_set_completion_result(completion, VDO_READ_ONLY);
  2238. break;
  2239. }
  2240. result = vdo_start_operation(&vdo->admin.state,
  2241. VDO_ADMIN_STATE_SUSPENDED_OPERATION);
  2242. if (result != VDO_SUCCESS) {
  2243. vdo_continue_completion(completion, result);
  2244. return;
  2245. }
  2246. /* Copy the journal into the new layout. */
  2247. copy_partition(vdo, VDO_RECOVERY_JOURNAL_PARTITION, completion);
  2248. return;
  2249. case GROW_PHYSICAL_PHASE_COPY_SUMMARY:
  2250. copy_partition(vdo, VDO_SLAB_SUMMARY_PARTITION, completion);
  2251. return;
  2252. case GROW_PHYSICAL_PHASE_UPDATE_COMPONENTS:
  2253. vdo_uninitialize_layout(&vdo->layout);
  2254. vdo->layout = vdo->next_layout;
  2255. vdo_forget(vdo->next_layout.head);
  2256. vdo->states.vdo.config.physical_blocks = vdo->layout.size;
  2257. vdo_update_slab_depot_size(vdo->depot);
  2258. vdo_save_components(vdo, completion);
  2259. return;
  2260. case GROW_PHYSICAL_PHASE_USE_NEW_SLABS:
  2261. vdo_use_new_slabs(vdo->depot, completion);
  2262. return;
  2263. case GROW_PHYSICAL_PHASE_END:
  2264. vdo->depot->summary_origin =
  2265. vdo_get_known_partition(&vdo->layout,
  2266. VDO_SLAB_SUMMARY_PARTITION)->offset;
  2267. vdo->recovery_journal->origin =
  2268. vdo_get_known_partition(&vdo->layout,
  2269. VDO_RECOVERY_JOURNAL_PARTITION)->offset;
  2270. break;
  2271. case GROW_PHYSICAL_PHASE_ERROR:
  2272. vdo_enter_read_only_mode(vdo, completion->result);
  2273. break;
  2274. default:
  2275. vdo_set_completion_result(completion, UDS_BAD_STATE);
  2276. }
  2277. vdo_uninitialize_layout(&vdo->next_layout);
  2278. finish_operation_callback(completion);
  2279. }
  2280. /**
  2281. * handle_physical_growth_error() - Handle an error during the grow physical process.
  2282. * @completion: The sub-task completion.
  2283. */
  2284. static void handle_physical_growth_error(struct vdo_completion *completion)
  2285. {
  2286. completion->vdo->admin.phase = GROW_PHYSICAL_PHASE_ERROR;
  2287. grow_physical_callback(completion);
  2288. }
  2289. /**
  2290. * perform_grow_physical() - Grow the physical size of the vdo.
  2291. * @vdo: The vdo to resize.
  2292. * @new_physical_blocks: The new physical size in blocks.
  2293. *
  2294. * Context: This method may only be called when the vdo has been suspended and must not be called
  2295. * from a base thread.
  2296. *
  2297. * Return: VDO_SUCCESS or an error.
  2298. */
  2299. static int perform_grow_physical(struct vdo *vdo, block_count_t new_physical_blocks)
  2300. {
  2301. int result;
  2302. block_count_t new_depot_size, prepared_depot_size;
  2303. block_count_t old_physical_blocks = vdo->states.vdo.config.physical_blocks;
  2304. /* Skip any noop grows. */
  2305. if (old_physical_blocks == new_physical_blocks)
  2306. return VDO_SUCCESS;
  2307. if (new_physical_blocks != vdo->next_layout.size) {
  2308. /*
  2309. * Either the VDO isn't prepared to grow, or it was prepared to grow to a different
  2310. * size. Doing this check here relies on the fact that the call to this method is
  2311. * done under the dmsetup message lock.
  2312. */
  2313. vdo_uninitialize_layout(&vdo->next_layout);
  2314. vdo_abandon_new_slabs(vdo->depot);
  2315. return VDO_PARAMETER_MISMATCH;
  2316. }
  2317. /* Validate that we are prepared to grow appropriately. */
  2318. new_depot_size =
  2319. vdo_get_known_partition(&vdo->next_layout, VDO_SLAB_DEPOT_PARTITION)->count;
  2320. prepared_depot_size = (vdo->depot->new_slabs == NULL) ? 0 : vdo->depot->new_size;
  2321. if (prepared_depot_size != new_depot_size)
  2322. return VDO_PARAMETER_MISMATCH;
  2323. result = perform_admin_operation(vdo, GROW_PHYSICAL_PHASE_START,
  2324. grow_physical_callback,
  2325. handle_physical_growth_error, "grow physical");
  2326. if (result != VDO_SUCCESS)
  2327. return result;
  2328. vdo_log_info("Physical block count was %llu, now %llu",
  2329. (unsigned long long) old_physical_blocks,
  2330. (unsigned long long) new_physical_blocks);
  2331. return VDO_SUCCESS;
  2332. }
  2333. /**
  2334. * apply_new_vdo_configuration() - Attempt to make any configuration changes from the table being
  2335. * resumed.
  2336. * @vdo: The vdo being resumed.
  2337. * @config: The new device configuration derived from the table with which the vdo is being
  2338. * resumed.
  2339. *
  2340. * Return: VDO_SUCCESS or an error.
  2341. */
  2342. static int __must_check apply_new_vdo_configuration(struct vdo *vdo,
  2343. struct device_config *config)
  2344. {
  2345. int result;
  2346. result = perform_grow_logical(vdo, config->logical_blocks);
  2347. if (result != VDO_SUCCESS) {
  2348. vdo_log_error("grow logical operation failed, result = %d", result);
  2349. return result;
  2350. }
  2351. result = perform_grow_physical(vdo, config->physical_blocks);
  2352. if (result != VDO_SUCCESS)
  2353. vdo_log_error("resize operation failed, result = %d", result);
  2354. return result;
  2355. }
  2356. static int vdo_preresume_registered(struct dm_target *ti, struct vdo *vdo)
  2357. {
  2358. struct device_config *config = ti->private;
  2359. const char *device_name = vdo_get_device_name(ti);
  2360. block_count_t backing_blocks;
  2361. int result;
  2362. backing_blocks = get_underlying_device_block_count(vdo);
  2363. if (backing_blocks < config->physical_blocks) {
  2364. /* FIXME: can this still happen? */
  2365. vdo_log_error("resume of device '%s' failed: backing device has %llu blocks but VDO physical size is %llu blocks",
  2366. device_name, (unsigned long long) backing_blocks,
  2367. (unsigned long long) config->physical_blocks);
  2368. return -EINVAL;
  2369. }
  2370. if (vdo_get_admin_state(vdo) == VDO_ADMIN_STATE_PRE_LOADED) {
  2371. vdo_log_info("starting device '%s'", device_name);
  2372. result = perform_admin_operation(vdo, LOAD_PHASE_START, load_callback,
  2373. handle_load_error, "load");
  2374. if (result == VDO_UNSUPPORTED_VERSION) {
  2375. /*
  2376. * A component version is not supported. This can happen when the
  2377. * recovery journal metadata is in an old version format. Abort the
  2378. * load without saving the state.
  2379. */
  2380. vdo->suspend_type = VDO_ADMIN_STATE_SUSPENDING;
  2381. perform_admin_operation(vdo, SUSPEND_PHASE_START,
  2382. suspend_callback, suspend_callback,
  2383. "suspend");
  2384. return result;
  2385. }
  2386. if ((result != VDO_SUCCESS) && (result != VDO_READ_ONLY)) {
  2387. /*
  2388. * Something has gone very wrong. Make sure everything has drained and
  2389. * leave the device in an unresumable state.
  2390. */
  2391. vdo_log_error_strerror(result,
  2392. "Start failed, could not load VDO metadata");
  2393. vdo->suspend_type = VDO_ADMIN_STATE_STOPPING;
  2394. perform_admin_operation(vdo, SUSPEND_PHASE_START,
  2395. suspend_callback, suspend_callback,
  2396. "suspend");
  2397. return result;
  2398. }
  2399. /* Even if the VDO is read-only, it is now able to handle read requests. */
  2400. vdo_log_info("device '%s' started", device_name);
  2401. }
  2402. vdo_log_info("resuming device '%s'", device_name);
  2403. /* If this fails, the VDO was not in a state to be resumed. This should never happen. */
  2404. result = apply_new_vdo_configuration(vdo, config);
  2405. BUG_ON(result == VDO_INVALID_ADMIN_STATE);
  2406. /*
  2407. * Now that we've tried to modify the vdo, the new config *is* the config, whether the
  2408. * modifications worked or not.
  2409. */
  2410. vdo->device_config = config;
  2411. /*
  2412. * Any error here is highly unexpected and the state of the vdo is questionable, so we mark
  2413. * it read-only in memory. Because we are suspended, the read-only state will not be
  2414. * written to disk.
  2415. */
  2416. if (result != VDO_SUCCESS) {
  2417. vdo_log_error_strerror(result,
  2418. "Commit of modifications to device '%s' failed",
  2419. device_name);
  2420. vdo_enter_read_only_mode(vdo, result);
  2421. return result;
  2422. }
  2423. if (vdo_get_admin_state(vdo)->normal) {
  2424. /* The VDO was just started, so we don't need to resume it. */
  2425. return VDO_SUCCESS;
  2426. }
  2427. result = perform_admin_operation(vdo, RESUME_PHASE_START, resume_callback,
  2428. resume_callback, "resume");
  2429. BUG_ON(result == VDO_INVALID_ADMIN_STATE);
  2430. if (result == VDO_READ_ONLY) {
  2431. /* Even if the vdo is read-only, it has still resumed. */
  2432. result = VDO_SUCCESS;
  2433. }
  2434. if (result != VDO_SUCCESS)
  2435. vdo_log_error("resume of device '%s' failed with error: %d", device_name,
  2436. result);
  2437. return result;
  2438. }
  2439. static int vdo_preresume(struct dm_target *ti)
  2440. {
  2441. struct registered_thread instance_thread;
  2442. struct vdo *vdo = get_vdo_for_target(ti);
  2443. int result;
  2444. vdo_register_thread_device_id(&instance_thread, &vdo->instance);
  2445. result = vdo_preresume_registered(ti, vdo);
  2446. if ((result == VDO_PARAMETER_MISMATCH) || (result == VDO_INVALID_ADMIN_STATE) ||
  2447. (result == VDO_UNSUPPORTED_VERSION))
  2448. result = -EINVAL;
  2449. vdo_unregister_thread_device_id();
  2450. return vdo_status_to_errno(result);
  2451. }
  2452. static void vdo_resume(struct dm_target *ti)
  2453. {
  2454. struct registered_thread instance_thread;
  2455. vdo_register_thread_device_id(&instance_thread,
  2456. &get_vdo_for_target(ti)->instance);
  2457. vdo_log_info("device '%s' resumed", vdo_get_device_name(ti));
  2458. vdo_unregister_thread_device_id();
  2459. }
  2460. /*
  2461. * If anything changes that affects how user tools will interact with vdo, update the version
  2462. * number and make sure documentation about the change is complete so tools can properly update
  2463. * their management code.
  2464. */
  2465. static struct target_type vdo_target_bio = {
  2466. .features = DM_TARGET_SINGLETON,
  2467. .name = "vdo",
  2468. .version = { 9, 1, 0 },
  2469. .module = THIS_MODULE,
  2470. .ctr = vdo_ctr,
  2471. .dtr = vdo_dtr,
  2472. .io_hints = vdo_io_hints,
  2473. .iterate_devices = vdo_iterate_devices,
  2474. .map = vdo_map_bio,
  2475. .message = vdo_message,
  2476. .status = vdo_status,
  2477. .presuspend = vdo_presuspend,
  2478. .postsuspend = vdo_postsuspend,
  2479. .preresume = vdo_preresume,
  2480. .resume = vdo_resume,
  2481. };
  2482. static bool dm_registered;
  2483. static void vdo_module_destroy(void)
  2484. {
  2485. vdo_log_debug("unloading");
  2486. if (dm_registered)
  2487. dm_unregister_target(&vdo_target_bio);
  2488. VDO_ASSERT_LOG_ONLY(instances.count == 0,
  2489. "should have no instance numbers still in use, but have %u",
  2490. instances.count);
  2491. vdo_free(instances.words);
  2492. memset(&instances, 0, sizeof(struct instance_tracker));
  2493. }
  2494. static int __init vdo_init(void)
  2495. {
  2496. int result = 0;
  2497. /* Memory tracking must be initialized first for accurate accounting. */
  2498. vdo_memory_init();
  2499. vdo_initialize_threads_mutex();
  2500. vdo_initialize_thread_device_registry();
  2501. vdo_initialize_device_registry_once();
  2502. /* Add VDO errors to the set of errors registered by the indexer. */
  2503. result = vdo_register_status_codes();
  2504. if (result != VDO_SUCCESS) {
  2505. vdo_log_error("vdo_register_status_codes failed %d", result);
  2506. vdo_module_destroy();
  2507. return result;
  2508. }
  2509. result = dm_register_target(&vdo_target_bio);
  2510. if (result < 0) {
  2511. vdo_log_error("dm_register_target failed %d", result);
  2512. vdo_module_destroy();
  2513. return result;
  2514. }
  2515. dm_registered = true;
  2516. return result;
  2517. }
  2518. static void __exit vdo_exit(void)
  2519. {
  2520. vdo_module_destroy();
  2521. /* Memory tracking cleanup must be done last. */
  2522. vdo_memory_exit();
  2523. }
  2524. module_init(vdo_init);
  2525. module_exit(vdo_exit);
  2526. module_param_named(log_level, vdo_log_level, uint, 0644);
  2527. MODULE_PARM_DESC(log_level, "Log level for log messages");
  2528. MODULE_DESCRIPTION(DM_NAME " target for transparent deduplication");
  2529. MODULE_AUTHOR("Red Hat, Inc.");
  2530. MODULE_LICENSE("GPL");