vdo.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. /*
  6. * This file contains the main entry points for normal operations on a vdo as well as functions for
  7. * constructing and destroying vdo instances (in memory).
  8. */
  9. /**
  10. * DOC:
  11. *
  12. * A read_only_notifier has a single completion which is used to perform read-only notifications,
  13. * however, vdo_enter_read_only_mode() may be called from any thread. A pair of fields, protected
  14. * by a spinlock, are used to control the read-only mode entry process. The first field holds the
  15. * read-only error. The second is the state field, which may hold any of the four special values
  16. * enumerated here.
  17. *
  18. * When vdo_enter_read_only_mode() is called from some vdo thread, if the read_only_error field
  19. * already contains an error (i.e. its value is not VDO_SUCCESS), then some other error has already
  20. * initiated the read-only process, and nothing more is done. Otherwise, the new error is stored in
  21. * the read_only_error field, and the state field is consulted. If the state is MAY_NOTIFY, it is
  22. * set to NOTIFYING, and the notification process begins. If the state is MAY_NOT_NOTIFY, then
  23. * notifications are currently disallowed, generally due to the vdo being suspended. In this case,
  24. * the nothing more will be done until the vdo is resumed, at which point the notification will be
  25. * performed. In any other case, the vdo is already read-only, and there is nothing more to do.
  26. */
  27. #include "vdo.h"
  28. #include <linux/completion.h>
  29. #include <linux/device-mapper.h>
  30. #include <linux/lz4.h>
  31. #include <linux/mutex.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/types.h>
  34. #include "logger.h"
  35. #include "memory-alloc.h"
  36. #include "permassert.h"
  37. #include "string-utils.h"
  38. #include "block-map.h"
  39. #include "completion.h"
  40. #include "data-vio.h"
  41. #include "dedupe.h"
  42. #include "encodings.h"
  43. #include "funnel-workqueue.h"
  44. #include "io-submitter.h"
  45. #include "logical-zone.h"
  46. #include "packer.h"
  47. #include "physical-zone.h"
  48. #include "recovery-journal.h"
  49. #include "slab-depot.h"
  50. #include "statistics.h"
  51. #include "status-codes.h"
  52. #include "vio.h"
  53. #define PARANOID_THREAD_CONSISTENCY_CHECKS 0
  54. struct sync_completion {
  55. struct vdo_completion vdo_completion;
  56. struct completion completion;
  57. };
  58. /* A linked list is adequate for the small number of entries we expect. */
  59. struct device_registry {
  60. struct list_head links;
  61. /* TODO: Convert to rcu per kernel recommendation. */
  62. rwlock_t lock;
  63. };
  64. static struct device_registry registry;
  65. /**
  66. * vdo_initialize_device_registry_once() - Initialize the necessary structures for the device
  67. * registry.
  68. */
  69. void vdo_initialize_device_registry_once(void)
  70. {
  71. INIT_LIST_HEAD(&registry.links);
  72. rwlock_init(&registry.lock);
  73. }
  74. /** vdo_is_equal() - Implements vdo_filter_fn. */
  75. static bool vdo_is_equal(struct vdo *vdo, const void *context)
  76. {
  77. return (vdo == context);
  78. }
  79. /**
  80. * filter_vdos_locked() - Find a vdo in the registry if it exists there.
  81. * @filter: The filter function to apply to devices.
  82. * @context: A bit of context to provide the filter.
  83. *
  84. * Context: Must be called holding the lock.
  85. *
  86. * Return: the vdo object found, if any.
  87. */
  88. static struct vdo * __must_check filter_vdos_locked(vdo_filter_fn filter,
  89. const void *context)
  90. {
  91. struct vdo *vdo;
  92. list_for_each_entry(vdo, &registry.links, registration) {
  93. if (filter(vdo, context))
  94. return vdo;
  95. }
  96. return NULL;
  97. }
  98. /**
  99. * vdo_find_matching() - Find and return the first (if any) vdo matching a given filter function.
  100. * @filter: The filter function to apply to vdos.
  101. * @context: A bit of context to provide the filter.
  102. */
  103. struct vdo *vdo_find_matching(vdo_filter_fn filter, const void *context)
  104. {
  105. struct vdo *vdo;
  106. read_lock(&registry.lock);
  107. vdo = filter_vdos_locked(filter, context);
  108. read_unlock(&registry.lock);
  109. return vdo;
  110. }
  111. static void start_vdo_request_queue(void *ptr)
  112. {
  113. struct vdo_thread *thread = vdo_get_work_queue_owner(vdo_get_current_work_queue());
  114. vdo_register_allocating_thread(&thread->allocating_thread,
  115. &thread->vdo->allocations_allowed);
  116. }
  117. static void finish_vdo_request_queue(void *ptr)
  118. {
  119. vdo_unregister_allocating_thread();
  120. }
  121. static const struct vdo_work_queue_type default_queue_type = {
  122. .start = start_vdo_request_queue,
  123. .finish = finish_vdo_request_queue,
  124. .max_priority = VDO_DEFAULT_Q_MAX_PRIORITY,
  125. .default_priority = VDO_DEFAULT_Q_COMPLETION_PRIORITY,
  126. };
  127. static const struct vdo_work_queue_type bio_ack_q_type = {
  128. .start = NULL,
  129. .finish = NULL,
  130. .max_priority = BIO_ACK_Q_MAX_PRIORITY,
  131. .default_priority = BIO_ACK_Q_ACK_PRIORITY,
  132. };
  133. static const struct vdo_work_queue_type cpu_q_type = {
  134. .start = NULL,
  135. .finish = NULL,
  136. .max_priority = CPU_Q_MAX_PRIORITY,
  137. .default_priority = CPU_Q_MAX_PRIORITY,
  138. };
  139. static void uninitialize_thread_config(struct thread_config *config)
  140. {
  141. vdo_free(vdo_forget(config->logical_threads));
  142. vdo_free(vdo_forget(config->physical_threads));
  143. vdo_free(vdo_forget(config->hash_zone_threads));
  144. vdo_free(vdo_forget(config->bio_threads));
  145. memset(config, 0, sizeof(struct thread_config));
  146. }
  147. static void assign_thread_ids(struct thread_config *config,
  148. thread_id_t thread_ids[], zone_count_t count)
  149. {
  150. zone_count_t zone;
  151. for (zone = 0; zone < count; zone++)
  152. thread_ids[zone] = config->thread_count++;
  153. }
  154. /**
  155. * initialize_thread_config() - Initialize the thread mapping
  156. *
  157. * If the logical, physical, and hash zone counts are all 0, a single thread will be shared by all
  158. * three plus the packer and recovery journal. Otherwise, there must be at least one of each type,
  159. * and each will have its own thread, as will the packer and recovery journal.
  160. *
  161. * Return: VDO_SUCCESS or an error.
  162. */
  163. static int __must_check initialize_thread_config(struct thread_count_config counts,
  164. struct thread_config *config)
  165. {
  166. int result;
  167. bool single = ((counts.logical_zones + counts.physical_zones + counts.hash_zones) == 0);
  168. config->bio_thread_count = counts.bio_threads;
  169. if (single) {
  170. config->logical_zone_count = 1;
  171. config->physical_zone_count = 1;
  172. config->hash_zone_count = 1;
  173. } else {
  174. config->logical_zone_count = counts.logical_zones;
  175. config->physical_zone_count = counts.physical_zones;
  176. config->hash_zone_count = counts.hash_zones;
  177. }
  178. result = vdo_allocate(config->logical_zone_count, thread_id_t,
  179. "logical thread array", &config->logical_threads);
  180. if (result != VDO_SUCCESS) {
  181. uninitialize_thread_config(config);
  182. return result;
  183. }
  184. result = vdo_allocate(config->physical_zone_count, thread_id_t,
  185. "physical thread array", &config->physical_threads);
  186. if (result != VDO_SUCCESS) {
  187. uninitialize_thread_config(config);
  188. return result;
  189. }
  190. result = vdo_allocate(config->hash_zone_count, thread_id_t,
  191. "hash thread array", &config->hash_zone_threads);
  192. if (result != VDO_SUCCESS) {
  193. uninitialize_thread_config(config);
  194. return result;
  195. }
  196. result = vdo_allocate(config->bio_thread_count, thread_id_t,
  197. "bio thread array", &config->bio_threads);
  198. if (result != VDO_SUCCESS) {
  199. uninitialize_thread_config(config);
  200. return result;
  201. }
  202. if (single) {
  203. config->logical_threads[0] = config->thread_count;
  204. config->physical_threads[0] = config->thread_count;
  205. config->hash_zone_threads[0] = config->thread_count++;
  206. } else {
  207. config->admin_thread = config->thread_count;
  208. config->journal_thread = config->thread_count++;
  209. config->packer_thread = config->thread_count++;
  210. assign_thread_ids(config, config->logical_threads, counts.logical_zones);
  211. assign_thread_ids(config, config->physical_threads, counts.physical_zones);
  212. assign_thread_ids(config, config->hash_zone_threads, counts.hash_zones);
  213. }
  214. config->dedupe_thread = config->thread_count++;
  215. config->bio_ack_thread =
  216. ((counts.bio_ack_threads > 0) ? config->thread_count++ : VDO_INVALID_THREAD_ID);
  217. config->cpu_thread = config->thread_count++;
  218. assign_thread_ids(config, config->bio_threads, counts.bio_threads);
  219. return VDO_SUCCESS;
  220. }
  221. /**
  222. * read_geometry_block() - Synchronously read the geometry block from a vdo's underlying block
  223. * device.
  224. * @vdo: The vdo whose geometry is to be read.
  225. *
  226. * Return: VDO_SUCCESS or an error code.
  227. */
  228. static int __must_check read_geometry_block(struct vdo *vdo)
  229. {
  230. struct vio *vio;
  231. char *block;
  232. int result;
  233. result = vdo_allocate(VDO_BLOCK_SIZE, u8, __func__, &block);
  234. if (result != VDO_SUCCESS)
  235. return result;
  236. result = create_metadata_vio(vdo, VIO_TYPE_GEOMETRY, VIO_PRIORITY_HIGH, NULL,
  237. block, &vio);
  238. if (result != VDO_SUCCESS) {
  239. vdo_free(block);
  240. return result;
  241. }
  242. /*
  243. * This is only safe because, having not already loaded the geometry, the vdo's geometry's
  244. * bio_offset field is 0, so the fact that vio_reset_bio() will subtract that offset from
  245. * the supplied pbn is not a problem.
  246. */
  247. result = vio_reset_bio(vio, block, NULL, REQ_OP_READ,
  248. VDO_GEOMETRY_BLOCK_LOCATION);
  249. if (result != VDO_SUCCESS) {
  250. free_vio(vdo_forget(vio));
  251. vdo_free(block);
  252. return result;
  253. }
  254. bio_set_dev(vio->bio, vdo_get_backing_device(vdo));
  255. submit_bio_wait(vio->bio);
  256. result = blk_status_to_errno(vio->bio->bi_status);
  257. free_vio(vdo_forget(vio));
  258. if (result != 0) {
  259. vdo_log_error_strerror(result, "synchronous read failed");
  260. vdo_free(block);
  261. return -EIO;
  262. }
  263. result = vdo_parse_geometry_block((u8 *) block, &vdo->geometry);
  264. vdo_free(block);
  265. return result;
  266. }
  267. static bool get_zone_thread_name(const thread_id_t thread_ids[], zone_count_t count,
  268. thread_id_t id, const char *prefix,
  269. char *buffer, size_t buffer_length)
  270. {
  271. if (id >= thread_ids[0]) {
  272. thread_id_t index = id - thread_ids[0];
  273. if (index < count) {
  274. snprintf(buffer, buffer_length, "%s%d", prefix, index);
  275. return true;
  276. }
  277. }
  278. return false;
  279. }
  280. /**
  281. * get_thread_name() - Format the name of the worker thread desired to support a given work queue.
  282. * @thread_config: The thread configuration.
  283. * @thread_id: The thread id.
  284. * @buffer: Where to put the formatted name.
  285. * @buffer_length: Size of the output buffer.
  286. *
  287. * The physical layer may add a prefix identifying the product; the output from this function
  288. * should just identify the thread.
  289. */
  290. static void get_thread_name(const struct thread_config *thread_config,
  291. thread_id_t thread_id, char *buffer, size_t buffer_length)
  292. {
  293. if (thread_id == thread_config->journal_thread) {
  294. if (thread_config->packer_thread == thread_id) {
  295. /*
  296. * This is the "single thread" config where one thread is used for the
  297. * journal, packer, logical, physical, and hash zones. In that case, it is
  298. * known as the "request queue."
  299. */
  300. snprintf(buffer, buffer_length, "reqQ");
  301. return;
  302. }
  303. snprintf(buffer, buffer_length, "journalQ");
  304. return;
  305. } else if (thread_id == thread_config->admin_thread) {
  306. /* Theoretically this could be different from the journal thread. */
  307. snprintf(buffer, buffer_length, "adminQ");
  308. return;
  309. } else if (thread_id == thread_config->packer_thread) {
  310. snprintf(buffer, buffer_length, "packerQ");
  311. return;
  312. } else if (thread_id == thread_config->dedupe_thread) {
  313. snprintf(buffer, buffer_length, "dedupeQ");
  314. return;
  315. } else if (thread_id == thread_config->bio_ack_thread) {
  316. snprintf(buffer, buffer_length, "ackQ");
  317. return;
  318. } else if (thread_id == thread_config->cpu_thread) {
  319. snprintf(buffer, buffer_length, "cpuQ");
  320. return;
  321. }
  322. if (get_zone_thread_name(thread_config->logical_threads,
  323. thread_config->logical_zone_count,
  324. thread_id, "logQ", buffer, buffer_length))
  325. return;
  326. if (get_zone_thread_name(thread_config->physical_threads,
  327. thread_config->physical_zone_count,
  328. thread_id, "physQ", buffer, buffer_length))
  329. return;
  330. if (get_zone_thread_name(thread_config->hash_zone_threads,
  331. thread_config->hash_zone_count,
  332. thread_id, "hashQ", buffer, buffer_length))
  333. return;
  334. if (get_zone_thread_name(thread_config->bio_threads,
  335. thread_config->bio_thread_count,
  336. thread_id, "bioQ", buffer, buffer_length))
  337. return;
  338. /* Some sort of misconfiguration? */
  339. snprintf(buffer, buffer_length, "reqQ%d", thread_id);
  340. }
  341. /**
  342. * vdo_make_thread() - Construct a single vdo work_queue and its associated thread (or threads for
  343. * round-robin queues).
  344. * @vdo: The vdo which owns the thread.
  345. * @thread_id: The id of the thread to create (as determined by the thread_config).
  346. * @type: The description of the work queue for this thread.
  347. * @queue_count: The number of actual threads/queues contained in the "thread".
  348. * @contexts: An array of queue_count contexts, one for each individual queue; may be NULL.
  349. *
  350. * Each "thread" constructed by this method is represented by a unique thread id in the thread
  351. * config, and completions can be enqueued to the queue and run on the threads comprising this
  352. * entity.
  353. *
  354. * Return: VDO_SUCCESS or an error.
  355. */
  356. int vdo_make_thread(struct vdo *vdo, thread_id_t thread_id,
  357. const struct vdo_work_queue_type *type,
  358. unsigned int queue_count, void *contexts[])
  359. {
  360. struct vdo_thread *thread = &vdo->threads[thread_id];
  361. char queue_name[MAX_VDO_WORK_QUEUE_NAME_LEN];
  362. if (type == NULL)
  363. type = &default_queue_type;
  364. if (thread->queue != NULL) {
  365. return VDO_ASSERT(vdo_work_queue_type_is(thread->queue, type),
  366. "already constructed vdo thread %u is of the correct type",
  367. thread_id);
  368. }
  369. thread->vdo = vdo;
  370. thread->thread_id = thread_id;
  371. get_thread_name(&vdo->thread_config, thread_id, queue_name, sizeof(queue_name));
  372. return vdo_make_work_queue(vdo->thread_name_prefix, queue_name, thread,
  373. type, queue_count, contexts, &thread->queue);
  374. }
  375. /**
  376. * register_vdo() - Register a VDO; it must not already be registered.
  377. * @vdo: The vdo to register.
  378. *
  379. * Return: VDO_SUCCESS or an error.
  380. */
  381. static int register_vdo(struct vdo *vdo)
  382. {
  383. int result;
  384. write_lock(&registry.lock);
  385. result = VDO_ASSERT(filter_vdos_locked(vdo_is_equal, vdo) == NULL,
  386. "VDO not already registered");
  387. if (result == VDO_SUCCESS) {
  388. INIT_LIST_HEAD(&vdo->registration);
  389. list_add_tail(&vdo->registration, &registry.links);
  390. }
  391. write_unlock(&registry.lock);
  392. return result;
  393. }
  394. /**
  395. * initialize_vdo() - Do the portion of initializing a vdo which will clean up after itself on
  396. * error.
  397. * @vdo: The vdo being initialized
  398. * @config: The configuration of the vdo
  399. * @instance: The instance number of the vdo
  400. * @reason: The buffer to hold the failure reason on error
  401. */
  402. static int initialize_vdo(struct vdo *vdo, struct device_config *config,
  403. unsigned int instance, char **reason)
  404. {
  405. int result;
  406. zone_count_t i;
  407. vdo->device_config = config;
  408. vdo->starting_sector_offset = config->owning_target->begin;
  409. vdo->instance = instance;
  410. vdo->allocations_allowed = true;
  411. vdo_set_admin_state_code(&vdo->admin.state, VDO_ADMIN_STATE_NEW);
  412. INIT_LIST_HEAD(&vdo->device_config_list);
  413. vdo_initialize_completion(&vdo->admin.completion, vdo, VDO_ADMIN_COMPLETION);
  414. init_completion(&vdo->admin.callback_sync);
  415. mutex_init(&vdo->stats_mutex);
  416. result = read_geometry_block(vdo);
  417. if (result != VDO_SUCCESS) {
  418. *reason = "Could not load geometry block";
  419. return result;
  420. }
  421. result = initialize_thread_config(config->thread_counts, &vdo->thread_config);
  422. if (result != VDO_SUCCESS) {
  423. *reason = "Cannot create thread configuration";
  424. return result;
  425. }
  426. vdo_log_info("zones: %d logical, %d physical, %d hash; total threads: %d",
  427. config->thread_counts.logical_zones,
  428. config->thread_counts.physical_zones,
  429. config->thread_counts.hash_zones, vdo->thread_config.thread_count);
  430. /* Compression context storage */
  431. result = vdo_allocate(config->thread_counts.cpu_threads, char *, "LZ4 context",
  432. &vdo->compression_context);
  433. if (result != VDO_SUCCESS) {
  434. *reason = "cannot allocate LZ4 context";
  435. return result;
  436. }
  437. for (i = 0; i < config->thread_counts.cpu_threads; i++) {
  438. result = vdo_allocate(LZ4_MEM_COMPRESS, char, "LZ4 context",
  439. &vdo->compression_context[i]);
  440. if (result != VDO_SUCCESS) {
  441. *reason = "cannot allocate LZ4 context";
  442. return result;
  443. }
  444. }
  445. result = register_vdo(vdo);
  446. if (result != VDO_SUCCESS) {
  447. *reason = "Cannot add VDO to device registry";
  448. return result;
  449. }
  450. vdo_set_admin_state_code(&vdo->admin.state, VDO_ADMIN_STATE_INITIALIZED);
  451. return result;
  452. }
  453. /**
  454. * vdo_make() - Allocate and initialize a vdo.
  455. * @instance: Device instantiation counter.
  456. * @config: The device configuration.
  457. * @reason: The reason for any failure during this call.
  458. * @vdo_ptr: A pointer to hold the created vdo.
  459. *
  460. * Return: VDO_SUCCESS or an error.
  461. */
  462. int vdo_make(unsigned int instance, struct device_config *config, char **reason,
  463. struct vdo **vdo_ptr)
  464. {
  465. int result;
  466. struct vdo *vdo;
  467. /* Initialize with a generic failure reason to prevent returning garbage. */
  468. *reason = "Unspecified error";
  469. result = vdo_allocate(1, struct vdo, __func__, &vdo);
  470. if (result != VDO_SUCCESS) {
  471. *reason = "Cannot allocate VDO";
  472. return result;
  473. }
  474. result = initialize_vdo(vdo, config, instance, reason);
  475. if (result != VDO_SUCCESS) {
  476. vdo_destroy(vdo);
  477. return result;
  478. }
  479. /* From here on, the caller will clean up if there is an error. */
  480. *vdo_ptr = vdo;
  481. snprintf(vdo->thread_name_prefix, sizeof(vdo->thread_name_prefix),
  482. "vdo%u", instance);
  483. result = vdo_allocate(vdo->thread_config.thread_count,
  484. struct vdo_thread, __func__, &vdo->threads);
  485. if (result != VDO_SUCCESS) {
  486. *reason = "Cannot allocate thread structures";
  487. return result;
  488. }
  489. result = vdo_make_thread(vdo, vdo->thread_config.admin_thread,
  490. &default_queue_type, 1, NULL);
  491. if (result != VDO_SUCCESS) {
  492. *reason = "Cannot make admin thread";
  493. return result;
  494. }
  495. result = vdo_make_flusher(vdo);
  496. if (result != VDO_SUCCESS) {
  497. *reason = "Cannot make flusher zones";
  498. return result;
  499. }
  500. result = vdo_make_packer(vdo, DEFAULT_PACKER_BINS, &vdo->packer);
  501. if (result != VDO_SUCCESS) {
  502. *reason = "Cannot make packer zones";
  503. return result;
  504. }
  505. BUG_ON(vdo->device_config->logical_block_size <= 0);
  506. BUG_ON(vdo->device_config->owned_device == NULL);
  507. result = make_data_vio_pool(vdo, MAXIMUM_VDO_USER_VIOS,
  508. MAXIMUM_VDO_USER_VIOS * 3 / 4,
  509. &vdo->data_vio_pool);
  510. if (result != VDO_SUCCESS) {
  511. *reason = "Cannot allocate data_vio pool";
  512. return result;
  513. }
  514. result = vdo_make_io_submitter(config->thread_counts.bio_threads,
  515. config->thread_counts.bio_rotation_interval,
  516. get_data_vio_pool_request_limit(vdo->data_vio_pool),
  517. vdo, &vdo->io_submitter);
  518. if (result != VDO_SUCCESS) {
  519. *reason = "bio submission initialization failed";
  520. return result;
  521. }
  522. if (vdo_uses_bio_ack_queue(vdo)) {
  523. result = vdo_make_thread(vdo, vdo->thread_config.bio_ack_thread,
  524. &bio_ack_q_type,
  525. config->thread_counts.bio_ack_threads, NULL);
  526. if (result != VDO_SUCCESS) {
  527. *reason = "bio ack queue initialization failed";
  528. return result;
  529. }
  530. }
  531. result = vdo_make_thread(vdo, vdo->thread_config.cpu_thread, &cpu_q_type,
  532. config->thread_counts.cpu_threads,
  533. (void **) vdo->compression_context);
  534. if (result != VDO_SUCCESS) {
  535. *reason = "CPU queue initialization failed";
  536. return result;
  537. }
  538. return VDO_SUCCESS;
  539. }
  540. static void finish_vdo(struct vdo *vdo)
  541. {
  542. int i;
  543. if (vdo->threads == NULL)
  544. return;
  545. vdo_cleanup_io_submitter(vdo->io_submitter);
  546. vdo_finish_dedupe_index(vdo->hash_zones);
  547. for (i = 0; i < vdo->thread_config.thread_count; i++)
  548. vdo_finish_work_queue(vdo->threads[i].queue);
  549. }
  550. /**
  551. * free_listeners() - Free the list of read-only listeners associated with a thread.
  552. * @thread_data: The thread holding the list to free.
  553. */
  554. static void free_listeners(struct vdo_thread *thread)
  555. {
  556. struct read_only_listener *listener, *next;
  557. for (listener = vdo_forget(thread->listeners); listener != NULL; listener = next) {
  558. next = vdo_forget(listener->next);
  559. vdo_free(listener);
  560. }
  561. }
  562. static void uninitialize_super_block(struct vdo_super_block *super_block)
  563. {
  564. free_vio_components(&super_block->vio);
  565. vdo_free(super_block->buffer);
  566. }
  567. /**
  568. * unregister_vdo() - Remove a vdo from the device registry.
  569. * @vdo: The vdo to remove.
  570. */
  571. static void unregister_vdo(struct vdo *vdo)
  572. {
  573. write_lock(&registry.lock);
  574. if (filter_vdos_locked(vdo_is_equal, vdo) == vdo)
  575. list_del_init(&vdo->registration);
  576. write_unlock(&registry.lock);
  577. }
  578. /**
  579. * vdo_destroy() - Destroy a vdo instance.
  580. * @vdo: The vdo to destroy (may be NULL).
  581. */
  582. void vdo_destroy(struct vdo *vdo)
  583. {
  584. unsigned int i;
  585. if (vdo == NULL)
  586. return;
  587. /* A running VDO should never be destroyed without suspending first. */
  588. BUG_ON(vdo_get_admin_state(vdo)->normal);
  589. vdo->allocations_allowed = true;
  590. finish_vdo(vdo);
  591. unregister_vdo(vdo);
  592. free_data_vio_pool(vdo->data_vio_pool);
  593. vdo_free_io_submitter(vdo_forget(vdo->io_submitter));
  594. vdo_free_flusher(vdo_forget(vdo->flusher));
  595. vdo_free_packer(vdo_forget(vdo->packer));
  596. vdo_free_recovery_journal(vdo_forget(vdo->recovery_journal));
  597. vdo_free_slab_depot(vdo_forget(vdo->depot));
  598. vdo_uninitialize_layout(&vdo->layout);
  599. vdo_uninitialize_layout(&vdo->next_layout);
  600. if (vdo->partition_copier)
  601. dm_kcopyd_client_destroy(vdo_forget(vdo->partition_copier));
  602. uninitialize_super_block(&vdo->super_block);
  603. vdo_free_block_map(vdo_forget(vdo->block_map));
  604. vdo_free_hash_zones(vdo_forget(vdo->hash_zones));
  605. vdo_free_physical_zones(vdo_forget(vdo->physical_zones));
  606. vdo_free_logical_zones(vdo_forget(vdo->logical_zones));
  607. if (vdo->threads != NULL) {
  608. for (i = 0; i < vdo->thread_config.thread_count; i++) {
  609. free_listeners(&vdo->threads[i]);
  610. vdo_free_work_queue(vdo_forget(vdo->threads[i].queue));
  611. }
  612. vdo_free(vdo_forget(vdo->threads));
  613. }
  614. uninitialize_thread_config(&vdo->thread_config);
  615. if (vdo->compression_context != NULL) {
  616. for (i = 0; i < vdo->device_config->thread_counts.cpu_threads; i++)
  617. vdo_free(vdo_forget(vdo->compression_context[i]));
  618. vdo_free(vdo_forget(vdo->compression_context));
  619. }
  620. vdo_free(vdo);
  621. }
  622. static int initialize_super_block(struct vdo *vdo, struct vdo_super_block *super_block)
  623. {
  624. int result;
  625. result = vdo_allocate(VDO_BLOCK_SIZE, char, "encoded super block",
  626. (char **) &vdo->super_block.buffer);
  627. if (result != VDO_SUCCESS)
  628. return result;
  629. return allocate_vio_components(vdo, VIO_TYPE_SUPER_BLOCK,
  630. VIO_PRIORITY_METADATA, NULL, 1,
  631. (char *) super_block->buffer,
  632. &vdo->super_block.vio);
  633. }
  634. /**
  635. * finish_reading_super_block() - Continue after loading the super block.
  636. * @completion: The super block vio.
  637. *
  638. * This callback is registered in vdo_load_super_block().
  639. */
  640. static void finish_reading_super_block(struct vdo_completion *completion)
  641. {
  642. struct vdo_super_block *super_block =
  643. container_of(as_vio(completion), struct vdo_super_block, vio);
  644. vdo_continue_completion(vdo_forget(completion->parent),
  645. vdo_decode_super_block(super_block->buffer));
  646. }
  647. /**
  648. * handle_super_block_read_error() - Handle an error reading the super block.
  649. * @completion: The super block vio.
  650. *
  651. * This error handler is registered in vdo_load_super_block().
  652. */
  653. static void handle_super_block_read_error(struct vdo_completion *completion)
  654. {
  655. vio_record_metadata_io_error(as_vio(completion));
  656. finish_reading_super_block(completion);
  657. }
  658. static void read_super_block_endio(struct bio *bio)
  659. {
  660. struct vio *vio = bio->bi_private;
  661. struct vdo_completion *parent = vio->completion.parent;
  662. continue_vio_after_io(vio, finish_reading_super_block,
  663. parent->callback_thread_id);
  664. }
  665. /**
  666. * vdo_load_super_block() - Allocate a super block and read its contents from storage.
  667. * @vdo: The vdo containing the super block on disk.
  668. * @parent: The completion to notify after loading the super block.
  669. */
  670. void vdo_load_super_block(struct vdo *vdo, struct vdo_completion *parent)
  671. {
  672. int result;
  673. result = initialize_super_block(vdo, &vdo->super_block);
  674. if (result != VDO_SUCCESS) {
  675. vdo_continue_completion(parent, result);
  676. return;
  677. }
  678. vdo->super_block.vio.completion.parent = parent;
  679. vdo_submit_metadata_vio(&vdo->super_block.vio,
  680. vdo_get_data_region_start(vdo->geometry),
  681. read_super_block_endio,
  682. handle_super_block_read_error,
  683. REQ_OP_READ);
  684. }
  685. /**
  686. * vdo_get_backing_device() - Get the block device object underlying a vdo.
  687. * @vdo: The vdo.
  688. *
  689. * Return: The vdo's current block device.
  690. */
  691. struct block_device *vdo_get_backing_device(const struct vdo *vdo)
  692. {
  693. return vdo->device_config->owned_device->bdev;
  694. }
  695. /**
  696. * vdo_get_device_name() - Get the device name associated with the vdo target.
  697. * @target: The target device interface.
  698. *
  699. * Return: The block device name.
  700. */
  701. const char *vdo_get_device_name(const struct dm_target *target)
  702. {
  703. return dm_device_name(dm_table_get_md(target->table));
  704. }
  705. /**
  706. * vdo_synchronous_flush() - Issue a flush request and wait for it to complete.
  707. * @vdo: The vdo.
  708. *
  709. * Return: VDO_SUCCESS or an error.
  710. */
  711. int vdo_synchronous_flush(struct vdo *vdo)
  712. {
  713. int result;
  714. struct bio bio;
  715. bio_init(&bio, vdo_get_backing_device(vdo), NULL, 0,
  716. REQ_OP_WRITE | REQ_PREFLUSH);
  717. submit_bio_wait(&bio);
  718. result = blk_status_to_errno(bio.bi_status);
  719. atomic64_inc(&vdo->stats.flush_out);
  720. if (result != 0) {
  721. vdo_log_error_strerror(result, "synchronous flush failed");
  722. result = -EIO;
  723. }
  724. bio_uninit(&bio);
  725. return result;
  726. }
  727. /**
  728. * vdo_get_state() - Get the current state of the vdo.
  729. * @vdo: The vdo.
  730. * Context: This method may be called from any thread.
  731. *
  732. * Return: The current state of the vdo.
  733. */
  734. enum vdo_state vdo_get_state(const struct vdo *vdo)
  735. {
  736. enum vdo_state state = atomic_read(&vdo->state);
  737. /* pairs with barriers where state field is changed */
  738. smp_rmb();
  739. return state;
  740. }
  741. /**
  742. * vdo_set_state() - Set the current state of the vdo.
  743. * @vdo: The vdo whose state is to be set.
  744. * @state: The new state of the vdo.
  745. *
  746. * Context: This method may be called from any thread.
  747. */
  748. void vdo_set_state(struct vdo *vdo, enum vdo_state state)
  749. {
  750. /* pairs with barrier in vdo_get_state */
  751. smp_wmb();
  752. atomic_set(&vdo->state, state);
  753. }
  754. /**
  755. * vdo_get_admin_state() - Get the admin state of the vdo.
  756. * @vdo: The vdo.
  757. *
  758. * Return: The code for the vdo's current admin state.
  759. */
  760. const struct admin_state_code *vdo_get_admin_state(const struct vdo *vdo)
  761. {
  762. return vdo_get_admin_state_code(&vdo->admin.state);
  763. }
  764. /**
  765. * record_vdo() - Record the state of the VDO for encoding in the super block.
  766. */
  767. static void record_vdo(struct vdo *vdo)
  768. {
  769. /* This is for backwards compatibility. */
  770. vdo->states.unused = vdo->geometry.unused;
  771. vdo->states.vdo.state = vdo_get_state(vdo);
  772. vdo->states.block_map = vdo_record_block_map(vdo->block_map);
  773. vdo->states.recovery_journal = vdo_record_recovery_journal(vdo->recovery_journal);
  774. vdo->states.slab_depot = vdo_record_slab_depot(vdo->depot);
  775. vdo->states.layout = vdo->layout;
  776. }
  777. /**
  778. * continue_super_block_parent() - Continue the parent of a super block save operation.
  779. * @completion: The super block vio.
  780. *
  781. * This callback is registered in vdo_save_components().
  782. */
  783. static void continue_super_block_parent(struct vdo_completion *completion)
  784. {
  785. vdo_continue_completion(vdo_forget(completion->parent), completion->result);
  786. }
  787. /**
  788. * handle_save_error() - Log a super block save error.
  789. * @completion: The super block vio.
  790. *
  791. * This error handler is registered in vdo_save_components().
  792. */
  793. static void handle_save_error(struct vdo_completion *completion)
  794. {
  795. struct vdo_super_block *super_block =
  796. container_of(as_vio(completion), struct vdo_super_block, vio);
  797. vio_record_metadata_io_error(&super_block->vio);
  798. vdo_log_error_strerror(completion->result, "super block save failed");
  799. /*
  800. * Mark the super block as unwritable so that we won't attempt to write it again. This
  801. * avoids the case where a growth attempt fails writing the super block with the new size,
  802. * but the subsequent attempt to write out the read-only state succeeds. In this case,
  803. * writes which happened just before the suspend would not be visible if the VDO is
  804. * restarted without rebuilding, but, after a read-only rebuild, the effects of those
  805. * writes would reappear.
  806. */
  807. super_block->unwritable = true;
  808. completion->callback(completion);
  809. }
  810. static void super_block_write_endio(struct bio *bio)
  811. {
  812. struct vio *vio = bio->bi_private;
  813. struct vdo_completion *parent = vio->completion.parent;
  814. continue_vio_after_io(vio, continue_super_block_parent,
  815. parent->callback_thread_id);
  816. }
  817. /**
  818. * vdo_save_components() - Encode the vdo and save the super block asynchronously.
  819. * @vdo: The vdo whose state is being saved.
  820. * @parent: The completion to notify when the save is complete.
  821. */
  822. void vdo_save_components(struct vdo *vdo, struct vdo_completion *parent)
  823. {
  824. struct vdo_super_block *super_block = &vdo->super_block;
  825. if (super_block->unwritable) {
  826. vdo_continue_completion(parent, VDO_READ_ONLY);
  827. return;
  828. }
  829. if (super_block->vio.completion.parent != NULL) {
  830. vdo_continue_completion(parent, VDO_COMPONENT_BUSY);
  831. return;
  832. }
  833. record_vdo(vdo);
  834. vdo_encode_super_block(super_block->buffer, &vdo->states);
  835. super_block->vio.completion.parent = parent;
  836. super_block->vio.completion.callback_thread_id = parent->callback_thread_id;
  837. vdo_submit_metadata_vio(&super_block->vio,
  838. vdo_get_data_region_start(vdo->geometry),
  839. super_block_write_endio, handle_save_error,
  840. REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA);
  841. }
  842. /**
  843. * vdo_register_read_only_listener() - Register a listener to be notified when the VDO goes
  844. * read-only.
  845. * @vdo: The vdo to register with.
  846. * @listener: The object to notify.
  847. * @notification: The function to call to send the notification.
  848. * @thread_id: The id of the thread on which to send the notification.
  849. *
  850. * Return: VDO_SUCCESS or an error.
  851. */
  852. int vdo_register_read_only_listener(struct vdo *vdo, void *listener,
  853. vdo_read_only_notification_fn notification,
  854. thread_id_t thread_id)
  855. {
  856. struct vdo_thread *thread = &vdo->threads[thread_id];
  857. struct read_only_listener *read_only_listener;
  858. int result;
  859. result = VDO_ASSERT(thread_id != vdo->thread_config.dedupe_thread,
  860. "read only listener not registered on dedupe thread");
  861. if (result != VDO_SUCCESS)
  862. return result;
  863. result = vdo_allocate(1, struct read_only_listener, __func__,
  864. &read_only_listener);
  865. if (result != VDO_SUCCESS)
  866. return result;
  867. *read_only_listener = (struct read_only_listener) {
  868. .listener = listener,
  869. .notify = notification,
  870. .next = thread->listeners,
  871. };
  872. thread->listeners = read_only_listener;
  873. return VDO_SUCCESS;
  874. }
  875. /**
  876. * notify_vdo_of_read_only_mode() - Notify a vdo that it is going read-only.
  877. * @listener: The vdo.
  878. * @parent: The completion to notify in order to acknowledge the notification.
  879. *
  880. * This will save the read-only state to the super block.
  881. *
  882. * Implements vdo_read_only_notification_fn.
  883. */
  884. static void notify_vdo_of_read_only_mode(void *listener, struct vdo_completion *parent)
  885. {
  886. struct vdo *vdo = listener;
  887. if (vdo_in_read_only_mode(vdo))
  888. vdo_finish_completion(parent);
  889. vdo_set_state(vdo, VDO_READ_ONLY_MODE);
  890. vdo_save_components(vdo, parent);
  891. }
  892. /**
  893. * vdo_enable_read_only_entry() - Enable a vdo to enter read-only mode on errors.
  894. * @vdo: The vdo to enable.
  895. *
  896. * Return: VDO_SUCCESS or an error.
  897. */
  898. int vdo_enable_read_only_entry(struct vdo *vdo)
  899. {
  900. thread_id_t id;
  901. bool is_read_only = vdo_in_read_only_mode(vdo);
  902. struct read_only_notifier *notifier = &vdo->read_only_notifier;
  903. if (is_read_only) {
  904. notifier->read_only_error = VDO_READ_ONLY;
  905. notifier->state = NOTIFIED;
  906. } else {
  907. notifier->state = MAY_NOT_NOTIFY;
  908. }
  909. spin_lock_init(&notifier->lock);
  910. vdo_initialize_completion(&notifier->completion, vdo,
  911. VDO_READ_ONLY_MODE_COMPLETION);
  912. for (id = 0; id < vdo->thread_config.thread_count; id++)
  913. vdo->threads[id].is_read_only = is_read_only;
  914. return vdo_register_read_only_listener(vdo, vdo, notify_vdo_of_read_only_mode,
  915. vdo->thread_config.admin_thread);
  916. }
  917. /**
  918. * vdo_wait_until_not_entering_read_only_mode() - Wait until no read-only notifications are in
  919. * progress and prevent any subsequent
  920. * notifications.
  921. * @parent: The completion to notify when no threads are entering read-only mode.
  922. *
  923. * Notifications may be re-enabled by calling vdo_allow_read_only_mode_entry().
  924. */
  925. void vdo_wait_until_not_entering_read_only_mode(struct vdo_completion *parent)
  926. {
  927. struct vdo *vdo = parent->vdo;
  928. struct read_only_notifier *notifier = &vdo->read_only_notifier;
  929. vdo_assert_on_admin_thread(vdo, __func__);
  930. if (notifier->waiter != NULL) {
  931. vdo_continue_completion(parent, VDO_COMPONENT_BUSY);
  932. return;
  933. }
  934. spin_lock(&notifier->lock);
  935. if (notifier->state == NOTIFYING)
  936. notifier->waiter = parent;
  937. else if (notifier->state == MAY_NOTIFY)
  938. notifier->state = MAY_NOT_NOTIFY;
  939. spin_unlock(&notifier->lock);
  940. if (notifier->waiter == NULL) {
  941. /*
  942. * A notification was not in progress, and now they are
  943. * disallowed.
  944. */
  945. vdo_launch_completion(parent);
  946. return;
  947. }
  948. }
  949. /**
  950. * as_notifier() - Convert a generic vdo_completion to a read_only_notifier.
  951. * @completion: The completion to convert.
  952. *
  953. * Return: The completion as a read_only_notifier.
  954. */
  955. static inline struct read_only_notifier *as_notifier(struct vdo_completion *completion)
  956. {
  957. vdo_assert_completion_type(completion, VDO_READ_ONLY_MODE_COMPLETION);
  958. return container_of(completion, struct read_only_notifier, completion);
  959. }
  960. /**
  961. * finish_entering_read_only_mode() - Complete the process of entering read only mode.
  962. * @completion: The read-only mode completion.
  963. */
  964. static void finish_entering_read_only_mode(struct vdo_completion *completion)
  965. {
  966. struct read_only_notifier *notifier = as_notifier(completion);
  967. vdo_assert_on_admin_thread(completion->vdo, __func__);
  968. spin_lock(&notifier->lock);
  969. notifier->state = NOTIFIED;
  970. spin_unlock(&notifier->lock);
  971. if (notifier->waiter != NULL)
  972. vdo_continue_completion(vdo_forget(notifier->waiter),
  973. completion->result);
  974. }
  975. /**
  976. * make_thread_read_only() - Inform each thread that the VDO is in read-only mode.
  977. * @completion: The read-only mode completion.
  978. */
  979. static void make_thread_read_only(struct vdo_completion *completion)
  980. {
  981. struct vdo *vdo = completion->vdo;
  982. thread_id_t thread_id = completion->callback_thread_id;
  983. struct read_only_notifier *notifier = as_notifier(completion);
  984. struct read_only_listener *listener = completion->parent;
  985. if (listener == NULL) {
  986. /* This is the first call on this thread */
  987. struct vdo_thread *thread = &vdo->threads[thread_id];
  988. thread->is_read_only = true;
  989. listener = thread->listeners;
  990. if (thread_id == 0)
  991. vdo_log_error_strerror(READ_ONCE(notifier->read_only_error),
  992. "Unrecoverable error, entering read-only mode");
  993. } else {
  994. /* We've just finished notifying a listener */
  995. listener = listener->next;
  996. }
  997. if (listener != NULL) {
  998. /* We have a listener to notify */
  999. vdo_prepare_completion(completion, make_thread_read_only,
  1000. make_thread_read_only, thread_id,
  1001. listener);
  1002. listener->notify(listener->listener, completion);
  1003. return;
  1004. }
  1005. /* We're done with this thread */
  1006. if (++thread_id == vdo->thread_config.dedupe_thread) {
  1007. /*
  1008. * We don't want to notify the dedupe thread since it may be
  1009. * blocked rebuilding the index.
  1010. */
  1011. thread_id++;
  1012. }
  1013. if (thread_id >= vdo->thread_config.thread_count) {
  1014. /* There are no more threads */
  1015. vdo_prepare_completion(completion, finish_entering_read_only_mode,
  1016. finish_entering_read_only_mode,
  1017. vdo->thread_config.admin_thread, NULL);
  1018. } else {
  1019. vdo_prepare_completion(completion, make_thread_read_only,
  1020. make_thread_read_only, thread_id, NULL);
  1021. }
  1022. vdo_launch_completion(completion);
  1023. }
  1024. /**
  1025. * vdo_allow_read_only_mode_entry() - Allow the notifier to put the VDO into read-only mode,
  1026. * reversing the effects of
  1027. * vdo_wait_until_not_entering_read_only_mode().
  1028. * @parent: The object to notify once the operation is complete.
  1029. *
  1030. * If some thread tried to put the vdo into read-only mode while notifications were disallowed, it
  1031. * will be done when this method is called. If that happens, the parent will not be notified until
  1032. * the vdo has actually entered read-only mode and attempted to save the super block.
  1033. *
  1034. * Context: This method may only be called from the admin thread.
  1035. */
  1036. void vdo_allow_read_only_mode_entry(struct vdo_completion *parent)
  1037. {
  1038. struct vdo *vdo = parent->vdo;
  1039. struct read_only_notifier *notifier = &vdo->read_only_notifier;
  1040. vdo_assert_on_admin_thread(vdo, __func__);
  1041. if (notifier->waiter != NULL) {
  1042. vdo_continue_completion(parent, VDO_COMPONENT_BUSY);
  1043. return;
  1044. }
  1045. spin_lock(&notifier->lock);
  1046. if (notifier->state == MAY_NOT_NOTIFY) {
  1047. if (notifier->read_only_error == VDO_SUCCESS) {
  1048. notifier->state = MAY_NOTIFY;
  1049. } else {
  1050. notifier->state = NOTIFYING;
  1051. notifier->waiter = parent;
  1052. }
  1053. }
  1054. spin_unlock(&notifier->lock);
  1055. if (notifier->waiter == NULL) {
  1056. /* We're done */
  1057. vdo_launch_completion(parent);
  1058. return;
  1059. }
  1060. /* Do the pending notification. */
  1061. make_thread_read_only(&notifier->completion);
  1062. }
  1063. /**
  1064. * vdo_enter_read_only_mode() - Put a VDO into read-only mode and save the read-only state in the
  1065. * super block.
  1066. * @vdo: The vdo.
  1067. * @error_code: The error which caused the VDO to enter read-only mode.
  1068. *
  1069. * This method is a no-op if the VDO is already read-only.
  1070. */
  1071. void vdo_enter_read_only_mode(struct vdo *vdo, int error_code)
  1072. {
  1073. bool notify = false;
  1074. thread_id_t thread_id = vdo_get_callback_thread_id();
  1075. struct read_only_notifier *notifier = &vdo->read_only_notifier;
  1076. struct vdo_thread *thread;
  1077. if (thread_id != VDO_INVALID_THREAD_ID) {
  1078. thread = &vdo->threads[thread_id];
  1079. if (thread->is_read_only) {
  1080. /* This thread has already gone read-only. */
  1081. return;
  1082. }
  1083. /* Record for this thread that the VDO is read-only. */
  1084. thread->is_read_only = true;
  1085. }
  1086. spin_lock(&notifier->lock);
  1087. if (notifier->read_only_error == VDO_SUCCESS) {
  1088. WRITE_ONCE(notifier->read_only_error, error_code);
  1089. if (notifier->state == MAY_NOTIFY) {
  1090. notifier->state = NOTIFYING;
  1091. notify = true;
  1092. }
  1093. }
  1094. spin_unlock(&notifier->lock);
  1095. if (!notify) {
  1096. /* The notifier is already aware of a read-only error */
  1097. return;
  1098. }
  1099. /* Initiate a notification starting on the lowest numbered thread. */
  1100. vdo_launch_completion_callback(&notifier->completion, make_thread_read_only, 0);
  1101. }
  1102. /**
  1103. * vdo_is_read_only() - Check whether the VDO is read-only.
  1104. * @vdo: The vdo.
  1105. *
  1106. * Return: true if the vdo is read-only.
  1107. *
  1108. * This method may be called from any thread, as opposed to examining the VDO's state field which
  1109. * is only safe to check from the admin thread.
  1110. */
  1111. bool vdo_is_read_only(struct vdo *vdo)
  1112. {
  1113. return vdo->threads[vdo_get_callback_thread_id()].is_read_only;
  1114. }
  1115. /**
  1116. * vdo_in_read_only_mode() - Check whether a vdo is in read-only mode.
  1117. * @vdo: The vdo to query.
  1118. *
  1119. * Return: true if the vdo is in read-only mode.
  1120. */
  1121. bool vdo_in_read_only_mode(const struct vdo *vdo)
  1122. {
  1123. return (vdo_get_state(vdo) == VDO_READ_ONLY_MODE);
  1124. }
  1125. /**
  1126. * vdo_in_recovery_mode() - Check whether the vdo is in recovery mode.
  1127. * @vdo: The vdo to query.
  1128. *
  1129. * Return: true if the vdo is in recovery mode.
  1130. */
  1131. bool vdo_in_recovery_mode(const struct vdo *vdo)
  1132. {
  1133. return (vdo_get_state(vdo) == VDO_RECOVERING);
  1134. }
  1135. /**
  1136. * vdo_enter_recovery_mode() - Put the vdo into recovery mode.
  1137. * @vdo: The vdo.
  1138. */
  1139. void vdo_enter_recovery_mode(struct vdo *vdo)
  1140. {
  1141. vdo_assert_on_admin_thread(vdo, __func__);
  1142. if (vdo_in_read_only_mode(vdo))
  1143. return;
  1144. vdo_log_info("Entering recovery mode");
  1145. vdo_set_state(vdo, VDO_RECOVERING);
  1146. }
  1147. /**
  1148. * complete_synchronous_action() - Signal the waiting thread that a synchronous action is complete.
  1149. * @completion: The sync completion.
  1150. */
  1151. static void complete_synchronous_action(struct vdo_completion *completion)
  1152. {
  1153. vdo_assert_completion_type(completion, VDO_SYNC_COMPLETION);
  1154. complete(&(container_of(completion, struct sync_completion,
  1155. vdo_completion)->completion));
  1156. }
  1157. /**
  1158. * perform_synchronous_action() - Launch an action on a VDO thread and wait for it to complete.
  1159. * @vdo: The vdo.
  1160. * @action: The callback to launch.
  1161. * @thread_id: The thread on which to run the action.
  1162. * @parent: The parent of the sync completion (may be NULL).
  1163. */
  1164. static int perform_synchronous_action(struct vdo *vdo, vdo_action_fn action,
  1165. thread_id_t thread_id, void *parent)
  1166. {
  1167. struct sync_completion sync;
  1168. vdo_initialize_completion(&sync.vdo_completion, vdo, VDO_SYNC_COMPLETION);
  1169. init_completion(&sync.completion);
  1170. sync.vdo_completion.parent = parent;
  1171. vdo_launch_completion_callback(&sync.vdo_completion, action, thread_id);
  1172. wait_for_completion(&sync.completion);
  1173. return sync.vdo_completion.result;
  1174. }
  1175. /**
  1176. * set_compression_callback() - Callback to turn compression on or off.
  1177. * @completion: The completion.
  1178. */
  1179. static void set_compression_callback(struct vdo_completion *completion)
  1180. {
  1181. struct vdo *vdo = completion->vdo;
  1182. bool *enable = completion->parent;
  1183. bool was_enabled = vdo_get_compressing(vdo);
  1184. if (*enable != was_enabled) {
  1185. WRITE_ONCE(vdo->compressing, *enable);
  1186. if (was_enabled) {
  1187. /* Signal the packer to flush since compression has been disabled. */
  1188. vdo_flush_packer(vdo->packer);
  1189. }
  1190. }
  1191. vdo_log_info("compression is %s", (*enable ? "enabled" : "disabled"));
  1192. *enable = was_enabled;
  1193. complete_synchronous_action(completion);
  1194. }
  1195. /**
  1196. * vdo_set_compressing() - Turn compression on or off.
  1197. * @vdo: The vdo.
  1198. * @enable: Whether to enable or disable compression.
  1199. *
  1200. * Return: Whether compression was previously on or off.
  1201. */
  1202. bool vdo_set_compressing(struct vdo *vdo, bool enable)
  1203. {
  1204. perform_synchronous_action(vdo, set_compression_callback,
  1205. vdo->thread_config.packer_thread,
  1206. &enable);
  1207. return enable;
  1208. }
  1209. /**
  1210. * vdo_get_compressing() - Get whether compression is enabled in a vdo.
  1211. * @vdo: The vdo.
  1212. *
  1213. * Return: State of compression.
  1214. */
  1215. bool vdo_get_compressing(struct vdo *vdo)
  1216. {
  1217. return READ_ONCE(vdo->compressing);
  1218. }
  1219. static size_t get_block_map_cache_size(const struct vdo *vdo)
  1220. {
  1221. return ((size_t) vdo->device_config->cache_size) * VDO_BLOCK_SIZE;
  1222. }
  1223. static struct error_statistics __must_check get_vdo_error_statistics(const struct vdo *vdo)
  1224. {
  1225. /*
  1226. * The error counts can be incremented from arbitrary threads and so must be incremented
  1227. * atomically, but they are just statistics with no semantics that could rely on memory
  1228. * order, so unfenced reads are sufficient.
  1229. */
  1230. const struct atomic_statistics *atoms = &vdo->stats;
  1231. return (struct error_statistics) {
  1232. .invalid_advice_pbn_count = atomic64_read(&atoms->invalid_advice_pbn_count),
  1233. .no_space_error_count = atomic64_read(&atoms->no_space_error_count),
  1234. .read_only_error_count = atomic64_read(&atoms->read_only_error_count),
  1235. };
  1236. }
  1237. static void copy_bio_stat(struct bio_stats *b, const struct atomic_bio_stats *a)
  1238. {
  1239. b->read = atomic64_read(&a->read);
  1240. b->write = atomic64_read(&a->write);
  1241. b->discard = atomic64_read(&a->discard);
  1242. b->flush = atomic64_read(&a->flush);
  1243. b->empty_flush = atomic64_read(&a->empty_flush);
  1244. b->fua = atomic64_read(&a->fua);
  1245. }
  1246. static struct bio_stats subtract_bio_stats(struct bio_stats minuend,
  1247. struct bio_stats subtrahend)
  1248. {
  1249. return (struct bio_stats) {
  1250. .read = minuend.read - subtrahend.read,
  1251. .write = minuend.write - subtrahend.write,
  1252. .discard = minuend.discard - subtrahend.discard,
  1253. .flush = minuend.flush - subtrahend.flush,
  1254. .empty_flush = minuend.empty_flush - subtrahend.empty_flush,
  1255. .fua = minuend.fua - subtrahend.fua,
  1256. };
  1257. }
  1258. /**
  1259. * vdo_get_physical_blocks_allocated() - Get the number of physical blocks in use by user data.
  1260. * @vdo: The vdo.
  1261. *
  1262. * Return: The number of blocks allocated for user data.
  1263. */
  1264. static block_count_t __must_check vdo_get_physical_blocks_allocated(const struct vdo *vdo)
  1265. {
  1266. return (vdo_get_slab_depot_allocated_blocks(vdo->depot) -
  1267. vdo_get_journal_block_map_data_blocks_used(vdo->recovery_journal));
  1268. }
  1269. /**
  1270. * vdo_get_physical_blocks_overhead() - Get the number of physical blocks used by vdo metadata.
  1271. * @vdo: The vdo.
  1272. *
  1273. * Return: The number of overhead blocks.
  1274. */
  1275. static block_count_t __must_check vdo_get_physical_blocks_overhead(const struct vdo *vdo)
  1276. {
  1277. /*
  1278. * config.physical_blocks is mutated during resize and is in a packed structure,
  1279. * but resize runs on admin thread.
  1280. * TODO: Verify that this is always safe.
  1281. */
  1282. return (vdo->states.vdo.config.physical_blocks -
  1283. vdo_get_slab_depot_data_blocks(vdo->depot) +
  1284. vdo_get_journal_block_map_data_blocks_used(vdo->recovery_journal));
  1285. }
  1286. static const char *vdo_describe_state(enum vdo_state state)
  1287. {
  1288. /* These strings should all fit in the 15 chars of VDOStatistics.mode. */
  1289. switch (state) {
  1290. case VDO_RECOVERING:
  1291. return "recovering";
  1292. case VDO_READ_ONLY_MODE:
  1293. return "read-only";
  1294. default:
  1295. return "normal";
  1296. }
  1297. }
  1298. /**
  1299. * get_vdo_statistics() - Populate a vdo_statistics structure on the admin thread.
  1300. * @vdo: The vdo.
  1301. * @stats: The statistics structure to populate.
  1302. */
  1303. static void get_vdo_statistics(const struct vdo *vdo, struct vdo_statistics *stats)
  1304. {
  1305. struct recovery_journal *journal = vdo->recovery_journal;
  1306. enum vdo_state state = vdo_get_state(vdo);
  1307. vdo_assert_on_admin_thread(vdo, __func__);
  1308. /* start with a clean slate */
  1309. memset(stats, 0, sizeof(struct vdo_statistics));
  1310. /*
  1311. * These are immutable properties of the vdo object, so it is safe to query them from any
  1312. * thread.
  1313. */
  1314. stats->version = STATISTICS_VERSION;
  1315. stats->logical_blocks = vdo->states.vdo.config.logical_blocks;
  1316. /*
  1317. * config.physical_blocks is mutated during resize and is in a packed structure, but resize
  1318. * runs on the admin thread.
  1319. * TODO: verify that this is always safe
  1320. */
  1321. stats->physical_blocks = vdo->states.vdo.config.physical_blocks;
  1322. stats->block_size = VDO_BLOCK_SIZE;
  1323. stats->complete_recoveries = vdo->states.vdo.complete_recoveries;
  1324. stats->read_only_recoveries = vdo->states.vdo.read_only_recoveries;
  1325. stats->block_map_cache_size = get_block_map_cache_size(vdo);
  1326. /* The callees are responsible for thread-safety. */
  1327. stats->data_blocks_used = vdo_get_physical_blocks_allocated(vdo);
  1328. stats->overhead_blocks_used = vdo_get_physical_blocks_overhead(vdo);
  1329. stats->logical_blocks_used = vdo_get_recovery_journal_logical_blocks_used(journal);
  1330. vdo_get_slab_depot_statistics(vdo->depot, stats);
  1331. stats->journal = vdo_get_recovery_journal_statistics(journal);
  1332. stats->packer = vdo_get_packer_statistics(vdo->packer);
  1333. stats->block_map = vdo_get_block_map_statistics(vdo->block_map);
  1334. vdo_get_dedupe_statistics(vdo->hash_zones, stats);
  1335. stats->errors = get_vdo_error_statistics(vdo);
  1336. stats->in_recovery_mode = (state == VDO_RECOVERING);
  1337. snprintf(stats->mode, sizeof(stats->mode), "%s", vdo_describe_state(state));
  1338. stats->instance = vdo->instance;
  1339. stats->current_vios_in_progress = get_data_vio_pool_active_requests(vdo->data_vio_pool);
  1340. stats->max_vios = get_data_vio_pool_maximum_requests(vdo->data_vio_pool);
  1341. stats->flush_out = atomic64_read(&vdo->stats.flush_out);
  1342. stats->logical_block_size = vdo->device_config->logical_block_size;
  1343. copy_bio_stat(&stats->bios_in, &vdo->stats.bios_in);
  1344. copy_bio_stat(&stats->bios_in_partial, &vdo->stats.bios_in_partial);
  1345. copy_bio_stat(&stats->bios_out, &vdo->stats.bios_out);
  1346. copy_bio_stat(&stats->bios_meta, &vdo->stats.bios_meta);
  1347. copy_bio_stat(&stats->bios_journal, &vdo->stats.bios_journal);
  1348. copy_bio_stat(&stats->bios_page_cache, &vdo->stats.bios_page_cache);
  1349. copy_bio_stat(&stats->bios_out_completed, &vdo->stats.bios_out_completed);
  1350. copy_bio_stat(&stats->bios_meta_completed, &vdo->stats.bios_meta_completed);
  1351. copy_bio_stat(&stats->bios_journal_completed,
  1352. &vdo->stats.bios_journal_completed);
  1353. copy_bio_stat(&stats->bios_page_cache_completed,
  1354. &vdo->stats.bios_page_cache_completed);
  1355. copy_bio_stat(&stats->bios_acknowledged, &vdo->stats.bios_acknowledged);
  1356. copy_bio_stat(&stats->bios_acknowledged_partial, &vdo->stats.bios_acknowledged_partial);
  1357. stats->bios_in_progress =
  1358. subtract_bio_stats(stats->bios_in, stats->bios_acknowledged);
  1359. vdo_get_memory_stats(&stats->memory_usage.bytes_used,
  1360. &stats->memory_usage.peak_bytes_used);
  1361. }
  1362. /**
  1363. * vdo_fetch_statistics_callback() - Action to populate a vdo_statistics
  1364. * structure on the admin thread.
  1365. * @completion: The completion.
  1366. *
  1367. * This callback is registered in vdo_fetch_statistics().
  1368. */
  1369. static void vdo_fetch_statistics_callback(struct vdo_completion *completion)
  1370. {
  1371. get_vdo_statistics(completion->vdo, completion->parent);
  1372. complete_synchronous_action(completion);
  1373. }
  1374. /**
  1375. * vdo_fetch_statistics() - Fetch statistics on the correct thread.
  1376. * @vdo: The vdo.
  1377. * @stats: The vdo statistics are returned here.
  1378. */
  1379. void vdo_fetch_statistics(struct vdo *vdo, struct vdo_statistics *stats)
  1380. {
  1381. perform_synchronous_action(vdo, vdo_fetch_statistics_callback,
  1382. vdo->thread_config.admin_thread, stats);
  1383. }
  1384. /**
  1385. * vdo_get_callback_thread_id() - Get the id of the callback thread on which a completion is
  1386. * currently running.
  1387. *
  1388. * Return: The current thread ID, or -1 if no such thread.
  1389. */
  1390. thread_id_t vdo_get_callback_thread_id(void)
  1391. {
  1392. struct vdo_work_queue *queue = vdo_get_current_work_queue();
  1393. struct vdo_thread *thread;
  1394. thread_id_t thread_id;
  1395. if (queue == NULL)
  1396. return VDO_INVALID_THREAD_ID;
  1397. thread = vdo_get_work_queue_owner(queue);
  1398. thread_id = thread->thread_id;
  1399. if (PARANOID_THREAD_CONSISTENCY_CHECKS) {
  1400. BUG_ON(thread_id >= thread->vdo->thread_config.thread_count);
  1401. BUG_ON(thread != &thread->vdo->threads[thread_id]);
  1402. }
  1403. return thread_id;
  1404. }
  1405. /**
  1406. * vdo_dump_status() - Dump status information about a vdo to the log for debugging.
  1407. * @vdo: The vdo to dump.
  1408. */
  1409. void vdo_dump_status(const struct vdo *vdo)
  1410. {
  1411. zone_count_t zone;
  1412. vdo_dump_flusher(vdo->flusher);
  1413. vdo_dump_recovery_journal_statistics(vdo->recovery_journal);
  1414. vdo_dump_packer(vdo->packer);
  1415. vdo_dump_slab_depot(vdo->depot);
  1416. for (zone = 0; zone < vdo->thread_config.logical_zone_count; zone++)
  1417. vdo_dump_logical_zone(&vdo->logical_zones->zones[zone]);
  1418. for (zone = 0; zone < vdo->thread_config.physical_zone_count; zone++)
  1419. vdo_dump_physical_zone(&vdo->physical_zones->zones[zone]);
  1420. vdo_dump_hash_zones(vdo->hash_zones);
  1421. }
  1422. /**
  1423. * vdo_assert_on_admin_thread() - Assert that we are running on the admin thread.
  1424. * @vdo: The vdo.
  1425. * @name: The name of the function which should be running on the admin thread (for logging).
  1426. */
  1427. void vdo_assert_on_admin_thread(const struct vdo *vdo, const char *name)
  1428. {
  1429. VDO_ASSERT_LOG_ONLY((vdo_get_callback_thread_id() == vdo->thread_config.admin_thread),
  1430. "%s called on admin thread", name);
  1431. }
  1432. /**
  1433. * vdo_assert_on_logical_zone_thread() - Assert that this function was called on the specified
  1434. * logical zone thread.
  1435. * @vdo: The vdo.
  1436. * @logical_zone: The number of the logical zone.
  1437. * @name: The name of the calling function.
  1438. */
  1439. void vdo_assert_on_logical_zone_thread(const struct vdo *vdo, zone_count_t logical_zone,
  1440. const char *name)
  1441. {
  1442. VDO_ASSERT_LOG_ONLY((vdo_get_callback_thread_id() ==
  1443. vdo->thread_config.logical_threads[logical_zone]),
  1444. "%s called on logical thread", name);
  1445. }
  1446. /**
  1447. * vdo_assert_on_physical_zone_thread() - Assert that this function was called on the specified
  1448. * physical zone thread.
  1449. * @vdo: The vdo.
  1450. * @physical_zone: The number of the physical zone.
  1451. * @name: The name of the calling function.
  1452. */
  1453. void vdo_assert_on_physical_zone_thread(const struct vdo *vdo,
  1454. zone_count_t physical_zone, const char *name)
  1455. {
  1456. VDO_ASSERT_LOG_ONLY((vdo_get_callback_thread_id() ==
  1457. vdo->thread_config.physical_threads[physical_zone]),
  1458. "%s called on physical thread", name);
  1459. }
  1460. /**
  1461. * vdo_get_physical_zone() - Get the physical zone responsible for a given physical block number.
  1462. * @vdo: The vdo containing the physical zones.
  1463. * @pbn: The PBN of the data block.
  1464. * @zone_ptr: A pointer to return the physical zone.
  1465. *
  1466. * Gets the physical zone responsible for a given physical block number of a data block in this vdo
  1467. * instance, or of the zero block (for which a NULL zone is returned). For any other block number
  1468. * that is not in the range of valid data block numbers in any slab, an error will be returned.
  1469. * This function is safe to call on invalid block numbers; it will not put the vdo into read-only
  1470. * mode.
  1471. *
  1472. * Return: VDO_SUCCESS or VDO_OUT_OF_RANGE if the block number is invalid or an error code for any
  1473. * other failure.
  1474. */
  1475. int vdo_get_physical_zone(const struct vdo *vdo, physical_block_number_t pbn,
  1476. struct physical_zone **zone_ptr)
  1477. {
  1478. struct vdo_slab *slab;
  1479. int result;
  1480. if (pbn == VDO_ZERO_BLOCK) {
  1481. *zone_ptr = NULL;
  1482. return VDO_SUCCESS;
  1483. }
  1484. /*
  1485. * Used because it does a more restrictive bounds check than vdo_get_slab(), and done first
  1486. * because it won't trigger read-only mode on an invalid PBN.
  1487. */
  1488. if (!vdo_is_physical_data_block(vdo->depot, pbn))
  1489. return VDO_OUT_OF_RANGE;
  1490. /* With the PBN already checked, we should always succeed in finding a slab. */
  1491. slab = vdo_get_slab(vdo->depot, pbn);
  1492. result = VDO_ASSERT(slab != NULL, "vdo_get_slab must succeed on all valid PBNs");
  1493. if (result != VDO_SUCCESS)
  1494. return result;
  1495. *zone_ptr = &vdo->physical_zones->zones[slab->allocator->zone_number];
  1496. return VDO_SUCCESS;
  1497. }