data-vio.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2023 Red Hat
  4. */
  5. #include "data-vio.h"
  6. #include <linux/atomic.h>
  7. #include <linux/bio.h>
  8. #include <linux/blkdev.h>
  9. #include <linux/delay.h>
  10. #include <linux/device-mapper.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/kernel.h>
  13. #include <linux/list.h>
  14. #include <linux/lz4.h>
  15. #include <linux/minmax.h>
  16. #include <linux/sched.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/wait.h>
  19. #include "logger.h"
  20. #include "memory-alloc.h"
  21. #include "murmurhash3.h"
  22. #include "permassert.h"
  23. #include "block-map.h"
  24. #include "dump.h"
  25. #include "encodings.h"
  26. #include "int-map.h"
  27. #include "io-submitter.h"
  28. #include "logical-zone.h"
  29. #include "packer.h"
  30. #include "recovery-journal.h"
  31. #include "slab-depot.h"
  32. #include "status-codes.h"
  33. #include "types.h"
  34. #include "vdo.h"
  35. #include "vio.h"
  36. #include "wait-queue.h"
  37. /**
  38. * DOC: Bio flags.
  39. *
  40. * For certain flags set on user bios, if the user bio has not yet been acknowledged, setting those
  41. * flags on our own bio(s) for that request may help underlying layers better fulfill the user
  42. * bio's needs. This constant contains the aggregate of those flags; VDO strips all the other
  43. * flags, as they convey incorrect information.
  44. *
  45. * These flags are always irrelevant if we have already finished the user bio as they are only
  46. * hints on IO importance. If VDO has finished the user bio, any remaining IO done doesn't care how
  47. * important finishing the finished bio was.
  48. *
  49. * Note that bio.c contains the complete list of flags we believe may be set; the following list
  50. * explains the action taken with each of those flags VDO could receive:
  51. *
  52. * * REQ_SYNC: Passed down if the user bio is not yet completed, since it indicates the user bio
  53. * completion is required for further work to be done by the issuer.
  54. * * REQ_META: Passed down if the user bio is not yet completed, since it may mean the lower layer
  55. * treats it as more urgent, similar to REQ_SYNC.
  56. * * REQ_PRIO: Passed down if the user bio is not yet completed, since it indicates the user bio is
  57. * important.
  58. * * REQ_NOMERGE: Set only if the incoming bio was split; irrelevant to VDO IO.
  59. * * REQ_IDLE: Set if the incoming bio had more IO quickly following; VDO's IO pattern doesn't
  60. * match incoming IO, so this flag is incorrect for it.
  61. * * REQ_FUA: Handled separately, and irrelevant to VDO IO otherwise.
  62. * * REQ_RAHEAD: Passed down, as, for reads, it indicates trivial importance.
  63. * * REQ_BACKGROUND: Not passed down, as VIOs are a limited resource and VDO needs them recycled
  64. * ASAP to service heavy load, which is the only place where REQ_BACKGROUND might aid in load
  65. * prioritization.
  66. */
  67. static blk_opf_t PASSTHROUGH_FLAGS = (REQ_PRIO | REQ_META | REQ_SYNC | REQ_RAHEAD);
  68. /**
  69. * DOC:
  70. *
  71. * The data_vio_pool maintains the pool of data_vios which a vdo uses to service incoming bios. For
  72. * correctness, and in order to avoid potentially expensive or blocking memory allocations during
  73. * normal operation, the number of concurrently active data_vios is capped. Furthermore, in order
  74. * to avoid starvation of reads and writes, at most 75% of the data_vios may be used for
  75. * discards. The data_vio_pool is responsible for enforcing these limits. Threads submitting bios
  76. * for which a data_vio or discard permit are not available will block until the necessary
  77. * resources are available. The pool is also responsible for distributing resources to blocked
  78. * threads and waking them. Finally, the pool attempts to batch the work of recycling data_vios by
  79. * performing the work of actually assigning resources to blocked threads or placing data_vios back
  80. * into the pool on a single cpu at a time.
  81. *
  82. * The pool contains two "limiters", one for tracking data_vios and one for tracking discard
  83. * permits. The limiters also provide safe cross-thread access to pool statistics without the need
  84. * to take the pool's lock. When a thread submits a bio to a vdo device, it will first attempt to
  85. * get a discard permit if it is a discard, and then to get a data_vio. If the necessary resources
  86. * are available, the incoming bio will be assigned to the acquired data_vio, and it will be
  87. * launched. However, if either of these are unavailable, the arrival time of the bio is recorded
  88. * in the bio's bi_private field, the bio and its submitter are both queued on the appropriate
  89. * limiter and the submitting thread will then put itself to sleep. (note that this mechanism will
  90. * break if jiffies are only 32 bits.)
  91. *
  92. * Whenever a data_vio has completed processing for the bio it was servicing, release_data_vio()
  93. * will be called on it. This function will add the data_vio to a funnel queue, and then check the
  94. * state of the pool. If the pool is not currently processing released data_vios, the pool's
  95. * completion will be enqueued on a cpu queue. This obviates the need for the releasing threads to
  96. * hold the pool's lock, and also batches release work while avoiding starvation of the cpu
  97. * threads.
  98. *
  99. * Whenever the pool's completion is run on a cpu thread, it calls process_release_callback() which
  100. * processes a batch of returned data_vios (currently at most 32) from the pool's funnel queue. For
  101. * each data_vio, it first checks whether that data_vio was processing a discard. If so, and there
  102. * is a blocked bio waiting for a discard permit, that permit is notionally transferred to the
  103. * eldest discard waiter, and that waiter is moved to the end of the list of discard bios waiting
  104. * for a data_vio. If there are no discard waiters, the discard permit is returned to the pool.
  105. * Next, the data_vio is assigned to the oldest blocked bio which either has a discard permit, or
  106. * doesn't need one and relaunched. If neither of these exist, the data_vio is returned to the
  107. * pool. Finally, if any waiting bios were launched, the threads which blocked trying to submit
  108. * them are awakened.
  109. */
  110. #define DATA_VIO_RELEASE_BATCH_SIZE 128
  111. static const unsigned int VDO_SECTORS_PER_BLOCK_MASK = VDO_SECTORS_PER_BLOCK - 1;
  112. static const u32 COMPRESSION_STATUS_MASK = 0xff;
  113. static const u32 MAY_NOT_COMPRESS_MASK = 0x80000000;
  114. struct limiter;
  115. typedef void (*assigner_fn)(struct limiter *limiter);
  116. /* Bookkeeping structure for a single type of resource. */
  117. struct limiter {
  118. /* The data_vio_pool to which this limiter belongs */
  119. struct data_vio_pool *pool;
  120. /* The maximum number of data_vios available */
  121. data_vio_count_t limit;
  122. /* The number of resources in use */
  123. data_vio_count_t busy;
  124. /* The maximum number of resources ever simultaneously in use */
  125. data_vio_count_t max_busy;
  126. /* The number of resources to release */
  127. data_vio_count_t release_count;
  128. /* The number of waiters to wake */
  129. data_vio_count_t wake_count;
  130. /* The list of waiting bios which are known to process_release_callback() */
  131. struct bio_list waiters;
  132. /* The list of waiting bios which are not yet known to process_release_callback() */
  133. struct bio_list new_waiters;
  134. /* The list of waiters which have their permits */
  135. struct bio_list *permitted_waiters;
  136. /* The function for assigning a resource to a waiter */
  137. assigner_fn assigner;
  138. /* The queue of blocked threads */
  139. wait_queue_head_t blocked_threads;
  140. /* The arrival time of the eldest waiter */
  141. u64 arrival;
  142. };
  143. /*
  144. * A data_vio_pool is a collection of preallocated data_vios which may be acquired from any thread,
  145. * and are released in batches.
  146. */
  147. struct data_vio_pool {
  148. /* Completion for scheduling releases */
  149. struct vdo_completion completion;
  150. /* The administrative state of the pool */
  151. struct admin_state state;
  152. /* Lock protecting the pool */
  153. spinlock_t lock;
  154. /* The main limiter controlling the total data_vios in the pool. */
  155. struct limiter limiter;
  156. /* The limiter controlling data_vios for discard */
  157. struct limiter discard_limiter;
  158. /* The list of bios which have discard permits but still need a data_vio */
  159. struct bio_list permitted_discards;
  160. /* The list of available data_vios */
  161. struct list_head available;
  162. /* The queue of data_vios waiting to be returned to the pool */
  163. struct funnel_queue *queue;
  164. /* Whether the pool is processing, or scheduled to process releases */
  165. atomic_t processing;
  166. /* The data vios in the pool */
  167. struct data_vio data_vios[];
  168. };
  169. static const char * const ASYNC_OPERATION_NAMES[] = {
  170. "launch",
  171. "acknowledge_write",
  172. "acquire_hash_lock",
  173. "attempt_logical_block_lock",
  174. "lock_duplicate_pbn",
  175. "check_for_duplication",
  176. "cleanup",
  177. "compress_data_vio",
  178. "find_block_map_slot",
  179. "get_mapped_block_for_read",
  180. "get_mapped_block_for_write",
  181. "hash_data_vio",
  182. "journal_remapping",
  183. "vdo_attempt_packing",
  184. "put_mapped_block",
  185. "read_data_vio",
  186. "update_dedupe_index",
  187. "update_reference_counts",
  188. "verify_duplication",
  189. "write_data_vio",
  190. };
  191. /* The steps taken cleaning up a VIO, in the order they are performed. */
  192. enum data_vio_cleanup_stage {
  193. VIO_CLEANUP_START,
  194. VIO_RELEASE_HASH_LOCK = VIO_CLEANUP_START,
  195. VIO_RELEASE_ALLOCATED,
  196. VIO_RELEASE_RECOVERY_LOCKS,
  197. VIO_RELEASE_LOGICAL,
  198. VIO_CLEANUP_DONE
  199. };
  200. static inline struct data_vio_pool * __must_check
  201. as_data_vio_pool(struct vdo_completion *completion)
  202. {
  203. vdo_assert_completion_type(completion, VDO_DATA_VIO_POOL_COMPLETION);
  204. return container_of(completion, struct data_vio_pool, completion);
  205. }
  206. static inline u64 get_arrival_time(struct bio *bio)
  207. {
  208. return (u64) bio->bi_private;
  209. }
  210. /**
  211. * check_for_drain_complete_locked() - Check whether a data_vio_pool has no outstanding data_vios
  212. * or waiters while holding the pool's lock.
  213. */
  214. static bool check_for_drain_complete_locked(struct data_vio_pool *pool)
  215. {
  216. if (pool->limiter.busy > 0)
  217. return false;
  218. VDO_ASSERT_LOG_ONLY((pool->discard_limiter.busy == 0),
  219. "no outstanding discard permits");
  220. return (bio_list_empty(&pool->limiter.new_waiters) &&
  221. bio_list_empty(&pool->discard_limiter.new_waiters));
  222. }
  223. static void initialize_lbn_lock(struct data_vio *data_vio, logical_block_number_t lbn)
  224. {
  225. struct vdo *vdo = vdo_from_data_vio(data_vio);
  226. zone_count_t zone_number;
  227. struct lbn_lock *lock = &data_vio->logical;
  228. lock->lbn = lbn;
  229. lock->locked = false;
  230. vdo_waitq_init(&lock->waiters);
  231. zone_number = vdo_compute_logical_zone(data_vio);
  232. lock->zone = &vdo->logical_zones->zones[zone_number];
  233. }
  234. static void launch_locked_request(struct data_vio *data_vio)
  235. {
  236. data_vio->logical.locked = true;
  237. if (data_vio->write) {
  238. struct vdo *vdo = vdo_from_data_vio(data_vio);
  239. if (vdo_is_read_only(vdo)) {
  240. continue_data_vio_with_error(data_vio, VDO_READ_ONLY);
  241. return;
  242. }
  243. }
  244. data_vio->last_async_operation = VIO_ASYNC_OP_FIND_BLOCK_MAP_SLOT;
  245. vdo_find_block_map_slot(data_vio);
  246. }
  247. static void acknowledge_data_vio(struct data_vio *data_vio)
  248. {
  249. struct vdo *vdo = vdo_from_data_vio(data_vio);
  250. struct bio *bio = data_vio->user_bio;
  251. int error = vdo_status_to_errno(data_vio->vio.completion.result);
  252. if (bio == NULL)
  253. return;
  254. VDO_ASSERT_LOG_ONLY((data_vio->remaining_discard <=
  255. (u32) (VDO_BLOCK_SIZE - data_vio->offset)),
  256. "data_vio to acknowledge is not an incomplete discard");
  257. data_vio->user_bio = NULL;
  258. vdo_count_bios(&vdo->stats.bios_acknowledged, bio);
  259. if (data_vio->is_partial)
  260. vdo_count_bios(&vdo->stats.bios_acknowledged_partial, bio);
  261. bio->bi_status = errno_to_blk_status(error);
  262. bio_endio(bio);
  263. }
  264. static void copy_to_bio(struct bio *bio, char *data_ptr)
  265. {
  266. struct bio_vec biovec;
  267. struct bvec_iter iter;
  268. bio_for_each_segment(biovec, bio, iter) {
  269. memcpy_to_bvec(&biovec, data_ptr);
  270. data_ptr += biovec.bv_len;
  271. }
  272. }
  273. struct data_vio_compression_status get_data_vio_compression_status(struct data_vio *data_vio)
  274. {
  275. u32 packed = atomic_read(&data_vio->compression.status);
  276. /* pairs with cmpxchg in set_data_vio_compression_status */
  277. smp_rmb();
  278. return (struct data_vio_compression_status) {
  279. .stage = packed & COMPRESSION_STATUS_MASK,
  280. .may_not_compress = ((packed & MAY_NOT_COMPRESS_MASK) != 0),
  281. };
  282. }
  283. /**
  284. * pack_status() - Convert a data_vio_compression_status into a u32 which may be stored
  285. * atomically.
  286. * @status: The state to convert.
  287. *
  288. * Return: The compression state packed into a u32.
  289. */
  290. static u32 __must_check pack_status(struct data_vio_compression_status status)
  291. {
  292. return status.stage | (status.may_not_compress ? MAY_NOT_COMPRESS_MASK : 0);
  293. }
  294. /**
  295. * set_data_vio_compression_status() - Set the compression status of a data_vio.
  296. * @state: The expected current status of the data_vio.
  297. * @new_state: The status to set.
  298. *
  299. * Return: true if the new status was set, false if the data_vio's compression status did not
  300. * match the expected state, and so was left unchanged.
  301. */
  302. static bool __must_check
  303. set_data_vio_compression_status(struct data_vio *data_vio,
  304. struct data_vio_compression_status status,
  305. struct data_vio_compression_status new_status)
  306. {
  307. u32 actual;
  308. u32 expected = pack_status(status);
  309. u32 replacement = pack_status(new_status);
  310. /*
  311. * Extra barriers because this was original developed using a CAS operation that implicitly
  312. * had them.
  313. */
  314. smp_mb__before_atomic();
  315. actual = atomic_cmpxchg(&data_vio->compression.status, expected, replacement);
  316. /* same as before_atomic */
  317. smp_mb__after_atomic();
  318. return (expected == actual);
  319. }
  320. struct data_vio_compression_status advance_data_vio_compression_stage(struct data_vio *data_vio)
  321. {
  322. for (;;) {
  323. struct data_vio_compression_status status =
  324. get_data_vio_compression_status(data_vio);
  325. struct data_vio_compression_status new_status = status;
  326. if (status.stage == DATA_VIO_POST_PACKER) {
  327. /* We're already in the last stage. */
  328. return status;
  329. }
  330. if (status.may_not_compress) {
  331. /*
  332. * Compression has been dis-allowed for this VIO, so skip the rest of the
  333. * path and go to the end.
  334. */
  335. new_status.stage = DATA_VIO_POST_PACKER;
  336. } else {
  337. /* Go to the next state. */
  338. new_status.stage++;
  339. }
  340. if (set_data_vio_compression_status(data_vio, status, new_status))
  341. return new_status;
  342. /* Another thread changed the status out from under us so try again. */
  343. }
  344. }
  345. /**
  346. * cancel_data_vio_compression() - Prevent this data_vio from being compressed or packed.
  347. *
  348. * Return: true if the data_vio is in the packer and the caller was the first caller to cancel it.
  349. */
  350. bool cancel_data_vio_compression(struct data_vio *data_vio)
  351. {
  352. struct data_vio_compression_status status, new_status;
  353. for (;;) {
  354. status = get_data_vio_compression_status(data_vio);
  355. if (status.may_not_compress || (status.stage == DATA_VIO_POST_PACKER)) {
  356. /* This data_vio is already set up to not block in the packer. */
  357. break;
  358. }
  359. new_status.stage = status.stage;
  360. new_status.may_not_compress = true;
  361. if (set_data_vio_compression_status(data_vio, status, new_status))
  362. break;
  363. }
  364. return ((status.stage == DATA_VIO_PACKING) && !status.may_not_compress);
  365. }
  366. /**
  367. * attempt_logical_block_lock() - Attempt to acquire the lock on a logical block.
  368. * @completion: The data_vio for an external data request as a completion.
  369. *
  370. * This is the start of the path for all external requests. It is registered in launch_data_vio().
  371. */
  372. static void attempt_logical_block_lock(struct vdo_completion *completion)
  373. {
  374. struct data_vio *data_vio = as_data_vio(completion);
  375. struct lbn_lock *lock = &data_vio->logical;
  376. struct vdo *vdo = vdo_from_data_vio(data_vio);
  377. struct data_vio *lock_holder;
  378. int result;
  379. assert_data_vio_in_logical_zone(data_vio);
  380. if (data_vio->logical.lbn >= vdo->states.vdo.config.logical_blocks) {
  381. continue_data_vio_with_error(data_vio, VDO_OUT_OF_RANGE);
  382. return;
  383. }
  384. result = vdo_int_map_put(lock->zone->lbn_operations, lock->lbn,
  385. data_vio, false, (void **) &lock_holder);
  386. if (result != VDO_SUCCESS) {
  387. continue_data_vio_with_error(data_vio, result);
  388. return;
  389. }
  390. if (lock_holder == NULL) {
  391. /* We got the lock */
  392. launch_locked_request(data_vio);
  393. return;
  394. }
  395. result = VDO_ASSERT(lock_holder->logical.locked, "logical block lock held");
  396. if (result != VDO_SUCCESS) {
  397. continue_data_vio_with_error(data_vio, result);
  398. return;
  399. }
  400. /*
  401. * If the new request is a pure read request (not read-modify-write) and the lock_holder is
  402. * writing and has received an allocation, service the read request immediately by copying
  403. * data from the lock_holder to avoid having to flush the write out of the packer just to
  404. * prevent the read from waiting indefinitely. If the lock_holder does not yet have an
  405. * allocation, prevent it from blocking in the packer and wait on it. This is necessary in
  406. * order to prevent returning data that may not have actually been written.
  407. */
  408. if (!data_vio->write && READ_ONCE(lock_holder->allocation_succeeded)) {
  409. copy_to_bio(data_vio->user_bio, lock_holder->vio.data + data_vio->offset);
  410. acknowledge_data_vio(data_vio);
  411. complete_data_vio(completion);
  412. return;
  413. }
  414. data_vio->last_async_operation = VIO_ASYNC_OP_ATTEMPT_LOGICAL_BLOCK_LOCK;
  415. vdo_waitq_enqueue_waiter(&lock_holder->logical.waiters, &data_vio->waiter);
  416. /*
  417. * Prevent writes and read-modify-writes from blocking indefinitely on lock holders in the
  418. * packer.
  419. */
  420. if (lock_holder->write && cancel_data_vio_compression(lock_holder)) {
  421. data_vio->compression.lock_holder = lock_holder;
  422. launch_data_vio_packer_callback(data_vio,
  423. vdo_remove_lock_holder_from_packer);
  424. }
  425. }
  426. /**
  427. * launch_data_vio() - (Re)initialize a data_vio to have a new logical block number, keeping the
  428. * same parent and other state and send it on its way.
  429. */
  430. static void launch_data_vio(struct data_vio *data_vio, logical_block_number_t lbn)
  431. {
  432. struct vdo_completion *completion = &data_vio->vio.completion;
  433. /*
  434. * Clearing the tree lock must happen before initializing the LBN lock, which also adds
  435. * information to the tree lock.
  436. */
  437. memset(&data_vio->tree_lock, 0, sizeof(data_vio->tree_lock));
  438. initialize_lbn_lock(data_vio, lbn);
  439. INIT_LIST_HEAD(&data_vio->hash_lock_entry);
  440. INIT_LIST_HEAD(&data_vio->write_entry);
  441. memset(&data_vio->allocation, 0, sizeof(data_vio->allocation));
  442. data_vio->is_duplicate = false;
  443. memset(&data_vio->record_name, 0, sizeof(data_vio->record_name));
  444. memset(&data_vio->duplicate, 0, sizeof(data_vio->duplicate));
  445. vdo_reset_completion(&data_vio->decrement_completion);
  446. vdo_reset_completion(completion);
  447. completion->error_handler = handle_data_vio_error;
  448. set_data_vio_logical_callback(data_vio, attempt_logical_block_lock);
  449. vdo_enqueue_completion(completion, VDO_DEFAULT_Q_MAP_BIO_PRIORITY);
  450. }
  451. static bool is_zero_block(char *block)
  452. {
  453. int i;
  454. for (i = 0; i < VDO_BLOCK_SIZE; i += sizeof(u64)) {
  455. if (*((u64 *) &block[i]))
  456. return false;
  457. }
  458. return true;
  459. }
  460. static void copy_from_bio(struct bio *bio, char *data_ptr)
  461. {
  462. struct bio_vec biovec;
  463. struct bvec_iter iter;
  464. bio_for_each_segment(biovec, bio, iter) {
  465. memcpy_from_bvec(data_ptr, &biovec);
  466. data_ptr += biovec.bv_len;
  467. }
  468. }
  469. static void launch_bio(struct vdo *vdo, struct data_vio *data_vio, struct bio *bio)
  470. {
  471. logical_block_number_t lbn;
  472. /*
  473. * Zero out the fields which don't need to be preserved (i.e. which are not pointers to
  474. * separately allocated objects).
  475. */
  476. memset(data_vio, 0, offsetof(struct data_vio, vio));
  477. memset(&data_vio->compression, 0, offsetof(struct compression_state, block));
  478. data_vio->user_bio = bio;
  479. data_vio->offset = to_bytes(bio->bi_iter.bi_sector & VDO_SECTORS_PER_BLOCK_MASK);
  480. data_vio->is_partial = (bio->bi_iter.bi_size < VDO_BLOCK_SIZE) || (data_vio->offset != 0);
  481. /*
  482. * Discards behave very differently than other requests when coming in from device-mapper.
  483. * We have to be able to handle any size discards and various sector offsets within a
  484. * block.
  485. */
  486. if (bio_op(bio) == REQ_OP_DISCARD) {
  487. data_vio->remaining_discard = bio->bi_iter.bi_size;
  488. data_vio->write = true;
  489. data_vio->is_discard = true;
  490. if (data_vio->is_partial) {
  491. vdo_count_bios(&vdo->stats.bios_in_partial, bio);
  492. data_vio->read = true;
  493. }
  494. } else if (data_vio->is_partial) {
  495. vdo_count_bios(&vdo->stats.bios_in_partial, bio);
  496. data_vio->read = true;
  497. if (bio_data_dir(bio) == WRITE)
  498. data_vio->write = true;
  499. } else if (bio_data_dir(bio) == READ) {
  500. data_vio->read = true;
  501. } else {
  502. /*
  503. * Copy the bio data to a char array so that we can continue to use the data after
  504. * we acknowledge the bio.
  505. */
  506. copy_from_bio(bio, data_vio->vio.data);
  507. data_vio->is_zero = is_zero_block(data_vio->vio.data);
  508. data_vio->write = true;
  509. }
  510. if (data_vio->user_bio->bi_opf & REQ_FUA)
  511. data_vio->fua = true;
  512. lbn = (bio->bi_iter.bi_sector - vdo->starting_sector_offset) / VDO_SECTORS_PER_BLOCK;
  513. launch_data_vio(data_vio, lbn);
  514. }
  515. static void assign_data_vio(struct limiter *limiter, struct data_vio *data_vio)
  516. {
  517. struct bio *bio = bio_list_pop(limiter->permitted_waiters);
  518. launch_bio(limiter->pool->completion.vdo, data_vio, bio);
  519. limiter->wake_count++;
  520. bio = bio_list_peek(limiter->permitted_waiters);
  521. limiter->arrival = ((bio == NULL) ? U64_MAX : get_arrival_time(bio));
  522. }
  523. static void assign_discard_permit(struct limiter *limiter)
  524. {
  525. struct bio *bio = bio_list_pop(&limiter->waiters);
  526. if (limiter->arrival == U64_MAX)
  527. limiter->arrival = get_arrival_time(bio);
  528. bio_list_add(limiter->permitted_waiters, bio);
  529. }
  530. static void get_waiters(struct limiter *limiter)
  531. {
  532. bio_list_merge_init(&limiter->waiters, &limiter->new_waiters);
  533. }
  534. static inline struct data_vio *get_available_data_vio(struct data_vio_pool *pool)
  535. {
  536. struct data_vio *data_vio =
  537. list_first_entry(&pool->available, struct data_vio, pool_entry);
  538. list_del_init(&data_vio->pool_entry);
  539. return data_vio;
  540. }
  541. static void assign_data_vio_to_waiter(struct limiter *limiter)
  542. {
  543. assign_data_vio(limiter, get_available_data_vio(limiter->pool));
  544. }
  545. static void update_limiter(struct limiter *limiter)
  546. {
  547. struct bio_list *waiters = &limiter->waiters;
  548. data_vio_count_t available = limiter->limit - limiter->busy;
  549. VDO_ASSERT_LOG_ONLY((limiter->release_count <= limiter->busy),
  550. "Release count %u is not more than busy count %u",
  551. limiter->release_count, limiter->busy);
  552. get_waiters(limiter);
  553. for (; (limiter->release_count > 0) && !bio_list_empty(waiters); limiter->release_count--)
  554. limiter->assigner(limiter);
  555. if (limiter->release_count > 0) {
  556. WRITE_ONCE(limiter->busy, limiter->busy - limiter->release_count);
  557. limiter->release_count = 0;
  558. return;
  559. }
  560. for (; (available > 0) && !bio_list_empty(waiters); available--)
  561. limiter->assigner(limiter);
  562. WRITE_ONCE(limiter->busy, limiter->limit - available);
  563. if (limiter->max_busy < limiter->busy)
  564. WRITE_ONCE(limiter->max_busy, limiter->busy);
  565. }
  566. /**
  567. * schedule_releases() - Ensure that release processing is scheduled.
  568. *
  569. * If this call switches the state to processing, enqueue. Otherwise, some other thread has already
  570. * done so.
  571. */
  572. static void schedule_releases(struct data_vio_pool *pool)
  573. {
  574. /* Pairs with the barrier in process_release_callback(). */
  575. smp_mb__before_atomic();
  576. if (atomic_cmpxchg(&pool->processing, false, true))
  577. return;
  578. pool->completion.requeue = true;
  579. vdo_launch_completion_with_priority(&pool->completion,
  580. CPU_Q_COMPLETE_VIO_PRIORITY);
  581. }
  582. static void reuse_or_release_resources(struct data_vio_pool *pool,
  583. struct data_vio *data_vio,
  584. struct list_head *returned)
  585. {
  586. if (data_vio->remaining_discard > 0) {
  587. if (bio_list_empty(&pool->discard_limiter.waiters)) {
  588. /* Return the data_vio's discard permit. */
  589. pool->discard_limiter.release_count++;
  590. } else {
  591. assign_discard_permit(&pool->discard_limiter);
  592. }
  593. }
  594. if (pool->limiter.arrival < pool->discard_limiter.arrival) {
  595. assign_data_vio(&pool->limiter, data_vio);
  596. } else if (pool->discard_limiter.arrival < U64_MAX) {
  597. assign_data_vio(&pool->discard_limiter, data_vio);
  598. } else {
  599. list_add(&data_vio->pool_entry, returned);
  600. pool->limiter.release_count++;
  601. }
  602. }
  603. /**
  604. * process_release_callback() - Process a batch of data_vio releases.
  605. * @completion: The pool with data_vios to release.
  606. */
  607. static void process_release_callback(struct vdo_completion *completion)
  608. {
  609. struct data_vio_pool *pool = as_data_vio_pool(completion);
  610. bool reschedule;
  611. bool drained;
  612. data_vio_count_t processed;
  613. data_vio_count_t to_wake;
  614. data_vio_count_t discards_to_wake;
  615. LIST_HEAD(returned);
  616. spin_lock(&pool->lock);
  617. get_waiters(&pool->discard_limiter);
  618. get_waiters(&pool->limiter);
  619. spin_unlock(&pool->lock);
  620. if (pool->limiter.arrival == U64_MAX) {
  621. struct bio *bio = bio_list_peek(&pool->limiter.waiters);
  622. if (bio != NULL)
  623. pool->limiter.arrival = get_arrival_time(bio);
  624. }
  625. for (processed = 0; processed < DATA_VIO_RELEASE_BATCH_SIZE; processed++) {
  626. struct data_vio *data_vio;
  627. struct funnel_queue_entry *entry = vdo_funnel_queue_poll(pool->queue);
  628. if (entry == NULL)
  629. break;
  630. data_vio = as_data_vio(container_of(entry, struct vdo_completion,
  631. work_queue_entry_link));
  632. acknowledge_data_vio(data_vio);
  633. reuse_or_release_resources(pool, data_vio, &returned);
  634. }
  635. spin_lock(&pool->lock);
  636. /*
  637. * There is a race where waiters could be added while we are in the unlocked section above.
  638. * Those waiters could not see the resources we are now about to release, so we assign
  639. * those resources now as we have no guarantee of being rescheduled. This is handled in
  640. * update_limiter().
  641. */
  642. update_limiter(&pool->discard_limiter);
  643. list_splice(&returned, &pool->available);
  644. update_limiter(&pool->limiter);
  645. to_wake = pool->limiter.wake_count;
  646. pool->limiter.wake_count = 0;
  647. discards_to_wake = pool->discard_limiter.wake_count;
  648. pool->discard_limiter.wake_count = 0;
  649. atomic_set(&pool->processing, false);
  650. /* Pairs with the barrier in schedule_releases(). */
  651. smp_mb();
  652. reschedule = !vdo_is_funnel_queue_empty(pool->queue);
  653. drained = (!reschedule &&
  654. vdo_is_state_draining(&pool->state) &&
  655. check_for_drain_complete_locked(pool));
  656. spin_unlock(&pool->lock);
  657. if (to_wake > 0)
  658. wake_up_nr(&pool->limiter.blocked_threads, to_wake);
  659. if (discards_to_wake > 0)
  660. wake_up_nr(&pool->discard_limiter.blocked_threads, discards_to_wake);
  661. if (reschedule)
  662. schedule_releases(pool);
  663. else if (drained)
  664. vdo_finish_draining(&pool->state);
  665. }
  666. static void initialize_limiter(struct limiter *limiter, struct data_vio_pool *pool,
  667. assigner_fn assigner, data_vio_count_t limit)
  668. {
  669. limiter->pool = pool;
  670. limiter->assigner = assigner;
  671. limiter->limit = limit;
  672. limiter->arrival = U64_MAX;
  673. init_waitqueue_head(&limiter->blocked_threads);
  674. }
  675. /**
  676. * initialize_data_vio() - Allocate the components of a data_vio.
  677. *
  678. * The caller is responsible for cleaning up the data_vio on error.
  679. *
  680. * Return: VDO_SUCCESS or an error.
  681. */
  682. static int initialize_data_vio(struct data_vio *data_vio, struct vdo *vdo)
  683. {
  684. struct bio *bio;
  685. int result;
  686. BUILD_BUG_ON(VDO_BLOCK_SIZE > PAGE_SIZE);
  687. result = vdo_allocate_memory(VDO_BLOCK_SIZE, 0, "data_vio data",
  688. &data_vio->vio.data);
  689. if (result != VDO_SUCCESS)
  690. return vdo_log_error_strerror(result,
  691. "data_vio data allocation failure");
  692. result = vdo_allocate_memory(VDO_BLOCK_SIZE, 0, "compressed block",
  693. &data_vio->compression.block);
  694. if (result != VDO_SUCCESS) {
  695. return vdo_log_error_strerror(result,
  696. "data_vio compressed block allocation failure");
  697. }
  698. result = vdo_allocate_memory(VDO_BLOCK_SIZE, 0, "vio scratch",
  699. &data_vio->scratch_block);
  700. if (result != VDO_SUCCESS)
  701. return vdo_log_error_strerror(result,
  702. "data_vio scratch allocation failure");
  703. result = vdo_create_bio(&bio);
  704. if (result != VDO_SUCCESS)
  705. return vdo_log_error_strerror(result,
  706. "data_vio data bio allocation failure");
  707. vdo_initialize_completion(&data_vio->decrement_completion, vdo,
  708. VDO_DECREMENT_COMPLETION);
  709. initialize_vio(&data_vio->vio, bio, 1, VIO_TYPE_DATA, VIO_PRIORITY_DATA, vdo);
  710. return VDO_SUCCESS;
  711. }
  712. static void destroy_data_vio(struct data_vio *data_vio)
  713. {
  714. if (data_vio == NULL)
  715. return;
  716. vdo_free_bio(vdo_forget(data_vio->vio.bio));
  717. vdo_free(vdo_forget(data_vio->vio.data));
  718. vdo_free(vdo_forget(data_vio->compression.block));
  719. vdo_free(vdo_forget(data_vio->scratch_block));
  720. }
  721. /**
  722. * make_data_vio_pool() - Initialize a data_vio pool.
  723. * @vdo: The vdo to which the pool will belong.
  724. * @pool_size: The number of data_vios in the pool.
  725. * @discard_limit: The maximum number of data_vios which may be used for discards.
  726. * @pool: A pointer to hold the newly allocated pool.
  727. */
  728. int make_data_vio_pool(struct vdo *vdo, data_vio_count_t pool_size,
  729. data_vio_count_t discard_limit, struct data_vio_pool **pool_ptr)
  730. {
  731. int result;
  732. struct data_vio_pool *pool;
  733. data_vio_count_t i;
  734. result = vdo_allocate_extended(struct data_vio_pool, pool_size, struct data_vio,
  735. __func__, &pool);
  736. if (result != VDO_SUCCESS)
  737. return result;
  738. VDO_ASSERT_LOG_ONLY((discard_limit <= pool_size),
  739. "discard limit does not exceed pool size");
  740. initialize_limiter(&pool->discard_limiter, pool, assign_discard_permit,
  741. discard_limit);
  742. pool->discard_limiter.permitted_waiters = &pool->permitted_discards;
  743. initialize_limiter(&pool->limiter, pool, assign_data_vio_to_waiter, pool_size);
  744. pool->limiter.permitted_waiters = &pool->limiter.waiters;
  745. INIT_LIST_HEAD(&pool->available);
  746. spin_lock_init(&pool->lock);
  747. vdo_set_admin_state_code(&pool->state, VDO_ADMIN_STATE_NORMAL_OPERATION);
  748. vdo_initialize_completion(&pool->completion, vdo, VDO_DATA_VIO_POOL_COMPLETION);
  749. vdo_prepare_completion(&pool->completion, process_release_callback,
  750. process_release_callback, vdo->thread_config.cpu_thread,
  751. NULL);
  752. result = vdo_make_funnel_queue(&pool->queue);
  753. if (result != VDO_SUCCESS) {
  754. free_data_vio_pool(vdo_forget(pool));
  755. return result;
  756. }
  757. for (i = 0; i < pool_size; i++) {
  758. struct data_vio *data_vio = &pool->data_vios[i];
  759. result = initialize_data_vio(data_vio, vdo);
  760. if (result != VDO_SUCCESS) {
  761. destroy_data_vio(data_vio);
  762. free_data_vio_pool(pool);
  763. return result;
  764. }
  765. list_add(&data_vio->pool_entry, &pool->available);
  766. }
  767. *pool_ptr = pool;
  768. return VDO_SUCCESS;
  769. }
  770. /**
  771. * free_data_vio_pool() - Free a data_vio_pool and the data_vios in it.
  772. *
  773. * All data_vios must be returned to the pool before calling this function.
  774. */
  775. void free_data_vio_pool(struct data_vio_pool *pool)
  776. {
  777. struct data_vio *data_vio, *tmp;
  778. if (pool == NULL)
  779. return;
  780. /*
  781. * Pairs with the barrier in process_release_callback(). Possibly not needed since it
  782. * caters to an enqueue vs. free race.
  783. */
  784. smp_mb();
  785. BUG_ON(atomic_read(&pool->processing));
  786. spin_lock(&pool->lock);
  787. VDO_ASSERT_LOG_ONLY((pool->limiter.busy == 0),
  788. "data_vio pool must not have %u busy entries when being freed",
  789. pool->limiter.busy);
  790. VDO_ASSERT_LOG_ONLY((bio_list_empty(&pool->limiter.waiters) &&
  791. bio_list_empty(&pool->limiter.new_waiters)),
  792. "data_vio pool must not have threads waiting to read or write when being freed");
  793. VDO_ASSERT_LOG_ONLY((bio_list_empty(&pool->discard_limiter.waiters) &&
  794. bio_list_empty(&pool->discard_limiter.new_waiters)),
  795. "data_vio pool must not have threads waiting to discard when being freed");
  796. spin_unlock(&pool->lock);
  797. list_for_each_entry_safe(data_vio, tmp, &pool->available, pool_entry) {
  798. list_del_init(&data_vio->pool_entry);
  799. destroy_data_vio(data_vio);
  800. }
  801. vdo_free_funnel_queue(vdo_forget(pool->queue));
  802. vdo_free(pool);
  803. }
  804. static bool acquire_permit(struct limiter *limiter)
  805. {
  806. if (limiter->busy >= limiter->limit)
  807. return false;
  808. WRITE_ONCE(limiter->busy, limiter->busy + 1);
  809. if (limiter->max_busy < limiter->busy)
  810. WRITE_ONCE(limiter->max_busy, limiter->busy);
  811. return true;
  812. }
  813. static void wait_permit(struct limiter *limiter, struct bio *bio)
  814. __releases(&limiter->pool->lock)
  815. {
  816. DEFINE_WAIT(wait);
  817. bio_list_add(&limiter->new_waiters, bio);
  818. prepare_to_wait_exclusive(&limiter->blocked_threads, &wait,
  819. TASK_UNINTERRUPTIBLE);
  820. spin_unlock(&limiter->pool->lock);
  821. io_schedule();
  822. finish_wait(&limiter->blocked_threads, &wait);
  823. }
  824. /**
  825. * vdo_launch_bio() - Acquire a data_vio from the pool, assign the bio to it, and launch it.
  826. *
  827. * This will block if data_vios or discard permits are not available.
  828. */
  829. void vdo_launch_bio(struct data_vio_pool *pool, struct bio *bio)
  830. {
  831. struct data_vio *data_vio;
  832. VDO_ASSERT_LOG_ONLY(!vdo_is_state_quiescent(&pool->state),
  833. "data_vio_pool not quiescent on acquire");
  834. bio->bi_private = (void *) jiffies;
  835. spin_lock(&pool->lock);
  836. if ((bio_op(bio) == REQ_OP_DISCARD) &&
  837. !acquire_permit(&pool->discard_limiter)) {
  838. wait_permit(&pool->discard_limiter, bio);
  839. return;
  840. }
  841. if (!acquire_permit(&pool->limiter)) {
  842. wait_permit(&pool->limiter, bio);
  843. return;
  844. }
  845. data_vio = get_available_data_vio(pool);
  846. spin_unlock(&pool->lock);
  847. launch_bio(pool->completion.vdo, data_vio, bio);
  848. }
  849. /* Implements vdo_admin_initiator_fn. */
  850. static void initiate_drain(struct admin_state *state)
  851. {
  852. bool drained;
  853. struct data_vio_pool *pool = container_of(state, struct data_vio_pool, state);
  854. spin_lock(&pool->lock);
  855. drained = check_for_drain_complete_locked(pool);
  856. spin_unlock(&pool->lock);
  857. if (drained)
  858. vdo_finish_draining(state);
  859. }
  860. static void assert_on_vdo_cpu_thread(const struct vdo *vdo, const char *name)
  861. {
  862. VDO_ASSERT_LOG_ONLY((vdo_get_callback_thread_id() == vdo->thread_config.cpu_thread),
  863. "%s called on cpu thread", name);
  864. }
  865. /**
  866. * drain_data_vio_pool() - Wait asynchronously for all data_vios to be returned to the pool.
  867. * @completion: The completion to notify when the pool has drained.
  868. */
  869. void drain_data_vio_pool(struct data_vio_pool *pool, struct vdo_completion *completion)
  870. {
  871. assert_on_vdo_cpu_thread(completion->vdo, __func__);
  872. vdo_start_draining(&pool->state, VDO_ADMIN_STATE_SUSPENDING, completion,
  873. initiate_drain);
  874. }
  875. /**
  876. * resume_data_vio_pool() - Resume a data_vio pool.
  877. * @completion: The completion to notify when the pool has resumed.
  878. */
  879. void resume_data_vio_pool(struct data_vio_pool *pool, struct vdo_completion *completion)
  880. {
  881. assert_on_vdo_cpu_thread(completion->vdo, __func__);
  882. vdo_continue_completion(completion, vdo_resume_if_quiescent(&pool->state));
  883. }
  884. static void dump_limiter(const char *name, struct limiter *limiter)
  885. {
  886. vdo_log_info("%s: %u of %u busy (max %u), %s", name, limiter->busy,
  887. limiter->limit, limiter->max_busy,
  888. ((bio_list_empty(&limiter->waiters) &&
  889. bio_list_empty(&limiter->new_waiters)) ?
  890. "no waiters" : "has waiters"));
  891. }
  892. /**
  893. * dump_data_vio_pool() - Dump a data_vio pool to the log.
  894. * @dump_vios: Whether to dump the details of each busy data_vio as well.
  895. */
  896. void dump_data_vio_pool(struct data_vio_pool *pool, bool dump_vios)
  897. {
  898. /*
  899. * In order that syslog can empty its buffer, sleep after 35 elements for 4ms (till the
  900. * second clock tick). These numbers were picked based on experiments with lab machines.
  901. */
  902. static const int ELEMENTS_PER_BATCH = 35;
  903. static const int SLEEP_FOR_SYSLOG = 4000;
  904. if (pool == NULL)
  905. return;
  906. spin_lock(&pool->lock);
  907. dump_limiter("data_vios", &pool->limiter);
  908. dump_limiter("discard permits", &pool->discard_limiter);
  909. if (dump_vios) {
  910. int i;
  911. int dumped = 0;
  912. for (i = 0; i < pool->limiter.limit; i++) {
  913. struct data_vio *data_vio = &pool->data_vios[i];
  914. if (!list_empty(&data_vio->pool_entry))
  915. continue;
  916. dump_data_vio(data_vio);
  917. if (++dumped >= ELEMENTS_PER_BATCH) {
  918. spin_unlock(&pool->lock);
  919. dumped = 0;
  920. fsleep(SLEEP_FOR_SYSLOG);
  921. spin_lock(&pool->lock);
  922. }
  923. }
  924. }
  925. spin_unlock(&pool->lock);
  926. }
  927. data_vio_count_t get_data_vio_pool_active_discards(struct data_vio_pool *pool)
  928. {
  929. return READ_ONCE(pool->discard_limiter.busy);
  930. }
  931. data_vio_count_t get_data_vio_pool_discard_limit(struct data_vio_pool *pool)
  932. {
  933. return READ_ONCE(pool->discard_limiter.limit);
  934. }
  935. data_vio_count_t get_data_vio_pool_maximum_discards(struct data_vio_pool *pool)
  936. {
  937. return READ_ONCE(pool->discard_limiter.max_busy);
  938. }
  939. int set_data_vio_pool_discard_limit(struct data_vio_pool *pool, data_vio_count_t limit)
  940. {
  941. if (get_data_vio_pool_request_limit(pool) < limit) {
  942. // The discard limit may not be higher than the data_vio limit.
  943. return -EINVAL;
  944. }
  945. spin_lock(&pool->lock);
  946. pool->discard_limiter.limit = limit;
  947. spin_unlock(&pool->lock);
  948. return VDO_SUCCESS;
  949. }
  950. data_vio_count_t get_data_vio_pool_active_requests(struct data_vio_pool *pool)
  951. {
  952. return READ_ONCE(pool->limiter.busy);
  953. }
  954. data_vio_count_t get_data_vio_pool_request_limit(struct data_vio_pool *pool)
  955. {
  956. return READ_ONCE(pool->limiter.limit);
  957. }
  958. data_vio_count_t get_data_vio_pool_maximum_requests(struct data_vio_pool *pool)
  959. {
  960. return READ_ONCE(pool->limiter.max_busy);
  961. }
  962. static void update_data_vio_error_stats(struct data_vio *data_vio)
  963. {
  964. u8 index = 0;
  965. static const char * const operations[] = {
  966. [0] = "empty",
  967. [1] = "read",
  968. [2] = "write",
  969. [3] = "read-modify-write",
  970. [5] = "read+fua",
  971. [6] = "write+fua",
  972. [7] = "read-modify-write+fua",
  973. };
  974. if (data_vio->read)
  975. index = 1;
  976. if (data_vio->write)
  977. index += 2;
  978. if (data_vio->fua)
  979. index += 4;
  980. update_vio_error_stats(&data_vio->vio,
  981. "Completing %s vio for LBN %llu with error after %s",
  982. operations[index],
  983. (unsigned long long) data_vio->logical.lbn,
  984. get_data_vio_operation_name(data_vio));
  985. }
  986. static void perform_cleanup_stage(struct data_vio *data_vio,
  987. enum data_vio_cleanup_stage stage);
  988. /**
  989. * release_allocated_lock() - Release the PBN lock and/or the reference on the allocated block at
  990. * the end of processing a data_vio.
  991. */
  992. static void release_allocated_lock(struct vdo_completion *completion)
  993. {
  994. struct data_vio *data_vio = as_data_vio(completion);
  995. assert_data_vio_in_allocated_zone(data_vio);
  996. release_data_vio_allocation_lock(data_vio, false);
  997. perform_cleanup_stage(data_vio, VIO_RELEASE_RECOVERY_LOCKS);
  998. }
  999. /** release_lock() - Release an uncontended LBN lock. */
  1000. static void release_lock(struct data_vio *data_vio, struct lbn_lock *lock)
  1001. {
  1002. struct int_map *lock_map = lock->zone->lbn_operations;
  1003. struct data_vio *lock_holder;
  1004. if (!lock->locked) {
  1005. /* The lock is not locked, so it had better not be registered in the lock map. */
  1006. struct data_vio *lock_holder = vdo_int_map_get(lock_map, lock->lbn);
  1007. VDO_ASSERT_LOG_ONLY((data_vio != lock_holder),
  1008. "no logical block lock held for block %llu",
  1009. (unsigned long long) lock->lbn);
  1010. return;
  1011. }
  1012. /* Release the lock by removing the lock from the map. */
  1013. lock_holder = vdo_int_map_remove(lock_map, lock->lbn);
  1014. VDO_ASSERT_LOG_ONLY((data_vio == lock_holder),
  1015. "logical block lock mismatch for block %llu",
  1016. (unsigned long long) lock->lbn);
  1017. lock->locked = false;
  1018. }
  1019. /** transfer_lock() - Transfer a contended LBN lock to the eldest waiter. */
  1020. static void transfer_lock(struct data_vio *data_vio, struct lbn_lock *lock)
  1021. {
  1022. struct data_vio *lock_holder, *next_lock_holder;
  1023. int result;
  1024. VDO_ASSERT_LOG_ONLY(lock->locked, "lbn_lock with waiters is not locked");
  1025. /* Another data_vio is waiting for the lock, transfer it in a single lock map operation. */
  1026. next_lock_holder =
  1027. vdo_waiter_as_data_vio(vdo_waitq_dequeue_waiter(&lock->waiters));
  1028. /* Transfer the remaining lock waiters to the next lock holder. */
  1029. vdo_waitq_transfer_all_waiters(&lock->waiters,
  1030. &next_lock_holder->logical.waiters);
  1031. result = vdo_int_map_put(lock->zone->lbn_operations, lock->lbn,
  1032. next_lock_holder, true, (void **) &lock_holder);
  1033. if (result != VDO_SUCCESS) {
  1034. continue_data_vio_with_error(next_lock_holder, result);
  1035. return;
  1036. }
  1037. VDO_ASSERT_LOG_ONLY((lock_holder == data_vio),
  1038. "logical block lock mismatch for block %llu",
  1039. (unsigned long long) lock->lbn);
  1040. lock->locked = false;
  1041. /*
  1042. * If there are still waiters, other data_vios must be trying to get the lock we just
  1043. * transferred. We must ensure that the new lock holder doesn't block in the packer.
  1044. */
  1045. if (vdo_waitq_has_waiters(&next_lock_holder->logical.waiters))
  1046. cancel_data_vio_compression(next_lock_holder);
  1047. /*
  1048. * Avoid stack overflow on lock transfer.
  1049. * FIXME: this is only an issue in the 1 thread config.
  1050. */
  1051. next_lock_holder->vio.completion.requeue = true;
  1052. launch_locked_request(next_lock_holder);
  1053. }
  1054. /**
  1055. * release_logical_lock() - Release the logical block lock and flush generation lock at the end of
  1056. * processing a data_vio.
  1057. */
  1058. static void release_logical_lock(struct vdo_completion *completion)
  1059. {
  1060. struct data_vio *data_vio = as_data_vio(completion);
  1061. struct lbn_lock *lock = &data_vio->logical;
  1062. assert_data_vio_in_logical_zone(data_vio);
  1063. if (vdo_waitq_has_waiters(&lock->waiters))
  1064. transfer_lock(data_vio, lock);
  1065. else
  1066. release_lock(data_vio, lock);
  1067. vdo_release_flush_generation_lock(data_vio);
  1068. perform_cleanup_stage(data_vio, VIO_CLEANUP_DONE);
  1069. }
  1070. /** clean_hash_lock() - Release the hash lock at the end of processing a data_vio. */
  1071. static void clean_hash_lock(struct vdo_completion *completion)
  1072. {
  1073. struct data_vio *data_vio = as_data_vio(completion);
  1074. assert_data_vio_in_hash_zone(data_vio);
  1075. if (completion->result != VDO_SUCCESS) {
  1076. vdo_clean_failed_hash_lock(data_vio);
  1077. return;
  1078. }
  1079. vdo_release_hash_lock(data_vio);
  1080. perform_cleanup_stage(data_vio, VIO_RELEASE_LOGICAL);
  1081. }
  1082. /**
  1083. * finish_cleanup() - Make some assertions about a data_vio which has finished cleaning up.
  1084. *
  1085. * If it is part of a multi-block discard, starts on the next block, otherwise, returns it to the
  1086. * pool.
  1087. */
  1088. static void finish_cleanup(struct data_vio *data_vio)
  1089. {
  1090. struct vdo_completion *completion = &data_vio->vio.completion;
  1091. u32 discard_size = min_t(u32, data_vio->remaining_discard,
  1092. VDO_BLOCK_SIZE - data_vio->offset);
  1093. VDO_ASSERT_LOG_ONLY(data_vio->allocation.lock == NULL,
  1094. "complete data_vio has no allocation lock");
  1095. VDO_ASSERT_LOG_ONLY(data_vio->hash_lock == NULL,
  1096. "complete data_vio has no hash lock");
  1097. if ((data_vio->remaining_discard <= discard_size) ||
  1098. (completion->result != VDO_SUCCESS)) {
  1099. struct data_vio_pool *pool = completion->vdo->data_vio_pool;
  1100. vdo_funnel_queue_put(pool->queue, &completion->work_queue_entry_link);
  1101. schedule_releases(pool);
  1102. return;
  1103. }
  1104. data_vio->remaining_discard -= discard_size;
  1105. data_vio->is_partial = (data_vio->remaining_discard < VDO_BLOCK_SIZE);
  1106. data_vio->read = data_vio->is_partial;
  1107. data_vio->offset = 0;
  1108. completion->requeue = true;
  1109. data_vio->first_reference_operation_complete = false;
  1110. launch_data_vio(data_vio, data_vio->logical.lbn + 1);
  1111. }
  1112. /** perform_cleanup_stage() - Perform the next step in the process of cleaning up a data_vio. */
  1113. static void perform_cleanup_stage(struct data_vio *data_vio,
  1114. enum data_vio_cleanup_stage stage)
  1115. {
  1116. struct vdo *vdo = vdo_from_data_vio(data_vio);
  1117. switch (stage) {
  1118. case VIO_RELEASE_HASH_LOCK:
  1119. if (data_vio->hash_lock != NULL) {
  1120. launch_data_vio_hash_zone_callback(data_vio, clean_hash_lock);
  1121. return;
  1122. }
  1123. fallthrough;
  1124. case VIO_RELEASE_ALLOCATED:
  1125. if (data_vio_has_allocation(data_vio)) {
  1126. launch_data_vio_allocated_zone_callback(data_vio,
  1127. release_allocated_lock);
  1128. return;
  1129. }
  1130. fallthrough;
  1131. case VIO_RELEASE_RECOVERY_LOCKS:
  1132. if ((data_vio->recovery_sequence_number > 0) &&
  1133. (READ_ONCE(vdo->read_only_notifier.read_only_error) == VDO_SUCCESS) &&
  1134. (data_vio->vio.completion.result != VDO_READ_ONLY))
  1135. vdo_log_warning("VDO not read-only when cleaning data_vio with RJ lock");
  1136. fallthrough;
  1137. case VIO_RELEASE_LOGICAL:
  1138. launch_data_vio_logical_callback(data_vio, release_logical_lock);
  1139. return;
  1140. default:
  1141. finish_cleanup(data_vio);
  1142. }
  1143. }
  1144. void complete_data_vio(struct vdo_completion *completion)
  1145. {
  1146. struct data_vio *data_vio = as_data_vio(completion);
  1147. completion->error_handler = NULL;
  1148. data_vio->last_async_operation = VIO_ASYNC_OP_CLEANUP;
  1149. perform_cleanup_stage(data_vio,
  1150. (data_vio->write ? VIO_CLEANUP_START : VIO_RELEASE_LOGICAL));
  1151. }
  1152. static void enter_read_only_mode(struct vdo_completion *completion)
  1153. {
  1154. if (vdo_is_read_only(completion->vdo))
  1155. return;
  1156. if (completion->result != VDO_READ_ONLY) {
  1157. struct data_vio *data_vio = as_data_vio(completion);
  1158. vdo_log_error_strerror(completion->result,
  1159. "Preparing to enter read-only mode: data_vio for LBN %llu (becoming mapped to %llu, previously mapped to %llu, allocated %llu) is completing with a fatal error after operation %s",
  1160. (unsigned long long) data_vio->logical.lbn,
  1161. (unsigned long long) data_vio->new_mapped.pbn,
  1162. (unsigned long long) data_vio->mapped.pbn,
  1163. (unsigned long long) data_vio->allocation.pbn,
  1164. get_data_vio_operation_name(data_vio));
  1165. }
  1166. vdo_enter_read_only_mode(completion->vdo, completion->result);
  1167. }
  1168. void handle_data_vio_error(struct vdo_completion *completion)
  1169. {
  1170. struct data_vio *data_vio = as_data_vio(completion);
  1171. if ((completion->result == VDO_READ_ONLY) || (data_vio->user_bio == NULL))
  1172. enter_read_only_mode(completion);
  1173. update_data_vio_error_stats(data_vio);
  1174. complete_data_vio(completion);
  1175. }
  1176. /**
  1177. * get_data_vio_operation_name() - Get the name of the last asynchronous operation performed on a
  1178. * data_vio.
  1179. */
  1180. const char *get_data_vio_operation_name(struct data_vio *data_vio)
  1181. {
  1182. BUILD_BUG_ON((MAX_VIO_ASYNC_OPERATION_NUMBER - MIN_VIO_ASYNC_OPERATION_NUMBER) !=
  1183. ARRAY_SIZE(ASYNC_OPERATION_NAMES));
  1184. return ((data_vio->last_async_operation < MAX_VIO_ASYNC_OPERATION_NUMBER) ?
  1185. ASYNC_OPERATION_NAMES[data_vio->last_async_operation] :
  1186. "unknown async operation");
  1187. }
  1188. /**
  1189. * data_vio_allocate_data_block() - Allocate a data block.
  1190. *
  1191. * @write_lock_type: The type of write lock to obtain on the block.
  1192. * @callback: The callback which will attempt an allocation in the current zone and continue if it
  1193. * succeeds.
  1194. * @error_handler: The handler for errors while allocating.
  1195. */
  1196. void data_vio_allocate_data_block(struct data_vio *data_vio,
  1197. enum pbn_lock_type write_lock_type,
  1198. vdo_action_fn callback, vdo_action_fn error_handler)
  1199. {
  1200. struct allocation *allocation = &data_vio->allocation;
  1201. VDO_ASSERT_LOG_ONLY((allocation->pbn == VDO_ZERO_BLOCK),
  1202. "data_vio does not have an allocation");
  1203. allocation->write_lock_type = write_lock_type;
  1204. allocation->zone = vdo_get_next_allocation_zone(data_vio->logical.zone);
  1205. allocation->first_allocation_zone = allocation->zone->zone_number;
  1206. data_vio->vio.completion.error_handler = error_handler;
  1207. launch_data_vio_allocated_zone_callback(data_vio, callback);
  1208. }
  1209. /**
  1210. * release_data_vio_allocation_lock() - Release the PBN lock on a data_vio's allocated block.
  1211. * @reset: If true, the allocation will be reset (i.e. any allocated pbn will be forgotten).
  1212. *
  1213. * If the reference to the locked block is still provisional, it will be released as well.
  1214. */
  1215. void release_data_vio_allocation_lock(struct data_vio *data_vio, bool reset)
  1216. {
  1217. struct allocation *allocation = &data_vio->allocation;
  1218. physical_block_number_t locked_pbn = allocation->pbn;
  1219. assert_data_vio_in_allocated_zone(data_vio);
  1220. if (reset || vdo_pbn_lock_has_provisional_reference(allocation->lock))
  1221. allocation->pbn = VDO_ZERO_BLOCK;
  1222. vdo_release_physical_zone_pbn_lock(allocation->zone, locked_pbn,
  1223. vdo_forget(allocation->lock));
  1224. }
  1225. /**
  1226. * uncompress_data_vio() - Uncompress the data a data_vio has just read.
  1227. * @mapping_state: The mapping state indicating which fragment to decompress.
  1228. * @buffer: The buffer to receive the uncompressed data.
  1229. */
  1230. int uncompress_data_vio(struct data_vio *data_vio,
  1231. enum block_mapping_state mapping_state, char *buffer)
  1232. {
  1233. int size;
  1234. u16 fragment_offset, fragment_size;
  1235. struct compressed_block *block = data_vio->compression.block;
  1236. int result = vdo_get_compressed_block_fragment(mapping_state, block,
  1237. &fragment_offset, &fragment_size);
  1238. if (result != VDO_SUCCESS) {
  1239. vdo_log_debug("%s: compressed fragment error %d", __func__, result);
  1240. return result;
  1241. }
  1242. size = LZ4_decompress_safe((block->data + fragment_offset), buffer,
  1243. fragment_size, VDO_BLOCK_SIZE);
  1244. if (size != VDO_BLOCK_SIZE) {
  1245. vdo_log_debug("%s: lz4 error", __func__);
  1246. return VDO_INVALID_FRAGMENT;
  1247. }
  1248. return VDO_SUCCESS;
  1249. }
  1250. /**
  1251. * modify_for_partial_write() - Do the modify-write part of a read-modify-write cycle.
  1252. * @completion: The data_vio which has just finished its read.
  1253. *
  1254. * This callback is registered in read_block().
  1255. */
  1256. static void modify_for_partial_write(struct vdo_completion *completion)
  1257. {
  1258. struct data_vio *data_vio = as_data_vio(completion);
  1259. char *data = data_vio->vio.data;
  1260. struct bio *bio = data_vio->user_bio;
  1261. assert_data_vio_on_cpu_thread(data_vio);
  1262. if (bio_op(bio) == REQ_OP_DISCARD) {
  1263. memset(data + data_vio->offset, '\0', min_t(u32,
  1264. data_vio->remaining_discard,
  1265. VDO_BLOCK_SIZE - data_vio->offset));
  1266. } else {
  1267. copy_from_bio(bio, data + data_vio->offset);
  1268. }
  1269. data_vio->is_zero = is_zero_block(data);
  1270. data_vio->read = false;
  1271. launch_data_vio_logical_callback(data_vio,
  1272. continue_data_vio_with_block_map_slot);
  1273. }
  1274. static void complete_read(struct vdo_completion *completion)
  1275. {
  1276. struct data_vio *data_vio = as_data_vio(completion);
  1277. char *data = data_vio->vio.data;
  1278. bool compressed = vdo_is_state_compressed(data_vio->mapped.state);
  1279. assert_data_vio_on_cpu_thread(data_vio);
  1280. if (compressed) {
  1281. int result = uncompress_data_vio(data_vio, data_vio->mapped.state, data);
  1282. if (result != VDO_SUCCESS) {
  1283. continue_data_vio_with_error(data_vio, result);
  1284. return;
  1285. }
  1286. }
  1287. if (data_vio->write) {
  1288. modify_for_partial_write(completion);
  1289. return;
  1290. }
  1291. if (compressed || data_vio->is_partial)
  1292. copy_to_bio(data_vio->user_bio, data + data_vio->offset);
  1293. acknowledge_data_vio(data_vio);
  1294. complete_data_vio(completion);
  1295. }
  1296. static void read_endio(struct bio *bio)
  1297. {
  1298. struct data_vio *data_vio = vio_as_data_vio(bio->bi_private);
  1299. int result = blk_status_to_errno(bio->bi_status);
  1300. vdo_count_completed_bios(bio);
  1301. if (result != VDO_SUCCESS) {
  1302. continue_data_vio_with_error(data_vio, result);
  1303. return;
  1304. }
  1305. launch_data_vio_cpu_callback(data_vio, complete_read,
  1306. CPU_Q_COMPLETE_READ_PRIORITY);
  1307. }
  1308. static void complete_zero_read(struct vdo_completion *completion)
  1309. {
  1310. struct data_vio *data_vio = as_data_vio(completion);
  1311. assert_data_vio_on_cpu_thread(data_vio);
  1312. if (data_vio->is_partial) {
  1313. memset(data_vio->vio.data, 0, VDO_BLOCK_SIZE);
  1314. if (data_vio->write) {
  1315. modify_for_partial_write(completion);
  1316. return;
  1317. }
  1318. } else {
  1319. zero_fill_bio(data_vio->user_bio);
  1320. }
  1321. complete_read(completion);
  1322. }
  1323. /**
  1324. * read_block() - Read a block asynchronously.
  1325. *
  1326. * This is the callback registered in read_block_mapping().
  1327. */
  1328. static void read_block(struct vdo_completion *completion)
  1329. {
  1330. struct data_vio *data_vio = as_data_vio(completion);
  1331. struct vio *vio = as_vio(completion);
  1332. int result = VDO_SUCCESS;
  1333. if (data_vio->mapped.pbn == VDO_ZERO_BLOCK) {
  1334. launch_data_vio_cpu_callback(data_vio, complete_zero_read,
  1335. CPU_Q_COMPLETE_VIO_PRIORITY);
  1336. return;
  1337. }
  1338. data_vio->last_async_operation = VIO_ASYNC_OP_READ_DATA_VIO;
  1339. if (vdo_is_state_compressed(data_vio->mapped.state)) {
  1340. result = vio_reset_bio(vio, (char *) data_vio->compression.block,
  1341. read_endio, REQ_OP_READ, data_vio->mapped.pbn);
  1342. } else {
  1343. blk_opf_t opf = ((data_vio->user_bio->bi_opf & PASSTHROUGH_FLAGS) | REQ_OP_READ);
  1344. if (data_vio->is_partial) {
  1345. result = vio_reset_bio(vio, vio->data, read_endio, opf,
  1346. data_vio->mapped.pbn);
  1347. } else {
  1348. /* A full 4k read. Use the incoming bio to avoid having to copy the data */
  1349. bio_reset(vio->bio, vio->bio->bi_bdev, opf);
  1350. bio_init_clone(data_vio->user_bio->bi_bdev, vio->bio,
  1351. data_vio->user_bio, GFP_KERNEL);
  1352. /* Copy over the original bio iovec and opflags. */
  1353. vdo_set_bio_properties(vio->bio, vio, read_endio, opf,
  1354. data_vio->mapped.pbn);
  1355. }
  1356. }
  1357. if (result != VDO_SUCCESS) {
  1358. continue_data_vio_with_error(data_vio, result);
  1359. return;
  1360. }
  1361. vdo_submit_data_vio(data_vio);
  1362. }
  1363. static inline struct data_vio *
  1364. reference_count_update_completion_as_data_vio(struct vdo_completion *completion)
  1365. {
  1366. if (completion->type == VIO_COMPLETION)
  1367. return as_data_vio(completion);
  1368. return container_of(completion, struct data_vio, decrement_completion);
  1369. }
  1370. /**
  1371. * update_block_map() - Rendezvous of the data_vio and decrement completions after each has
  1372. * made its reference updates. Handle any error from either, or proceed
  1373. * to updating the block map.
  1374. * @completion: The completion of the write in progress.
  1375. */
  1376. static void update_block_map(struct vdo_completion *completion)
  1377. {
  1378. struct data_vio *data_vio = reference_count_update_completion_as_data_vio(completion);
  1379. assert_data_vio_in_logical_zone(data_vio);
  1380. if (!data_vio->first_reference_operation_complete) {
  1381. /* Rendezvous, we're first */
  1382. data_vio->first_reference_operation_complete = true;
  1383. return;
  1384. }
  1385. completion = &data_vio->vio.completion;
  1386. vdo_set_completion_result(completion, data_vio->decrement_completion.result);
  1387. if (completion->result != VDO_SUCCESS) {
  1388. handle_data_vio_error(completion);
  1389. return;
  1390. }
  1391. completion->error_handler = handle_data_vio_error;
  1392. if (data_vio->hash_lock != NULL)
  1393. set_data_vio_hash_zone_callback(data_vio, vdo_continue_hash_lock);
  1394. else
  1395. completion->callback = complete_data_vio;
  1396. data_vio->last_async_operation = VIO_ASYNC_OP_PUT_MAPPED_BLOCK;
  1397. vdo_put_mapped_block(data_vio);
  1398. }
  1399. static void decrement_reference_count(struct vdo_completion *completion)
  1400. {
  1401. struct data_vio *data_vio = container_of(completion, struct data_vio,
  1402. decrement_completion);
  1403. assert_data_vio_in_mapped_zone(data_vio);
  1404. vdo_set_completion_callback(completion, update_block_map,
  1405. data_vio->logical.zone->thread_id);
  1406. completion->error_handler = update_block_map;
  1407. vdo_modify_reference_count(completion, &data_vio->decrement_updater);
  1408. }
  1409. static void increment_reference_count(struct vdo_completion *completion)
  1410. {
  1411. struct data_vio *data_vio = as_data_vio(completion);
  1412. assert_data_vio_in_new_mapped_zone(data_vio);
  1413. if (data_vio->downgrade_allocation_lock) {
  1414. /*
  1415. * Now that the data has been written, it's safe to deduplicate against the
  1416. * block. Downgrade the allocation lock to a read lock so it can be used later by
  1417. * the hash lock. This is done here since it needs to happen sometime before we
  1418. * return to the hash zone, and we are currently on the correct thread. For
  1419. * compressed blocks, the downgrade will have already been done.
  1420. */
  1421. vdo_downgrade_pbn_write_lock(data_vio->allocation.lock, false);
  1422. }
  1423. set_data_vio_logical_callback(data_vio, update_block_map);
  1424. completion->error_handler = update_block_map;
  1425. vdo_modify_reference_count(completion, &data_vio->increment_updater);
  1426. }
  1427. /** journal_remapping() - Add a recovery journal entry for a data remapping. */
  1428. static void journal_remapping(struct vdo_completion *completion)
  1429. {
  1430. struct data_vio *data_vio = as_data_vio(completion);
  1431. assert_data_vio_in_journal_zone(data_vio);
  1432. data_vio->decrement_updater.operation = VDO_JOURNAL_DATA_REMAPPING;
  1433. data_vio->decrement_updater.zpbn = data_vio->mapped;
  1434. if (data_vio->new_mapped.pbn == VDO_ZERO_BLOCK) {
  1435. data_vio->first_reference_operation_complete = true;
  1436. if (data_vio->mapped.pbn == VDO_ZERO_BLOCK)
  1437. set_data_vio_logical_callback(data_vio, update_block_map);
  1438. } else {
  1439. set_data_vio_new_mapped_zone_callback(data_vio,
  1440. increment_reference_count);
  1441. }
  1442. if (data_vio->mapped.pbn == VDO_ZERO_BLOCK) {
  1443. data_vio->first_reference_operation_complete = true;
  1444. } else {
  1445. vdo_set_completion_callback(&data_vio->decrement_completion,
  1446. decrement_reference_count,
  1447. data_vio->mapped.zone->thread_id);
  1448. }
  1449. data_vio->last_async_operation = VIO_ASYNC_OP_JOURNAL_REMAPPING;
  1450. vdo_add_recovery_journal_entry(completion->vdo->recovery_journal, data_vio);
  1451. }
  1452. /**
  1453. * read_old_block_mapping() - Get the previous PBN/LBN mapping of an in-progress write.
  1454. *
  1455. * Gets the previous PBN mapped to this LBN from the block map, so as to make an appropriate
  1456. * journal entry referencing the removal of this LBN->PBN mapping.
  1457. */
  1458. static void read_old_block_mapping(struct vdo_completion *completion)
  1459. {
  1460. struct data_vio *data_vio = as_data_vio(completion);
  1461. assert_data_vio_in_logical_zone(data_vio);
  1462. data_vio->last_async_operation = VIO_ASYNC_OP_GET_MAPPED_BLOCK_FOR_WRITE;
  1463. set_data_vio_journal_callback(data_vio, journal_remapping);
  1464. vdo_get_mapped_block(data_vio);
  1465. }
  1466. void update_metadata_for_data_vio_write(struct data_vio *data_vio, struct pbn_lock *lock)
  1467. {
  1468. data_vio->increment_updater = (struct reference_updater) {
  1469. .operation = VDO_JOURNAL_DATA_REMAPPING,
  1470. .increment = true,
  1471. .zpbn = data_vio->new_mapped,
  1472. .lock = lock,
  1473. };
  1474. launch_data_vio_logical_callback(data_vio, read_old_block_mapping);
  1475. }
  1476. /**
  1477. * pack_compressed_data() - Attempt to pack the compressed data_vio into a block.
  1478. *
  1479. * This is the callback registered in launch_compress_data_vio().
  1480. */
  1481. static void pack_compressed_data(struct vdo_completion *completion)
  1482. {
  1483. struct data_vio *data_vio = as_data_vio(completion);
  1484. assert_data_vio_in_packer_zone(data_vio);
  1485. if (!vdo_get_compressing(vdo_from_data_vio(data_vio)) ||
  1486. get_data_vio_compression_status(data_vio).may_not_compress) {
  1487. write_data_vio(data_vio);
  1488. return;
  1489. }
  1490. data_vio->last_async_operation = VIO_ASYNC_OP_ATTEMPT_PACKING;
  1491. vdo_attempt_packing(data_vio);
  1492. }
  1493. /**
  1494. * compress_data_vio() - Do the actual work of compressing the data on a CPU queue.
  1495. *
  1496. * This callback is registered in launch_compress_data_vio().
  1497. */
  1498. static void compress_data_vio(struct vdo_completion *completion)
  1499. {
  1500. struct data_vio *data_vio = as_data_vio(completion);
  1501. int size;
  1502. assert_data_vio_on_cpu_thread(data_vio);
  1503. /*
  1504. * By putting the compressed data at the start of the compressed block data field, we won't
  1505. * need to copy it if this data_vio becomes a compressed write agent.
  1506. */
  1507. size = LZ4_compress_default(data_vio->vio.data,
  1508. data_vio->compression.block->data, VDO_BLOCK_SIZE,
  1509. VDO_MAX_COMPRESSED_FRAGMENT_SIZE,
  1510. (char *) vdo_get_work_queue_private_data());
  1511. if ((size > 0) && (size < VDO_COMPRESSED_BLOCK_DATA_SIZE)) {
  1512. data_vio->compression.size = size;
  1513. launch_data_vio_packer_callback(data_vio, pack_compressed_data);
  1514. return;
  1515. }
  1516. write_data_vio(data_vio);
  1517. }
  1518. /**
  1519. * launch_compress_data_vio() - Continue a write by attempting to compress the data.
  1520. *
  1521. * This is a re-entry point to vio_write used by hash locks.
  1522. */
  1523. void launch_compress_data_vio(struct data_vio *data_vio)
  1524. {
  1525. VDO_ASSERT_LOG_ONLY(!data_vio->is_duplicate, "compressing a non-duplicate block");
  1526. VDO_ASSERT_LOG_ONLY(data_vio->hash_lock != NULL,
  1527. "data_vio to compress has a hash_lock");
  1528. VDO_ASSERT_LOG_ONLY(data_vio_has_allocation(data_vio),
  1529. "data_vio to compress has an allocation");
  1530. /*
  1531. * There are 4 reasons why a data_vio which has reached this point will not be eligible for
  1532. * compression:
  1533. *
  1534. * 1) Since data_vios can block indefinitely in the packer, it would be bad to do so if the
  1535. * write request also requests FUA.
  1536. *
  1537. * 2) A data_vio should not be compressed when compression is disabled for the vdo.
  1538. *
  1539. * 3) A data_vio could be doing a partial write on behalf of a larger discard which has not
  1540. * yet been acknowledged and hence blocking in the packer would be bad.
  1541. *
  1542. * 4) Some other data_vio may be waiting on this data_vio in which case blocking in the
  1543. * packer would also be bad.
  1544. */
  1545. if (data_vio->fua ||
  1546. !vdo_get_compressing(vdo_from_data_vio(data_vio)) ||
  1547. ((data_vio->user_bio != NULL) && (bio_op(data_vio->user_bio) == REQ_OP_DISCARD)) ||
  1548. (advance_data_vio_compression_stage(data_vio).stage != DATA_VIO_COMPRESSING)) {
  1549. write_data_vio(data_vio);
  1550. return;
  1551. }
  1552. data_vio->last_async_operation = VIO_ASYNC_OP_COMPRESS_DATA_VIO;
  1553. launch_data_vio_cpu_callback(data_vio, compress_data_vio,
  1554. CPU_Q_COMPRESS_BLOCK_PRIORITY);
  1555. }
  1556. /**
  1557. * hash_data_vio() - Hash the data in a data_vio and set the hash zone (which also flags the record
  1558. * name as set).
  1559. * This callback is registered in prepare_for_dedupe().
  1560. */
  1561. static void hash_data_vio(struct vdo_completion *completion)
  1562. {
  1563. struct data_vio *data_vio = as_data_vio(completion);
  1564. assert_data_vio_on_cpu_thread(data_vio);
  1565. VDO_ASSERT_LOG_ONLY(!data_vio->is_zero, "zero blocks should not be hashed");
  1566. murmurhash3_128(data_vio->vio.data, VDO_BLOCK_SIZE, 0x62ea60be,
  1567. &data_vio->record_name);
  1568. data_vio->hash_zone = vdo_select_hash_zone(vdo_from_data_vio(data_vio)->hash_zones,
  1569. &data_vio->record_name);
  1570. data_vio->last_async_operation = VIO_ASYNC_OP_ACQUIRE_VDO_HASH_LOCK;
  1571. launch_data_vio_hash_zone_callback(data_vio, vdo_acquire_hash_lock);
  1572. }
  1573. /** prepare_for_dedupe() - Prepare for the dedupe path after attempting to get an allocation. */
  1574. static void prepare_for_dedupe(struct data_vio *data_vio)
  1575. {
  1576. /* We don't care what thread we are on. */
  1577. VDO_ASSERT_LOG_ONLY(!data_vio->is_zero, "must not prepare to dedupe zero blocks");
  1578. /*
  1579. * Before we can dedupe, we need to know the record name, so the first
  1580. * step is to hash the block data.
  1581. */
  1582. data_vio->last_async_operation = VIO_ASYNC_OP_HASH_DATA_VIO;
  1583. launch_data_vio_cpu_callback(data_vio, hash_data_vio, CPU_Q_HASH_BLOCK_PRIORITY);
  1584. }
  1585. /**
  1586. * write_bio_finished() - This is the bio_end_io function registered in write_block() to be called
  1587. * when a data_vio's write to the underlying storage has completed.
  1588. */
  1589. static void write_bio_finished(struct bio *bio)
  1590. {
  1591. struct data_vio *data_vio = vio_as_data_vio((struct vio *) bio->bi_private);
  1592. vdo_count_completed_bios(bio);
  1593. vdo_set_completion_result(&data_vio->vio.completion,
  1594. blk_status_to_errno(bio->bi_status));
  1595. data_vio->downgrade_allocation_lock = true;
  1596. update_metadata_for_data_vio_write(data_vio, data_vio->allocation.lock);
  1597. }
  1598. /** write_data_vio() - Write a data block to storage without compression. */
  1599. void write_data_vio(struct data_vio *data_vio)
  1600. {
  1601. struct data_vio_compression_status status, new_status;
  1602. int result;
  1603. if (!data_vio_has_allocation(data_vio)) {
  1604. /*
  1605. * There was no space to write this block and we failed to deduplicate or compress
  1606. * it.
  1607. */
  1608. continue_data_vio_with_error(data_vio, VDO_NO_SPACE);
  1609. return;
  1610. }
  1611. new_status = (struct data_vio_compression_status) {
  1612. .stage = DATA_VIO_POST_PACKER,
  1613. .may_not_compress = true,
  1614. };
  1615. do {
  1616. status = get_data_vio_compression_status(data_vio);
  1617. } while ((status.stage != DATA_VIO_POST_PACKER) &&
  1618. !set_data_vio_compression_status(data_vio, status, new_status));
  1619. /* Write the data from the data block buffer. */
  1620. result = vio_reset_bio(&data_vio->vio, data_vio->vio.data,
  1621. write_bio_finished, REQ_OP_WRITE,
  1622. data_vio->allocation.pbn);
  1623. if (result != VDO_SUCCESS) {
  1624. continue_data_vio_with_error(data_vio, result);
  1625. return;
  1626. }
  1627. data_vio->last_async_operation = VIO_ASYNC_OP_WRITE_DATA_VIO;
  1628. vdo_submit_data_vio(data_vio);
  1629. }
  1630. /**
  1631. * acknowledge_write_callback() - Acknowledge a write to the requestor.
  1632. *
  1633. * This callback is registered in allocate_block() and continue_write_with_block_map_slot().
  1634. */
  1635. static void acknowledge_write_callback(struct vdo_completion *completion)
  1636. {
  1637. struct data_vio *data_vio = as_data_vio(completion);
  1638. struct vdo *vdo = completion->vdo;
  1639. VDO_ASSERT_LOG_ONLY((!vdo_uses_bio_ack_queue(vdo) ||
  1640. (vdo_get_callback_thread_id() == vdo->thread_config.bio_ack_thread)),
  1641. "%s() called on bio ack queue", __func__);
  1642. VDO_ASSERT_LOG_ONLY(data_vio_has_flush_generation_lock(data_vio),
  1643. "write VIO to be acknowledged has a flush generation lock");
  1644. acknowledge_data_vio(data_vio);
  1645. if (data_vio->new_mapped.pbn == VDO_ZERO_BLOCK) {
  1646. /* This is a zero write or discard */
  1647. update_metadata_for_data_vio_write(data_vio, NULL);
  1648. return;
  1649. }
  1650. prepare_for_dedupe(data_vio);
  1651. }
  1652. /**
  1653. * allocate_block() - Attempt to allocate a block in the current allocation zone.
  1654. *
  1655. * This callback is registered in continue_write_with_block_map_slot().
  1656. */
  1657. static void allocate_block(struct vdo_completion *completion)
  1658. {
  1659. struct data_vio *data_vio = as_data_vio(completion);
  1660. assert_data_vio_in_allocated_zone(data_vio);
  1661. if (!vdo_allocate_block_in_zone(data_vio))
  1662. return;
  1663. completion->error_handler = handle_data_vio_error;
  1664. WRITE_ONCE(data_vio->allocation_succeeded, true);
  1665. data_vio->new_mapped = (struct zoned_pbn) {
  1666. .zone = data_vio->allocation.zone,
  1667. .pbn = data_vio->allocation.pbn,
  1668. .state = VDO_MAPPING_STATE_UNCOMPRESSED,
  1669. };
  1670. if (data_vio->fua ||
  1671. data_vio->remaining_discard > (u32) (VDO_BLOCK_SIZE - data_vio->offset)) {
  1672. prepare_for_dedupe(data_vio);
  1673. return;
  1674. }
  1675. data_vio->last_async_operation = VIO_ASYNC_OP_ACKNOWLEDGE_WRITE;
  1676. launch_data_vio_on_bio_ack_queue(data_vio, acknowledge_write_callback);
  1677. }
  1678. /**
  1679. * handle_allocation_error() - Handle an error attempting to allocate a block.
  1680. *
  1681. * This error handler is registered in continue_write_with_block_map_slot().
  1682. */
  1683. static void handle_allocation_error(struct vdo_completion *completion)
  1684. {
  1685. struct data_vio *data_vio = as_data_vio(completion);
  1686. if (completion->result == VDO_NO_SPACE) {
  1687. /* We failed to get an allocation, but we can try to dedupe. */
  1688. vdo_reset_completion(completion);
  1689. completion->error_handler = handle_data_vio_error;
  1690. prepare_for_dedupe(data_vio);
  1691. return;
  1692. }
  1693. /* We got a "real" error, not just a failure to allocate, so fail the request. */
  1694. handle_data_vio_error(completion);
  1695. }
  1696. static int assert_is_discard(struct data_vio *data_vio)
  1697. {
  1698. int result = VDO_ASSERT(data_vio->is_discard,
  1699. "data_vio with no block map page is a discard");
  1700. return ((result == VDO_SUCCESS) ? result : VDO_READ_ONLY);
  1701. }
  1702. /**
  1703. * continue_data_vio_with_block_map_slot() - Read the data_vio's mapping from the block map.
  1704. *
  1705. * This callback is registered in launch_read_data_vio().
  1706. */
  1707. void continue_data_vio_with_block_map_slot(struct vdo_completion *completion)
  1708. {
  1709. struct data_vio *data_vio = as_data_vio(completion);
  1710. assert_data_vio_in_logical_zone(data_vio);
  1711. if (data_vio->read) {
  1712. set_data_vio_logical_callback(data_vio, read_block);
  1713. data_vio->last_async_operation = VIO_ASYNC_OP_GET_MAPPED_BLOCK_FOR_READ;
  1714. vdo_get_mapped_block(data_vio);
  1715. return;
  1716. }
  1717. vdo_acquire_flush_generation_lock(data_vio);
  1718. if (data_vio->tree_lock.tree_slots[0].block_map_slot.pbn == VDO_ZERO_BLOCK) {
  1719. /*
  1720. * This is a discard for a block on a block map page which has not been allocated, so
  1721. * there's nothing more we need to do.
  1722. */
  1723. completion->callback = complete_data_vio;
  1724. continue_data_vio_with_error(data_vio, assert_is_discard(data_vio));
  1725. return;
  1726. }
  1727. /*
  1728. * We need an allocation if this is neither a full-block discard nor a
  1729. * full-block zero write.
  1730. */
  1731. if (!data_vio->is_zero && (!data_vio->is_discard || data_vio->is_partial)) {
  1732. data_vio_allocate_data_block(data_vio, VIO_WRITE_LOCK, allocate_block,
  1733. handle_allocation_error);
  1734. return;
  1735. }
  1736. /*
  1737. * We don't need to write any data, so skip allocation and just update the block map and
  1738. * reference counts (via the journal).
  1739. */
  1740. data_vio->new_mapped.pbn = VDO_ZERO_BLOCK;
  1741. if (data_vio->is_zero)
  1742. data_vio->new_mapped.state = VDO_MAPPING_STATE_UNCOMPRESSED;
  1743. if (data_vio->remaining_discard > (u32) (VDO_BLOCK_SIZE - data_vio->offset)) {
  1744. /* This is not the final block of a discard so we can't acknowledge it yet. */
  1745. update_metadata_for_data_vio_write(data_vio, NULL);
  1746. return;
  1747. }
  1748. data_vio->last_async_operation = VIO_ASYNC_OP_ACKNOWLEDGE_WRITE;
  1749. launch_data_vio_on_bio_ack_queue(data_vio, acknowledge_write_callback);
  1750. }