Lines Matching full:lock

14  * deduplicate against a single block instead of being serialized through a PBN read lock. Only one
22 * A hash_lock acts like a state machine perhaps more than as a lock. Other than the starting and
25 * containing the lock. An asynchronous operation is almost always performed upon entering a state,
28 * In all states except DEDUPING, there is a single data_vio, called the lock agent, performing the
29 * asynchronous operations on behalf of the lock. The agent will change during the lifetime of the
30 * lock if the lock is shared by more than one data_vio. data_vios waiting to deduplicate are kept
31 * on a wait queue. Viewed a different way, the agent holds the lock exclusively until the lock
32 * enters the DEDUPING state, at which point it becomes a shared lock that all the waiters (and any
33 * new data_vios that arrive) use to share a PBN lock. In state DEDUPING, there is no agent. When
34 * the last data_vio in the lock calls back in DEDUPING, it becomes the agent and the lock becomes
35 * exclusive again. New data_vios that arrive in the lock will also go on the wait queue.
37 * The existence of lock waiters is a key factor controlling which state the lock transitions to
38 * next. When the lock is new or has waiters, it will always try to reach DEDUPING, and when it
41 * Deduping requires holding a PBN lock on a block that is known to contain data identical to the
42 * data_vios in the lock, so the lock will send the agent to the duplicate zone to acquire the PBN
43 * lock (LOCKING), to the kernel I/O threads to read and verify the data (VERIFYING), or to write a
48 * lock on the duplicate block (UNLOCKING), and if the agent is the last data_vio referencing the
49 * lock, releasing the hash_lock itself back to the hash zone (BYPASSING).
53 * This sequence is short because no PBN read lock or index update is needed.
61 * to deduplicate, a new lock is forked and the excess waiters roll over to the new lock (which
62 * goes directly to WRITING). The new lock takes the place of the old lock in the lock map so new
64 * lock will have the right to update the index (unless it also forks).
66 * Since rollover happens in a lock instance, once a valid data location has been selected, it will
67 * not change. QUERYING and WRITING are only performed once per lock lifetime. All other
73 * states) that transition to LOCKING. It performs the actual lock state change and must be invoked
75 * code actually obtaining the lock. It does any bookkeeping or decision-making required and
219 /* The block hash covered by this lock */
222 /* When the lock is unused, this list entry allows the lock to be pooled */
226 * A list containing the data VIOs sharing this lock, all having the same record name and
231 /* The number of data_vios sharing this lock instance */
234 /* The maximum value of reference_count in the lifetime of this lock */
237 /* The current state of this lock */
246 /* True if the lock has already accounted for an initial verification */
249 /* True if this lock is registered in the lock map (cleared on rollover) */
258 /* The PBN lock on the block containing the duplicate data */
261 /* The data_vio designated to act on behalf of the lock */
283 spinlock_t lock; member
285 /* The fields in the next block are all protected by the lock */
338 * return_hash_lock_to_pool() - (Re)initialize a hash lock and return it to its pool.
339 * @zone: The zone from which the lock was borrowed.
340 * @lock: The lock that is no longer in use.
342 static void return_hash_lock_to_pool(struct hash_zone *zone, struct hash_lock *lock) in return_hash_lock_to_pool() argument
344 memset(lock, 0, sizeof(*lock)); in return_hash_lock_to_pool()
345 INIT_LIST_HEAD(&lock->pool_node); in return_hash_lock_to_pool()
346 INIT_LIST_HEAD(&lock->duplicate_ring); in return_hash_lock_to_pool()
347 vdo_waitq_init(&lock->waiters); in return_hash_lock_to_pool()
348 list_add_tail(&lock->pool_node, &zone->lock_pool); in return_hash_lock_to_pool()
352 * vdo_get_duplicate_lock() - Get the PBN lock on the duplicate data location for a data_vio from
356 * Return: The PBN lock on the data_vio's duplicate location.
368 * @lock: The hash lock.
372 static inline u64 hash_lock_key(struct hash_lock *lock) in hash_lock_key() argument
374 return get_unaligned_le64(&lock->hash.name); in hash_lock_key()
378 * get_hash_lock_state_name() - Get the string representation of a hash lock state.
379 * @state: The hash lock state.
391 * assert_hash_lock_agent() - Assert that a data_vio is the agent of its hash lock, and that this
393 * @data_vio: The data_vio expected to be the lock agent.
401 "%s must be for the hash lock agent", where); in assert_hash_lock_agent()
405 * set_duplicate_lock() - Set the duplicate lock held by a hash lock. May only be called in the
406 * physical zone of the PBN lock.
407 * @hash_lock: The hash lock to update.
408 * @pbn_lock: The PBN read lock to use as the duplicate lock.
413 "hash lock must not already hold a duplicate lock"); in set_duplicate_lock()
419 * dequeue_lock_waiter() - Remove the first data_vio from the lock's waitq and return it.
420 * @lock: The lock containing the wait queue.
424 static inline struct data_vio *dequeue_lock_waiter(struct hash_lock *lock) in dequeue_lock_waiter() argument
426 return vdo_waiter_as_data_vio(vdo_waitq_dequeue_waiter(&lock->waiters)); in dequeue_lock_waiter()
430 * set_hash_lock() - Set, change, or clear the hash lock a data_vio is using.
432 * @new_lock: The hash lock the data_vio is joining.
434 * Updates the hash lock (or locks) to reflect the change in membership.
442 "must have a hash zone when holding a hash lock"); in set_hash_lock()
444 "must be on a hash lock ring when holding a hash lock"); in set_hash_lock()
446 "hash lock reference must be counted"); in set_hash_lock()
452 * likely leaking this lock. in set_hash_lock()
467 * Keep all data_vios sharing the lock on a ring since they can complete in any in set_hash_lock()
480 static void start_deduping(struct hash_lock *lock, struct data_vio *agent,
482 static void start_locking(struct hash_lock *lock, struct data_vio *agent);
483 static void start_writing(struct hash_lock *lock, struct data_vio *agent);
489 * longer needed to be an agent for the hash lock.
494 /* Release the hash lock now, saving a thread transition in cleanup. */ in exit_hash_lock()
517 * retire_lock_agent() - Retire the active lock agent, replacing it with the first lock waiter, and
518 * make the retired agent exit the hash lock.
519 * @lock: The hash lock to update.
521 * Return: The new lock agent (which will be NULL if there was no waiter)
523 static struct data_vio *retire_lock_agent(struct hash_lock *lock) in retire_lock_agent() argument
525 struct data_vio *old_agent = lock->agent; in retire_lock_agent()
526 struct data_vio *new_agent = dequeue_lock_waiter(lock); in retire_lock_agent()
528 lock->agent = new_agent; in retire_lock_agent()
531 set_duplicate_location(new_agent, lock->duplicate); in retire_lock_agent()
536 * wait_on_hash_lock() - Add a data_vio to the lock's queue of waiters.
537 * @lock: The hash lock on which to wait.
540 static void wait_on_hash_lock(struct hash_lock *lock, struct data_vio *data_vio) in wait_on_hash_lock() argument
542 vdo_waitq_enqueue_waiter(&lock->waiters, &data_vio->waiter); in wait_on_hash_lock()
548 if ((lock->state != VDO_HASH_LOCK_WRITING) || !cancel_data_vio_compression(lock->agent)) in wait_on_hash_lock()
558 data_vio->compression.lock_holder = lock->agent; in wait_on_hash_lock()
574 * start_bypassing() - Stop using the hash lock.
575 * @lock: The hash lock.
576 * @agent: The data_vio acting as the agent for the lock.
578 * Stops using the hash lock. This is the final transition for hash locks which did not get an
581 static void start_bypassing(struct hash_lock *lock, struct data_vio *agent) in start_bypassing() argument
583 lock->state = VDO_HASH_LOCK_BYPASSING; in start_bypassing()
589 struct hash_lock *lock = data_vio->hash_lock; in vdo_clean_failed_hash_lock() local
591 if (lock->state == VDO_HASH_LOCK_BYPASSING) { in vdo_clean_failed_hash_lock()
596 if (lock->agent == NULL) { in vdo_clean_failed_hash_lock()
597 lock->agent = data_vio; in vdo_clean_failed_hash_lock()
598 } else if (data_vio != lock->agent) { in vdo_clean_failed_hash_lock()
603 lock->state = VDO_HASH_LOCK_BYPASSING; in vdo_clean_failed_hash_lock()
606 lock->update_advice = false; in vdo_clean_failed_hash_lock()
608 vdo_waitq_notify_all_waiters(&lock->waiters, abort_waiter, NULL); in vdo_clean_failed_hash_lock()
610 if (lock->duplicate_lock != NULL) { in vdo_clean_failed_hash_lock()
612 data_vio->duplicate = lock->duplicate; in vdo_clean_failed_hash_lock()
617 lock->agent = NULL; in vdo_clean_failed_hash_lock()
623 * finish_unlocking() - Handle the result of the agent for the lock releasing a read lock on
625 * @completion: The completion of the data_vio acting as the lock's agent.
632 struct hash_lock *lock = agent->hash_lock; in finish_unlocking() local
636 VDO_ASSERT_LOG_ONLY(lock->duplicate_lock == NULL, in finish_unlocking()
637 "must have released the duplicate lock for the hash lock"); in finish_unlocking()
639 if (!lock->verified) { in finish_unlocking()
641 * UNLOCKING -> WRITING transition: The lock we released was on an unverified in finish_unlocking()
642 * block, so it must have been a lock on advice we were verifying, not on a in finish_unlocking()
646 start_writing(lock, agent); in finish_unlocking()
651 * With the lock released, the verified duplicate block may already have changed and will in finish_unlocking()
654 lock->verified = false; in finish_unlocking()
656 if (vdo_waitq_has_waiters(&lock->waiters)) { in finish_unlocking()
658 * UNLOCKING -> LOCKING transition: A new data_vio entered the hash lock while the in finish_unlocking()
659 * agent was releasing the PBN lock. The current agent exits and the waiter has to in finish_unlocking()
660 * re-lock and re-verify the duplicate location. in finish_unlocking()
662 * TODO: If we used the current agent to re-acquire the PBN lock we wouldn't need in finish_unlocking()
665 agent = retire_lock_agent(lock); in finish_unlocking()
666 start_locking(lock, agent); in finish_unlocking()
671 * UNLOCKING -> BYPASSING transition: The agent is done with the lock and no other in finish_unlocking()
672 * data_vios reference it, so remove it from the lock map and return it to the pool. in finish_unlocking()
674 start_bypassing(lock, agent); in finish_unlocking()
678 * unlock_duplicate_pbn() - Release a read lock on the PBN of the block that may or may not have
680 * @completion: The completion of the data_vio acting as the lock's agent.
688 struct hash_lock *lock = agent->hash_lock; in unlock_duplicate_pbn() local
691 VDO_ASSERT_LOG_ONLY(lock->duplicate_lock != NULL, in unlock_duplicate_pbn()
692 "must have a duplicate lock to release"); in unlock_duplicate_pbn()
695 vdo_forget(lock->duplicate_lock)); in unlock_duplicate_pbn()
696 if (lock->state == VDO_HASH_LOCK_BYPASSING) { in unlock_duplicate_pbn()
705 * start_unlocking() - Release a read lock on the PBN of the block that may or may not have
707 * @lock: The hash lock.
708 * @agent: The data_vio currently acting as the agent for the lock.
710 static void start_unlocking(struct hash_lock *lock, struct data_vio *agent) in start_unlocking() argument
712 lock->state = VDO_HASH_LOCK_UNLOCKING; in start_unlocking()
737 * finish_updating() - Process the result of a UDS update performed by the agent for the lock.
745 struct hash_lock *lock = agent->hash_lock; in finish_updating() local
755 lock->update_advice = false; in finish_updating()
757 if (vdo_waitq_has_waiters(&lock->waiters)) { in finish_updating()
760 * Send it on the verified dedupe path. The agent is done with the lock, but the in finish_updating()
761 * lock may still need to use it to clean up after rollover. in finish_updating()
763 start_deduping(lock, agent, true); in finish_updating()
767 if (lock->duplicate_lock != NULL) { in finish_updating()
770 * duplicate PBN lock, so go release it. in finish_updating()
772 start_unlocking(lock, agent); in finish_updating()
777 * UPDATING -> BYPASSING transition: No one is waiting to dedupe and there's no lock to in finish_updating()
780 start_bypassing(lock, agent); in finish_updating()
788 * @lock: The hash lock.
789 * @agent: The data_vio currently acting as the agent for the lock.
791 static void start_updating(struct hash_lock *lock, struct data_vio *agent) in start_updating() argument
793 lock->state = VDO_HASH_LOCK_UPDATING; in start_updating()
795 VDO_ASSERT_LOG_ONLY(lock->verified, "new advice should have been verified"); in start_updating()
796 VDO_ASSERT_LOG_ONLY(lock->update_advice, "should only update advice if needed"); in start_updating()
805 * by the hash lock.
806 * @lock: The hash lock.
807 * @data_vio: The lock holder that has finished deduplicating.
809 * If there are other data_vios still sharing the lock, this will just release the data_vio's share
810 * of the lock and finish processing the data_vio. If this is the last data_vio holding the lock,
811 * this makes the data_vio the lock agent and uses it to advance the state of the lock so it can
814 static void finish_deduping(struct hash_lock *lock, struct data_vio *data_vio) in finish_deduping() argument
818 VDO_ASSERT_LOG_ONLY(lock->agent == NULL, "shouldn't have an agent in DEDUPING"); in finish_deduping()
819 VDO_ASSERT_LOG_ONLY(!vdo_waitq_has_waiters(&lock->waiters), in finish_deduping()
820 "shouldn't have any lock waiters in DEDUPING"); in finish_deduping()
822 /* Just release the lock reference if other data_vios are still deduping. */ in finish_deduping()
823 if (lock->reference_count > 1) { in finish_deduping()
828 /* The hash lock must have an agent for all other lock states. */ in finish_deduping()
829 lock->agent = agent; in finish_deduping()
830 if (lock->update_advice) { in finish_deduping()
835 * was another change in location, but with only this data_vio using the hash lock, in finish_deduping()
838 start_updating(lock, agent); in finish_deduping()
841 * DEDUPING -> UNLOCKING transition: Release the PBN read lock on the duplicate in finish_deduping()
842 * location so the hash lock itself can be released (contingent on no new data_vios in finish_deduping()
843 * arriving in the lock before the agent returns). in finish_deduping()
845 start_unlocking(lock, agent); in finish_deduping()
850 * acquire_lock() - Get the lock for a record name.
852 * @hash: The hash to lock.
853 * @replace_lock: If non-NULL, the lock already registered for the hash which should be replaced by
854 * the new lock.
855 * @lock_ptr: A pointer to receive the hash lock.
857 * Gets the lock for the hash (record name) of the data in a data_vio, or if one does not exist (or
858 * if we are explicitly rolling over), initialize a new lock for the hash and register it in the
868 struct hash_lock *lock, *new_lock; in acquire_lock() local
872 * Borrow and prepare a lock from the pool so we don't have to do two int_map accesses in acquire_lock()
873 * in the common case of no lock contention. in acquire_lock()
876 "never need to wait for a free hash lock"); in acquire_lock()
884 * Fill in the hash of the new lock so we can map it, since we have to use the hash as the in acquire_lock()
890 new_lock, (replace_lock != NULL), (void **) &lock); in acquire_lock()
897 /* On mismatch put the old lock back and return a severe error */ in acquire_lock()
898 VDO_ASSERT_LOG_ONLY(lock == replace_lock, in acquire_lock()
899 "old lock must have been in the lock map"); in acquire_lock()
902 "old lock must have been marked registered"); in acquire_lock()
906 if (lock == replace_lock) { in acquire_lock()
907 lock = new_lock; in acquire_lock()
908 lock->registered = true; in acquire_lock()
910 /* There's already a lock for the hash, so we don't need the borrowed lock. */ in acquire_lock()
914 *lock_ptr = lock; in acquire_lock()
919 * enter_forked_lock() - Bind the data_vio to a new hash lock.
921 * Implements waiter_callback_fn. Binds the data_vio that was waiting to a new hash lock and waits
922 * on that lock.
934 * fork_hash_lock() - Fork a hash lock because it has run out of increments on the duplicate PBN.
935 * @old_lock: The hash lock to fork.
936 * @new_agent: The data_vio that will be the agent for the new lock.
938 * Transfers the new agent and any lock waiters to a new hash lock instance which takes the place
939 * of the old lock in the lock map. The old lock remains active, but will not update advice.
954 * Only one of the two locks should update UDS. The old lock is out of references, so it in fork_hash_lock()
972 * @lock: The hash lock.
973 * @data_vio: The data_vio to deduplicate using the hash lock.
974 * @has_claim: true if the data_vio already has claimed an increment from the duplicate lock.
976 * If no increments are available, this will roll over to a new hash lock and launch the data_vio
977 * as the writing agent for that lock.
979 static void launch_dedupe(struct hash_lock *lock, struct data_vio *data_vio, in launch_dedupe() argument
982 if (!has_claim && !vdo_claim_pbn_lock_increment(lock->duplicate_lock)) { in launch_dedupe()
983 /* Out of increments, so must roll over to a new lock. */ in launch_dedupe()
984 fork_hash_lock(lock, data_vio); in launch_dedupe()
988 /* Deduplicate against the lock's verified location. */ in launch_dedupe()
989 set_duplicate_location(data_vio, lock->duplicate); in launch_dedupe()
991 update_metadata_for_data_vio_write(data_vio, lock->duplicate_lock); in launch_dedupe()
995 * start_deduping() - Enter the hash lock state where data_vios deduplicate in parallel against a
997 * @lock: The hash lock.
998 * @agent: The data_vio acting as the agent for the lock.
1002 * from the duplicate lock, ensuring the hash lock will still have a data_vio holding it.
1004 static void start_deduping(struct hash_lock *lock, struct data_vio *agent, in start_deduping() argument
1007 lock->state = VDO_HASH_LOCK_DEDUPING; in start_deduping()
1010 * We don't take the downgraded allocation lock from the agent unless we actually need to in start_deduping()
1013 if (lock->duplicate_lock == NULL) { in start_deduping()
1015 "compression must have shared a lock"); in start_deduping()
1021 VDO_ASSERT_LOG_ONLY(vdo_is_pbn_read_lock(lock->duplicate_lock), in start_deduping()
1022 "duplicate_lock must be a PBN read lock"); in start_deduping()
1029 lock->agent = NULL; in start_deduping()
1032 * Launch the agent (if not already deduplicated) and as many lock waiters as we have in start_deduping()
1034 * be triggered and the remaining waiters will be transferred to the new lock. in start_deduping()
1037 launch_dedupe(lock, agent, true); in start_deduping()
1040 while (vdo_waitq_has_waiters(&lock->waiters)) in start_deduping()
1041 launch_dedupe(lock, dequeue_lock_waiter(lock), false); in start_deduping()
1045 * In the degenerate case where all the waiters rolled over to a new lock, this in start_deduping()
1046 * will continue to use the old agent to clean up this lock, and otherwise it just in start_deduping()
1047 * lets the agent exit the lock. in start_deduping()
1049 finish_deduping(lock, agent); in start_deduping()
1067 * finish_verifying() - Handle the result of the agent for the lock comparing its data to the
1076 struct hash_lock *lock = agent->hash_lock; in finish_verifying() local
1080 lock->verified = agent->is_duplicate; in finish_verifying()
1084 * not any re-verifications due to PBN lock releases. in finish_verifying()
1086 if (!lock->verify_counted) { in finish_verifying()
1087 lock->verify_counted = true; in finish_verifying()
1088 if (lock->verified) in finish_verifying()
1098 if (lock->verified && !vdo_claim_pbn_lock_increment(lock->duplicate_lock)) { in finish_verifying()
1100 lock->verified = false; in finish_verifying()
1103 if (lock->verified) { in finish_verifying()
1108 start_deduping(lock, agent, false); in finish_verifying()
1113 * lock without an agent to release the PBN lock. In both cases, the data will have in finish_verifying()
1117 lock->update_advice = true; in finish_verifying()
1118 start_unlocking(lock, agent); in finish_verifying()
1182 * @lock: The hash lock (must be LOCKING).
1185 * Continue the deduplication path for a hash lock by using the agent to read (and possibly
1191 static void start_verifying(struct hash_lock *lock, struct data_vio *agent) in start_verifying() argument
1199 lock->state = VDO_HASH_LOCK_VERIFYING; in start_verifying()
1200 VDO_ASSERT_LOG_ONLY(!lock->verified, "hash lock only verifies advice once"); in start_verifying()
1216 * finish_locking() - Handle the result of the agent for the lock attempting to obtain a PBN read
1217 * lock on the candidate duplicate block.
1218 * @completion: The completion of the data_vio that attempted to get the read lock.
1225 struct hash_lock *lock = agent->hash_lock; in finish_locking() local
1230 VDO_ASSERT_LOG_ONLY(lock->duplicate_lock == NULL, in finish_locking()
1238 lock->update_advice = true; in finish_locking()
1239 start_writing(lock, agent); in finish_locking()
1243 VDO_ASSERT_LOG_ONLY(lock->duplicate_lock != NULL, in finish_locking()
1246 if (!lock->verified) { in finish_locking()
1252 start_verifying(lock, agent); in finish_locking()
1256 if (!vdo_claim_pbn_lock_increment(lock->duplicate_lock)) { in finish_locking()
1259 * available increments left. Must first release the useless PBN read lock before in finish_locking()
1263 lock->verified = false; in finish_locking()
1264 lock->update_advice = true; in finish_locking()
1265 start_unlocking(lock, agent); in finish_locking()
1273 start_deduping(lock, agent, false); in finish_locking()
1276 static bool acquire_provisional_reference(struct data_vio *agent, struct pbn_lock *lock, in acquire_provisional_reference() argument
1281 int result = vdo_acquire_provisional_reference(slab, agent->duplicate.pbn, lock); in acquire_provisional_reference()
1290 agent->duplicate.pbn, lock); in acquire_provisional_reference()
1296 * lock_duplicate_pbn() - Acquire a read lock on the PBN of the block containing candidate
1298 * @completion: The completion of the data_vio attempting to acquire the physical block lock on
1299 * behalf of its hash lock.
1301 * If the PBN is already locked for writing, the lock attempt is abandoned and is_duplicate will be
1308 struct pbn_lock *lock; in lock_duplicate_pbn() local
1335 VIO_READ_LOCK, &lock); in lock_duplicate_pbn()
1341 if (!vdo_is_pbn_read_lock(lock)) { in lock_duplicate_pbn()
1346 * data in the write lock holder. in lock_duplicate_pbn()
1353 * only way we'd be getting lock contention is if we've written the same in lock_duplicate_pbn()
1360 * writing on behalf of our hash lock, but that's impossible since we're the lock in lock_duplicate_pbn()
1363 * 3a) If the lock is held by a data_vio with different data, the advice is already in lock_duplicate_pbn()
1366 * 3b) If the lock is held by a data_vio that matches us, we may as well either in lock_duplicate_pbn()
1368 * potentially having many duplicates wait for the lock holder to write, journal, in lock_duplicate_pbn()
1369 * hash, and finally arrive in the hash lock. We lose a chance to avoid a UDS in lock_duplicate_pbn()
1382 if (lock->holder_count == 0) { in lock_duplicate_pbn()
1383 if (!acquire_provisional_reference(agent, lock, depot)) in lock_duplicate_pbn()
1387 * The increment limit we grabbed earlier is still valid. The lock now holds the in lock_duplicate_pbn()
1389 * locks sharing this read lock. in lock_duplicate_pbn()
1391 lock->increment_limit = increment_limit; in lock_duplicate_pbn()
1395 * We've successfully acquired a read lock on behalf of the hash lock, so mark it as such. in lock_duplicate_pbn()
1397 set_duplicate_lock(agent->hash_lock, lock); in lock_duplicate_pbn()
1407 * start_locking() - Continue deduplication for a hash lock that has obtained valid advice of a
1409 * @lock: The hash lock (currently must be QUERYING).
1412 static void start_locking(struct hash_lock *lock, struct data_vio *agent) in start_locking() argument
1414 VDO_ASSERT_LOG_ONLY(lock->duplicate_lock == NULL, in start_locking()
1415 "must not acquire a duplicate lock when already holding it"); in start_locking()
1417 lock->state = VDO_HASH_LOCK_LOCKING; in start_locking()
1421 * accepting the advice, and don't explicitly change lock states (or use an agent-local in start_locking()
1429 * finish_writing() - Re-entry point for the lock agent after it has finished writing or
1431 * @lock: The hash lock, which must be in state WRITING.
1432 * @agent: The data_vio that wrote its data for the lock.
1434 * The agent will never need to dedupe against anything, so it's done with the lock, but the lock
1437 * If there are other lock holders, the agent will hand the job to one of them and exit, leaving
1438 * the lock to deduplicate against the just-written block. If there are no other lock holders, the
1439 * agent either exits (and later tears down the hash lock), or it remains the agent and updates
1442 static void finish_writing(struct hash_lock *lock, struct data_vio *agent) in finish_writing() argument
1448 lock->duplicate = agent->new_mapped; in finish_writing()
1449 lock->verified = true; in finish_writing()
1451 if (vdo_is_state_compressed(lock->duplicate.state) && lock->registered) { in finish_writing()
1456 lock->update_advice = true; in finish_writing()
1460 if (vdo_waitq_has_waiters(&lock->waiters)) { in finish_writing()
1463 * compress, so the PBN lock on the written copy was already transferred. The agent in finish_writing()
1464 * is done with the lock, but the lock may still need to use it to clean up after in finish_writing()
1467 start_deduping(lock, agent, true); in finish_writing()
1473 * being able to release the hash lock (or just release it). in finish_writing()
1475 if (lock->update_advice) { in finish_writing()
1481 start_updating(lock, agent); in finish_writing()
1482 } else if (lock->duplicate_lock != NULL) { in finish_writing()
1485 * compressed write gave us a shared duplicate lock that we must release. in finish_writing()
1487 set_duplicate_location(agent, lock->duplicate); in finish_writing()
1488 start_unlocking(lock, agent); in finish_writing()
1492 * duplicate lock held, so both the agent and lock have no more work to do. The in finish_writing()
1493 * agent will release its allocation lock in cleanup. in finish_writing()
1495 start_bypassing(lock, agent); in finish_writing()
1500 * select_writing_agent() - Search through the lock waiters for a data_vio that has an allocation.
1501 * @lock: The hash lock to modify.
1506 static struct data_vio *select_writing_agent(struct hash_lock *lock) in select_writing_agent() argument
1517 while (((data_vio = dequeue_lock_waiter(lock)) != NULL) && in select_writing_agent()
1526 * arrived at the lock. in select_writing_agent()
1528 vdo_waitq_transfer_all_waiters(&lock->waiters, &temp_queue); in select_writing_agent()
1532 * first waiter since it was the first to reach the lock. in select_writing_agent()
1534 vdo_waitq_enqueue_waiter(&lock->waiters, &lock->agent->waiter); in select_writing_agent()
1535 lock->agent = data_vio; in select_writing_agent()
1538 data_vio = lock->agent; in select_writing_agent()
1541 /* Swap all the waiters back onto the lock's queue. */ in select_writing_agent()
1542 vdo_waitq_transfer_all_waiters(&temp_queue, &lock->waiters); in select_writing_agent()
1548 * @lock: The hash lock (currently must be QUERYING).
1549 * @agent: The data_vio currently acting as the agent for the lock.
1551 * Begins the non-duplicate write path for a hash lock that had no advice, selecting a data_vio
1555 static void start_writing(struct hash_lock *lock, struct data_vio *agent) in start_writing() argument
1557 lock->state = VDO_HASH_LOCK_WRITING; in start_writing()
1564 agent = select_writing_agent(lock); in start_writing()
1570 * that on some path there would be non-waiters still referencing the lock, in start_writing()
1583 if (vdo_waitq_has_waiters(&lock->waiters)) in start_writing()
1588 * return to the hash lock via vdo_continue_hash_lock() and call finish_writing(). in start_writing()
1658 * finish_querying() - Process the result of a UDS query performed by the agent for the lock.
1666 struct hash_lock *lock = agent->hash_lock; in finish_querying() local
1673 lock->duplicate = agent->duplicate; in finish_querying()
1676 * QUERYING agent to start the hash lock on the unverified dedupe path, verifying in finish_querying()
1679 start_locking(lock, agent); in finish_querying()
1685 lock->update_advice = !data_vio_has_allocation(agent); in finish_querying()
1690 start_writing(lock, agent); in finish_querying()
1695 * start_querying() - Start deduplication for a hash lock.
1696 * @lock: The initialized hash lock.
1697 * @data_vio: The data_vio that has just obtained the new lock.
1699 * Starts deduplication for a hash lock that has finished initializing by making the data_vio that
1701 * query on behalf of the lock.
1703 static void start_querying(struct hash_lock *lock, struct data_vio *data_vio) in start_querying() argument
1705 lock->agent = data_vio; in start_querying()
1706 lock->state = VDO_HASH_LOCK_QUERYING; in start_querying()
1717 * @lock: The hash lock.
1718 * @data_vio: The data_vio attempting to enter the lock.
1720 static void report_bogus_lock_state(struct hash_lock *lock, struct data_vio *data_vio) in report_bogus_lock_state() argument
1722 VDO_ASSERT_LOG_ONLY(false, "hash lock must not be in unimplemented state %s", in report_bogus_lock_state()
1723 get_hash_lock_state_name(lock->state)); in report_bogus_lock_state()
1730 * @data_vio: The data_vio to continue processing in its hash lock.
1732 * Asynchronously continue processing a data_vio in its hash lock after it has finished writing,
1734 * lock, or update the UDS index, or simply release its share of the lock.
1741 struct hash_lock *lock = data_vio->hash_lock; in vdo_continue_hash_lock() local
1743 switch (lock->state) { in vdo_continue_hash_lock()
1745 VDO_ASSERT_LOG_ONLY(data_vio == lock->agent, in vdo_continue_hash_lock()
1746 "only the lock agent may continue the lock"); in vdo_continue_hash_lock()
1747 finish_writing(lock, data_vio); in vdo_continue_hash_lock()
1751 finish_deduping(lock, data_vio); in vdo_continue_hash_lock()
1755 /* This data_vio has finished the write path and the lock doesn't need it. */ in vdo_continue_hash_lock()
1765 /* A lock in this state should never be re-entered. */ in vdo_continue_hash_lock()
1766 report_bogus_lock_state(lock, data_vio); in vdo_continue_hash_lock()
1770 report_bogus_lock_state(lock, data_vio); in vdo_continue_hash_lock()
1776 * @lock: The lock to check.
1777 * @candidate: The data_vio seeking to share the lock.
1779 * Check whether the data in data_vios sharing a lock is different than in a data_vio seeking to
1780 * share the lock, which should only be possible in the extremely unlikely case of a hash
1783 * Return: true if the given data_vio must not share the lock because it doesn't have the same data
1784 * as the lock holders.
1786 static bool is_hash_collision(struct hash_lock *lock, struct data_vio *candidate) in is_hash_collision() argument
1792 if (list_empty(&lock->duplicate_ring)) in is_hash_collision()
1795 lock_holder = list_first_entry(&lock->duplicate_ring, struct data_vio, in is_hash_collision()
1813 "must not already hold a hash lock"); in assert_hash_lock_preconditions()
1818 "must not already be a member of a hash lock ring"); in assert_hash_lock_preconditions()
1823 "must not hold a recovery lock when getting a hash lock"); in assert_hash_lock_preconditions()
1827 * vdo_acquire_hash_lock() - Acquire or share a lock on a record name.
1828 * @data_vio: The data_vio acquiring a lock on its record name.
1830 * Acquire or share a lock on the hash (record name) of the data in a data_vio, updating the
1831 * data_vio to reference the lock. This must only be called in the correct thread for the zone. In
1833 * a lock reference.
1838 struct hash_lock *lock; in vdo_acquire_hash_lock() local
1849 result = acquire_lock(data_vio->hash_zone, &data_vio->record_name, NULL, &lock); in vdo_acquire_hash_lock()
1855 if (is_hash_collision(lock, data_vio)) { in vdo_acquire_hash_lock()
1866 set_hash_lock(data_vio, lock); in vdo_acquire_hash_lock()
1867 switch (lock->state) { in vdo_acquire_hash_lock()
1869 start_querying(lock, data_vio); in vdo_acquire_hash_lock()
1878 /* The lock is busy, and can't be shared yet. */ in vdo_acquire_hash_lock()
1879 wait_on_hash_lock(lock, data_vio); in vdo_acquire_hash_lock()
1883 /* We can't use this lock, so bypass optimization entirely. */ in vdo_acquire_hash_lock()
1889 launch_dedupe(lock, data_vio, false); in vdo_acquire_hash_lock()
1893 /* A lock in this state should not be acquired by new VIOs. */ in vdo_acquire_hash_lock()
1894 report_bogus_lock_state(lock, data_vio); in vdo_acquire_hash_lock()
1899 * vdo_release_hash_lock() - Release a data_vio's share of a hash lock, if held, and null out the
1901 * @data_vio: The data_vio releasing its hash lock.
1903 * If the data_vio is the only one holding the lock, this also releases any resources or locks used
1904 * by the hash lock (such as a PBN read lock on a block containing data with the same hash) and
1905 * returns the lock to the hash zone's lock pool.
1912 struct hash_lock *lock = data_vio->hash_lock; in vdo_release_hash_lock() local
1915 if (lock == NULL) in vdo_release_hash_lock()
1920 if (lock->reference_count > 0) { in vdo_release_hash_lock()
1921 /* The lock is still in use by other data_vios. */ in vdo_release_hash_lock()
1925 lock_key = hash_lock_key(lock); in vdo_release_hash_lock()
1926 if (lock->registered) { in vdo_release_hash_lock()
1930 VDO_ASSERT_LOG_ONLY(lock == removed, in vdo_release_hash_lock()
1931 "hash lock being released must have been mapped"); in vdo_release_hash_lock()
1933 VDO_ASSERT_LOG_ONLY(lock != vdo_int_map_get(zone->hash_lock_map, lock_key), in vdo_release_hash_lock()
1934 "unregistered hash lock must not be in the lock map"); in vdo_release_hash_lock()
1937 VDO_ASSERT_LOG_ONLY(!vdo_waitq_has_waiters(&lock->waiters), in vdo_release_hash_lock()
1938 "hash lock returned to zone must have no waiters"); in vdo_release_hash_lock()
1939 VDO_ASSERT_LOG_ONLY((lock->duplicate_lock == NULL), in vdo_release_hash_lock()
1940 "hash lock returned to zone must not reference a PBN lock"); in vdo_release_hash_lock()
1941 VDO_ASSERT_LOG_ONLY((lock->state == VDO_HASH_LOCK_BYPASSING), in vdo_release_hash_lock()
1942 "returned hash lock must not be in use with state %s", in vdo_release_hash_lock()
1943 get_hash_lock_state_name(lock->state)); in vdo_release_hash_lock()
1944 VDO_ASSERT_LOG_ONLY(list_empty(&lock->pool_node), in vdo_release_hash_lock()
1945 "hash lock returned to zone must not be in a pool ring"); in vdo_release_hash_lock()
1946 VDO_ASSERT_LOG_ONLY(list_empty(&lock->duplicate_ring), in vdo_release_hash_lock()
1947 "hash lock returned to zone must not reference DataVIOs"); in vdo_release_hash_lock()
1949 return_hash_lock_to_pool(zone, lock); in vdo_release_hash_lock()
1953 * transfer_allocation_lock() - Transfer a data_vio's downgraded allocation PBN lock to the
1954 * data_vio's hash lock, converting it to a duplicate PBN lock.
1955 * @data_vio: The data_vio holding the allocation lock to transfer.
1963 "transferred lock must be for the block written"); in transfer_allocation_lock()
1967 VDO_ASSERT_LOG_ONLY(vdo_is_pbn_read_lock(allocation->lock), in transfer_allocation_lock()
1968 "must have downgraded the allocation lock before transfer"); in transfer_allocation_lock()
1974 * Since the lock is being transferred, the holder count doesn't change (and isn't even in transfer_allocation_lock()
1977 hash_lock->duplicate_lock = vdo_forget(allocation->lock); in transfer_allocation_lock()
1981 * vdo_share_compressed_write_lock() - Make a data_vio's hash lock a shared holder of the PBN lock
1984 * @pbn_lock: The PBN lock on the compressed block.
1986 * If the lock is still a write lock (as it will be for the first share), it will be converted to a
1987 * read lock. This also reserves a reference count increment for the data_vio.
1995 "a duplicate PBN lock should not exist when writing"); in vdo_share_compressed_write_lock()
1997 "lock transfer must be for a compressed write"); in vdo_share_compressed_write_lock()
2000 /* First sharer downgrades the lock. */ in vdo_share_compressed_write_lock()
2005 * Get a share of the PBN lock, ensuring it cannot be released until after this data_vio in vdo_share_compressed_write_lock()
2039 __must_hold(&zones->lock) in close_index()
2048 /* Close the index session, while not holding the lock. */ in close_index()
2049 spin_unlock(&zones->lock); in close_index()
2054 spin_lock(&zones->lock); in close_index()
2061 __must_hold(&zones->lock) in open_index()
2075 /* Open the index session, while not holding the lock */ in open_index()
2076 spin_unlock(&zones->lock); in open_index()
2082 spin_lock(&zones->lock); in open_index()
2103 spin_unlock(&zones->lock); in open_index()
2105 spin_lock(&zones->lock); in open_index()
2117 spin_lock(&zones->lock); in change_dedupe_state()
2129 spin_unlock(&zones->lock); in change_dedupe_state()
2155 spin_lock(&zones->lock); in report_dedupe_timeouts()
2164 spin_unlock(&zones->lock); in report_dedupe_timeouts()
2493 spin_lock(&zones->lock); in initiate_suspend_index()
2495 spin_unlock(&zones->lock); in initiate_suspend_index()
2556 __must_hold(&zones->lock) in launch_dedupe_state_change()
2558 /* ASSERTION: We enter with the lock held. */ in launch_dedupe_state_change()
2569 /* ASSERTION: We exit with the lock held. */ in launch_dedupe_state_change()
2588 spin_lock(&zones->lock); in resume_index()
2599 spin_unlock(&zones->lock); in resume_index()
2657 spin_lock(&zones->lock); in get_index_statistics()
2659 spin_unlock(&zones->lock); in get_index_statistics()
2684 * Return: The sum of the hash lock statistics from all hash zones plus the statistics from the UDS
2733 * dump_hash_lock() - Dump a compact description of hash_lock to the log if the lock is not on the
2735 * @lock: The hash lock to dump.
2737 static void dump_hash_lock(const struct hash_lock *lock) in dump_hash_lock() argument
2741 if (!list_empty(&lock->pool_node)) { in dump_hash_lock()
2742 /* This lock is on the free list. */ in dump_hash_lock()
2748 * unambiguous. 'U' indicates a lock not registered in the map. in dump_hash_lock()
2750 state = get_hash_lock_state_name(lock->state); in dump_hash_lock()
2752 lock, state, (lock->registered ? 'D' : 'U'), in dump_hash_lock()
2753 (unsigned long long) lock->duplicate.pbn, in dump_hash_lock()
2754 lock->duplicate.state, lock->reference_count, in dump_hash_lock()
2755 vdo_waitq_num_waiters(&lock->waiters), lock->agent); in dump_hash_lock()
2804 spin_lock(&zones->lock); in vdo_dump_hash_zones()
2807 spin_unlock(&zones->lock); in vdo_dump_hash_zones()
2944 spin_lock(&zones->lock); in set_target_state()
2955 spin_unlock(&zones->lock); in set_target_state()
2965 spin_lock(&zones->lock); in vdo_get_dedupe_index_state_name()
2967 spin_unlock(&zones->lock); in vdo_get_dedupe_index_state_name()