From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (mail.openvz.org [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id BED4D803BB for ; Wed, 18 Nov 2026 13:12:21 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AIDB7Uv025501; Wed, 18 Nov 2026 16:11:07 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AIDB7Uv025501 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="ZMLua0NY" Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AIDAc2D025424 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Wed, 18 Nov 2026 16:10:38 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AIDAc2D025424 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-4a3052225cbso941761f8f.1 for ; Wed, 18 Nov 2026 05:10:38 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErENIZO9ASBKRO0B+aRNhfGZjHS+Ndx7mtyDT35fJ/IHGyhQy4nhp9TgL1INYySS6D+T66io5g==@openvz.org X-Gm-Message-State: APuU4Z8Ia15l9utWZjvwIhJtPqMsApRoLzuWOPMUrCUATGvje52eEPGZ awP2yR5ojrRBc4oDG3FrHBA2VD9f0gAelICe4Oc/IwCFCSGYxppCTbzTzMOrQeiMBYdzvw4DTYU bvZ1PXnZh49+iMeXihRgOyrBKeAch8H1RsjSCMGvdvs15f9GpJXPNOg== X-Gm-Gg: AYBFou3PY1vf6lnRI6BNejWcl9JcnkM84n6s/0SBNTw7YEZZ6eDaq50PWLLjq+qaWyl NREKubxhp73TbhGSImpuF5SAFOfBBJYoNCOPst474ZyUBCdPqJnEQQNlIE6Rt5hygJDYNsBVbtE GG8sVfGq8Cnzwui/WxXfXhtckOf9OtjGqs4wOZylOaUps7zbEcN+QtfMKcRQNgkkw79LxNBHcph 61EPM0y75Bw8ag1rYn8DJ+7rjJJ6tSw1kguxpmb9oTVA1Svo3nbzfd/0RwtZZJJSHpAg73vgHz/ 4hi0EnM1gAD85c4f0SJsrBDuTPaVoCkydpBRCrKt7Au4Hu+7551d0gWw2vaN8XvApM6D+31EafN 3T3kePDCYN/KWzouH09/dk0QuvOQA X-Received: by 2002:a05:600c:1d82:b0:4bb:6752:8413 with SMTP id 5b1f17b1804b1-4bb7da03b51mr46096945e9.8.1795007438272; Wed, 18 Nov 2026 05:10:38 -0800 (PST) X-Received: by 2002:a05:600c:1d82:b0:4bb:6752:8413 with SMTP id 5b1f17b1804b1-4bb7da03b51mr46096275e9.8.1795007437605; Wed, 18 Nov 2026 05:10:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795007437; cv=none; d=google.com; s=arc-20260327; b=Nr2SdET7+OkNExPIX0ZROw4hSan9Xn1i72LhAX+w2asO+WiA7DeGBdrZrE9Kxe5cet QA4hv+/Gr4oAG3beaeeLHX0QdFiQ2AGiPkrcQaCwB0QQA6R2PjqAa+4dSPHSSV5ruwz8 bdTfNMLBd9s+5fWFbZZYg2I3SuqFyl/SgUwgcqDMhzLbyp7m3ZpOfNcDvyW+77hbMxrk dYRjX+HXefoodAx/x+imNlAwmc6Uvp1yIQW2ade2Sm0nO/2D3Q+19A7HkDXKbyypgfpk /ZfsS9x7qW0IDeEacN4CMSbbCSa3sfRL0w9kCEbFyE1UoHivXB2T+eKzmIHyo3oy2htL 2QqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=rjeixI6t+Ie3yo1FEvAZYWF0unDM74xEA+zOUI2qjIw=; fh=CMILZ+goaghq6B4UpCDhf0EQp6kYD0LAlmOkUiVCOvs=; b=ABhRvwjqEXKZrGalFg7sV1UxZpChlVMHqAME6KIwh77y3fZwquOm7/Pzc9zjiKFUPE vh2I5CZ2oI/OUURkmYH7dCkagLLhJcPMU3v0+AMlXoomCJ9dT2ReT3N5muW0Puun6s3K qrqekjaEoqmInkWRAX0x2E7wz8lHo+hJxdor6yuhibkCz6usupspd4X7209MZP5a22e+ 7XIEY/YhVVQbWr9jKCDUn30u1LdjzFnkIxXShvWGRWU1/7bJmpMnjxHuNDjRkPh7SWEh csKg1UA6URaPaQmVFZkL9cKTRnzx2pBio8fjR1hhBVZN7bJPwrEqcG8Ez6yWsbhpcTXR YazA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=ZMLua0NY; spf=pass (google.com: domain of alexander.ivanov@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=alexander.ivanov@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: from relay.virtuozzo.com (relay.virtuozzo.com. [130.117.225.111]) by mx.google.com with ESMTPS id ffacd0b85a97d-4a682863982si4845701f8f.303.2026.11.18.05.10.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Nov 2026 05:10:37 -0800 (PST) Received-SPF: pass (google.com: domain of alexander.ivanov@virtuozzo.com designates 130.117.225.111 as permitted sender) client-ip=130.117.225.111; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=ZMLua0NY; spf=pass (google.com: domain of alexander.ivanov@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=alexander.ivanov@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-ID:Date:Subject:From: Content-Type; bh=rjeixI6t+Ie3yo1FEvAZYWF0unDM74xEA+zOUI2qjIw=; b=ZMLua0NYuZDg frRvMyZvYovVQywUor4u7idL8l+03HJz+V4ct1+MJYW1Gvu/uTurtTjndBUQ9trYoNDL/09RZpeeD rnW81bTd/SxJXXvgED6DR0Qin/+h4+h2Lln5/nPi+gI1/UiTy+qpiNvhWWr8qzHZahZylMgb043AG k5n0oholvV/L6aZGQZ8Ju/Rmk+2OG9ONacxtNQYmGxI8Lvk9yht8ALnJvT+8f7VR2DXMOoT4gWa3h iX6H7Uk5zmY0jNJVYa9smiYQIZkfmziTLS7tLtLefOrAN/Z24CS8p2x0hvkN6I/DoyzmChEl52Btb sY+u8lOECFUPyOfGX76zjQ==; Received: from ch-vpn.virtuozzo.com ([130.117.225.6] helo=sky) by relay.virtuozzo.com with esmtps (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1xTfMZ-004oHD-0n; Wed, 18 Nov 2026 14:10:36 +0100 From: Alexander Ivanov To: andrey.zhadchenko@virtuozzo.com Date: Wed, 18 Nov 2026 14:10:31 +0100 Message-ID: <20261118131032.3372996-9-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261118131032.3372996-1-alexander.ivanov@virtuozzo.com> References: <20261118131032.3372996-1-alexander.ivanov@virtuozzo.com> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.2 v8 8/9] dm-ploop: reload metadata on resume X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org On "dmsetup reload" the new ploop reads image metadata in ploop_ctr(), but IO keeps going through the old ploop until suspend, and userspace may modify images of a suspended device (merge zeroes BAT entries of the base delta, snapshot creation trims the top delta). So after resume the new ploop may work with a stale BAT and file sizes, e.g. allocate clusters already in use. Reload all metadata in ploop_preresume(): the top delta header, BAT of every delta and delta file sizes, and rebuild the BAT tree and holes_bitmap. On error the old metadata is kept and resume fails. DM has already swapped the new table in by then, so the device stays suspended and the next resume retries. Userspace must check the result and must not resume the previous instance on the same images. Skip the in-use signature check on reload, it is done in ploop_ctr(). Take the signature from disk: libploop marks the image closed on the suspended device before umount, and an in-memory IN_USE written over it makes umount trim the image and lose CBT. After flip_upper_deltas keep the in-memory signature, as userspace relies on it being written to the new top delta, otherwise the next load fails with -ENOLCK after online merge. Data bios complete before BAT writeback, so wait for inflight_md in postsuspend. A resize of the suspended device may leave BAT writeback in flight as well, so wait for it in preresume too. Keep metadata that is still dirty on resume, as md pages may still be referenced. Request preallocation in ploop_preresume() after the reload instead of in ploop_ctr(), so the new ploop doesn't truncate the top delta back to a stale size before resume. ploop_postsuspend() waits for pending preallocation since a5836033b8fe ("dm-ploop: wait for the ploop_allocator() thread before changing deltas[]"). Skip this wait and the one for BAT writeback in standby, as both may hang on a dead file, so that dm_replace() of libploop can suspend the device to recover it. Otherwise keep the waits uninterruptible: an ftruncate() of the old allocator finished after the next table has grown the top delta would cut its data, and a late BAT writeback would overwrite the reloaded BAT. Drop the pending requests on postsuspend, so that after a skipped wait the old allocator doesn't resize the top delta from its stale size under a ploop of a reloaded table. Only the operation already in flight is left unfenced. Skip the reload and prealloc on resume while preallocation is pending, as the allocator changes the top delta size under bat_lock only. Skip the reload in standby too, as the images may be inaccessible. Track standby per ploop instead of the queue bit. The queue is shared by all tables, and ploop_ctr() of a new table clears the bit before the old table is suspended, so the old ploop would wait for its stuck allocator in postsuspend. The flag is never cleared: once the files of a ploop failed, it is recovered only by a new table via dm_replace(). Drop the comment claiming that ploop_preallocate_cluster() is the only place to change file_size. ploop_llseek_hole() needs no locking against the reload: dm_llseek_hole() calls it under md->io_barrier and returns -EAGAIN on a suspended device. https://virtuozzo.atlassian.net/browse/VSTOR-148171 Feature: dm-ploop: ploop target driver Signed-off-by: Alexander Ivanov --- drivers/md/dm-ploop-bat.c | 144 ++++++++++++++++++++++++++++++++++- drivers/md/dm-ploop-cmd.c | 1 + drivers/md/dm-ploop-map.c | 6 +- drivers/md/dm-ploop-target.c | 97 ++++++++++++++++------- drivers/md/dm-ploop.h | 13 +++- 5 files changed, 228 insertions(+), 33 deletions(-) diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c index 5a89771a2e02..a6894910a5bf 100644 --- a/drivers/md/dm-ploop-bat.c +++ b/drivers/md/dm-ploop-bat.c @@ -185,7 +185,8 @@ static int ploop_setup_holes_bitmap(struct ploop *ploop, u32 bat_clusters) } ALLOW_ERROR_INJECTION(ploop_setup_holes_bitmap, ERRNO); -int ploop_setup_metadata(struct ploop *ploop, struct page *page) +int ploop_setup_metadata(struct ploop *ploop, struct page *page, + bool check_in_use) { struct ploop_pvd_header *m_hdr = NULL; u32 bat_clusters, offset_clusters; @@ -200,7 +201,8 @@ int ploop_setup_metadata(struct ploop *ploop, struct page *page) goto out; ret = -ENOLCK; - if (m_hdr->m_DiskInUse != cpu_to_le32(SIGNATURE_DISK_IN_USE) && + if (check_in_use && + m_hdr->m_DiskInUse != cpu_to_le32(SIGNATURE_DISK_IN_USE) && !ploop_is_ro(ploop) && !ignore_signature_disk_in_use) goto out; @@ -567,6 +569,144 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r } ALLOW_ERROR_INJECTION(ploop_add_delta, ERRNO); +static bool ploop_md_pages_are_clean(struct ploop *ploop) +{ + struct rb_node *node; + struct md_page *md; + + if (!llist_empty(&ploop->wb_batch_llist) || + atomic_read(&ploop->inflight_md)) + return false; + + ploop_for_each_md_page(ploop, md, node) { + if (test_bit(MD_DIRTY, &md->status) || + test_bit(MD_WRITEBACK, &md->status) || + test_bit(MD_UPDATING, &md->status) || + md->piwb || !llist_empty(&md->wait_llist)) + return false; + } + + return true; +} + +/* + * Build in-memory metadata from scratch: re-read header of top delta, + * BAT of every delta and sizes of delta files. This is called on resume + * of suspended device, since after "dmsetup reload" the metadata of the + * new ploop may be outdated: the images could be changed by the old + * ploop after the new one was constructed. On error the old metadata + * is kept. + */ +int ploop_reload_metadata(struct ploop *ploop) +{ + struct rb_root old_bat_entries = ploop->bat_entries; + void *old_holes_bitmap = ploop->holes_bitmap; + u32 old_nr_bat_entries = ploop->nr_bat_entries; + u32 old_hb_nr = ploop->hb_nr; + u32 old_m_Type = ploop->m_Type; + u32 old_m_Sectors = ploop->m_Sectors; + struct ploop_pvd_header *hdr, *old_hdr; + u8 old_m_Sig[sizeof(ploop->m_Sig)]; + struct ploop_delta *delta, *old_deltas; + struct page *page; + loff_t file_size; + u32 nr_be; + int level, ret; + + /* + * Metadata of the device that has already been running is up to + * date, so we only need to reload clean metadata. Keep dirty + * metadata, as md pages are still in use. + */ + if (!ploop_md_pages_are_clean(ploop)) + return 0; + + memcpy(old_m_Sig, ploop->m_Sig, sizeof(old_m_Sig)); + + old_deltas = kmemdup(ploop->deltas, + sizeof(*old_deltas) * ploop->nr_deltas, + GFP_KERNEL); + page = alloc_page(GFP_KERNEL); + ret = -ENOMEM; + if (!old_deltas || !page) + goto out; + + ret = ploop_rw_page_sync(READ, ploop_top_delta(ploop)->file, 0, page); + if (ret) + goto out; + + ploop->bat_entries = RB_ROOT; + ploop->holes_bitmap = NULL; + + /* Image is already checked for being in use in ploop_ctr() */ + ret = ploop_setup_metadata(ploop, page, false); + if (ret) + goto restore; + + ret = ploop_prealloc_md_pages(&ploop->bat_entries, 0, + ploop->nr_bat_entries); + if (ret) + goto restore; + + /* + * Same order as in ploop_add_deltas_stack(): from top to base. + * ploop_read_delta() relies on it, see the comment there. nr_be of + * the delta is updated right away, as the lower delta is checked + * against it on the next iteration. + */ + for (level = ploop_top_level(ploop); level >= 0; level--) { + delta = &ploop->deltas[level]; + + ret = ploop_read_delta(ploop, level, delta->file, + &file_size, &nr_be); + if (ret) + goto restore; + + delta->file_size = file_size; + delta->file_preallocated_area_start = file_size; + delta->nr_be = nr_be; + } + + /* + * Header of top delta is written with BAT page 0. Userspace doesn't + * mark the delta, which becomes top after flip_upper_deltas, as in + * use, and relies that the in-use signature of the old top delta + * will be written to it. So keep the signature we had. In other + * cases take it from disk: userspace may have changed it on the + * suspended device (e.g. marked the image closed on umount). + */ + if (ploop->top_flipped) { + old_hdr = ploop_md_first_entry(&old_bat_entries)->kmpage; + hdr = ploop_md_first_entry(&ploop->bat_entries)->kmpage; + hdr->m_DiskInUse = old_hdr->m_DiskInUse; + ploop->top_flipped = false; + } + + ploop_free_md_pages_tree(&old_bat_entries); + kvfree(old_holes_bitmap); + goto out; +restore: + ploop_free_md_pages_tree(&ploop->bat_entries); + kvfree(ploop->holes_bitmap); + ploop->bat_entries = old_bat_entries; + ploop->holes_bitmap = old_holes_bitmap; + ploop->nr_bat_entries = old_nr_bat_entries; + ploop->hb_nr = old_hb_nr; + memcpy(ploop->m_Sig, old_m_Sig, sizeof(ploop->m_Sig)); + ploop->m_Type = old_m_Type; + ploop->m_Sectors = old_m_Sectors; + memcpy(ploop->deltas, old_deltas, + sizeof(*old_deltas) * ploop->nr_deltas); +out: + if (page) + put_page(page); + kfree(old_deltas); + if (ret) + PL_ERR("Failed to reload metadata: %d", ret); + return ret; +} +ALLOW_ERROR_INJECTION(ploop_reload_metadata, ERRNO); + void ploop_put_delta(struct ploop *ploop, struct ploop_delta *delta) { int i; diff --git a/drivers/md/dm-ploop-cmd.c b/drivers/md/dm-ploop-cmd.c index 2d1fa449b052..e1b6aea4af94 100644 --- a/drivers/md/dm-ploop-cmd.c +++ b/drivers/md/dm-ploop-cmd.c @@ -1087,6 +1087,7 @@ static int process_flip_upper_deltas(struct ploop *ploop) /* FIXME */ swap(ploop->deltas[level], ploop->deltas[level+1]); + ploop->top_flipped = true; return 0; } diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c index 2f85383c4682..9db80a046af1 100644 --- a/drivers/md/dm-ploop-map.c +++ b/drivers/md/dm-ploop-map.c @@ -1195,6 +1195,8 @@ static int ploop_preallocate_cluster(struct ploop *ploop, struct file *file) spin_lock_irqsave(&ploop->bat_lock, flags); prealloc_more: + if (READ_ONCE(ploop->prealloc_stopped)) + ploop->prealloc_size = 0; ploop->prealloc_in_progress = ploop->prealloc_size; ploop->prealloc_size = 0; if (!ploop->prealloc_in_progress) { @@ -1212,9 +1214,6 @@ static int ploop_preallocate_cluster(struct ploop *ploop, struct file *file) goto out; } - /* - * Here must be the only place to change file_size. - */ spin_lock_irqsave(&ploop->bat_lock, flags); if (top->file_size < new_len) { top->file_size = new_len; @@ -1442,6 +1441,7 @@ static void ploop_check_standby_mode(struct ploop *ploop, long res) spin_lock_irqsave(&q->queue_lock, flags); prev = test_and_set_bit(QUEUE_FLAG_STANDBY, &q->queue_flags); spin_unlock_irqrestore(&q->queue_lock, flags); + WRITE_ONCE(ploop->standby, true); if (!prev) PL_INFO("was switched into the standby mode"); diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c index bfadca7f12ae..2be2aaeaa2d1 100644 --- a/drivers/md/dm-ploop-target.c +++ b/drivers/md/dm-ploop-target.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "dm-ploop.h" #define DM_MSG_PREFIX "ploop" @@ -297,7 +298,7 @@ static int ploop_check_top_delta(struct ploop *ploop, struct file *file) /* flush dirty pages before changing direct IO */ vfs_fsync(file, 1); - ret = ploop_setup_metadata(ploop, page); + ret = ploop_setup_metadata(ploop, page, true); if (ret) goto out; @@ -428,8 +429,7 @@ static struct ploop_worker *ploop_worker_create(struct ploop *ploop, * suspended and resumed. This behavior is intentional to allow for atomic * updates to the mapping table without disrupting ongoing I/O operations. * - * But dm-ploop is not fully compatible with this design. Ploop does not do a - * "metadata" reload on resume. So there can be situations like: + * So there can be situations like: * * 1. dmsetup reload is called, and new ploop is created with ploop_ctr for * the same delta image file as an old ploop and ploop_add_deltas_stack reads @@ -440,23 +440,19 @@ static struct ploop_worker *ploop_worker_create(struct ploop *ploop, * the new one, new ploop has outdated metadata and thus may corrupt the * images by relying on it. * - * For instance we thought to truncate the preallocations to the delta image - * file on ploop_dtr, but that can't be done as ploop_dtr is called on the old - * ploop after the new one is resumed, so we can't possibly modify the image - * from the old ploop context at this point, as new ploop may be already doing - * its own IO or preallocation. + * To handle this, ploop_preresume reloads all the metadata from the images + * (see ploop_reload_metadata), so that the new ploop "sees" all previous + * updates from the old one. For the same reason the new ploop must not + * modify the images before resume, so preallocation is started only after + * the metadata reload, and the old ploop waits for its preallocation to + * complete on postsuspend. * - * The better way to do this truncation is on ploop_*suspend, and then on resume - * to reload the metadata in the new ploop so that the new ploop "sees" all - * previous updates from the old one. - * - * For now we can leave with it, as we chose not to do this truncation on - * destroy and we rely that userspace always does dmsetup reload on already - * suspended device. - * - * FIXME: We must implement metadata reload on resume. After that we can - * implement preallocation truncation on suspend (see dm-qcow2 as it already - * implements that in qcow2_parse_metadata and qcow2_truncate_unused_tail). + * Still, we can't truncate the preallocations to the delta image file on + * ploop_dtr, as ploop_dtr is called on the old ploop after the new one is + * resumed, so we can't possibly modify the image from the old ploop context + * at this point, as new ploop may be already doing its own IO or + * preallocation. This truncation should be done on ploop_*suspend (see + * dm-qcow2 as it already implements that in qcow2_truncate_unused_tail). */ static int ploop_ctr(struct dm_target *ti, unsigned int argc, char **argv) { @@ -495,6 +491,8 @@ static int ploop_ctr(struct dm_target *ti, unsigned int argc, char **argv) spin_lock_init(&ploop->deferred_lock); init_llist_head(&ploop->llsuspended_pios); + /* A new table stays suspended until the first successful resume */ + ploop->suspended = true; for (i = 0; i < PLOOP_LIST_COUNT; i++) init_llist_head(&ploop->pios[i]); @@ -646,9 +644,6 @@ static int ploop_ctr(struct dm_target *ti, unsigned int argc, char **argv) ti->num_discard_bios = 1; ti->discards_supported = true; - if (!ploop_is_ro(ploop) && ploop->nr_deltas > 0) - ploop_should_prealloc(ploop); - return 0; err: @@ -729,6 +724,11 @@ static void ploop_set_suspended(struct dm_target *ti, bool suspended) up_write(&ploop->ctl_rwsem); } +static bool ploop_is_standby(struct ploop *ploop) +{ + return READ_ONCE(ploop->standby); +} + static void ploop_presuspend(struct dm_target *ti) { struct ploop *ploop = ti->private; @@ -755,29 +755,72 @@ static void ploop_postsuspend(struct dm_target *ti) { struct ploop *ploop = ti->private; - wait_event(ploop->dispatcher_wq_prealloc, - ploop_no_pending_prealloc(ploop)); + /* + * Let the next resumed ploop (maybe of reloaded table) see + * final size of the top delta and clean md pages: data bios + * are completed before BAT writeback is finished (see + * ploop_put_piwb()). Don't wait in standby mode: the allocator + * or BAT writeback may be stuck on the dead file, while + * dm_replace() of libploop must be able to suspend the device + * to recover it. Pending prealloc requests are dropped, so if + * the wait is skipped, only the operation already in flight may + * change the top delta under a ploop of a reloaded table. + */ + WRITE_ONCE(ploop->prealloc_stopped, true); + if (!ploop_is_standby(ploop)) { + wait_event(ploop->dispatcher_wq_prealloc, + ploop_no_pending_prealloc(ploop)); + wait_event(ploop->inflight_md_wq, + !atomic_read(&ploop->inflight_md)); + } ploop_set_suspended(ti, true); } static int ploop_preresume(struct dm_target *ti) { struct ploop *ploop = ti->private; + unsigned int noio_flags; + bool reload = false; int ret = 0; + /* A resize of the suspended device may leave BAT writeback in flight */ + if (!ploop_is_standby(ploop)) + wait_event(ploop->inflight_md_wq, + !atomic_read(&ploop->inflight_md)); + down_write(&ploop->ctl_rwsem); - if (ploop->noresume) + if (ploop->noresume) { ret = -EAGAIN; + } else if (!ploop_is_standby(ploop) && + ploop_no_pending_prealloc(ploop)) { + reload = true; + /* + * The device is suspended, so reclaim must not wait + * for IO to it. Otherwise we deadlock with ourselves. + */ + noio_flags = memalloc_noio_save(); + ret = ploop_reload_metadata(ploop); + memalloc_noio_restore(noio_flags); + } /* * We are singleton target. There will be no more reasons to break * resume. Clear it under the same lock as the noresume check, so * that flip_upper_deltas can't sneak in before the device goes live. */ - if (ret == 0) + if (ret == 0) { ploop->suspended = false; + WRITE_ONCE(ploop->prealloc_stopped, false); + } up_write(&ploop->ctl_rwsem); - if (ret == 0) + if (ret == 0) { + /* + * Preallocate only after the metadata is reloaded, + * as prealloc relies on the actual top delta size. + */ + if (reload && !ploop_is_ro(ploop)) + ploop_should_prealloc(ploop); ploop_set_wants_suspend(ti, false); + } return ret; } ALLOW_ERROR_INJECTION(ploop_preresume, ERRNO); diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h index 5ae02ef5ffc2..389a234ec80a 100644 --- a/drivers/md/dm-ploop.h +++ b/drivers/md/dm-ploop.h @@ -246,6 +246,15 @@ struct ploop { /* Resume is prohibited */ bool noresume; + /* Top delta was swapped by flip_upper_deltas */ + bool top_flipped; + /* + * This ploop switched the queue into standby. Unlike the queue + * bit, it is not cleared by ploop_ctr() of a new table. + */ + bool standby; + /* Suspended, the allocator must not start new requests */ + bool prealloc_stopped; /* Device is suspended */ bool suspended; /* Device wants suspend */ @@ -634,7 +643,9 @@ extern struct pio *ploop_alloc_pio_with_pages(struct ploop *ploop); extern void ploop_free_pio_with_pages(struct ploop *ploop, struct pio *pio); extern void ploop_pio_prepare_offsets(struct ploop *ploop, struct pio *pio, u32 clu); -extern int ploop_setup_metadata(struct ploop *ploop, struct page *page); +extern int ploop_setup_metadata(struct ploop *ploop, struct page *page, + bool check_in_use); +extern int ploop_reload_metadata(struct ploop *ploop); extern int ploop_read_delta_metadata(struct ploop *ploop, struct file *file, struct rb_root *md_root, u32 *delta_nr_be); extern void ploop_index_wb_init(struct ploop_index_wb *piwb, -- 2.53.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel