+static void
+release_extra_refcnts(struct wimfs_context *ctx)
+{
+ struct list_head *list = &ctx->orig_stream_list;
+ struct wim_lookup_table *lookup_table = ctx->wim->lookup_table;
+ struct wim_lookup_table_entry *lte, *tmp;
+
+ list_for_each_entry_safe(lte, tmp, list, orig_stream_list)
+ while (lte->out_refcnt--)
+ lte_decrement_refcnt(lte, lookup_table);
+}
+
+/* Moves the currently selected image, which may have been modified, to a new
+ * index, and sets the original index to refer to a reset (unmodified) copy of
+ * the image. */
+static int
+renew_current_image(struct wimfs_context *ctx)
+{
+ WIMStruct *wim = ctx->wim;
+ int ret;
+ int idx = wim->current_image - 1;
+ struct wim_image_metadata *imd = wim->image_metadata[idx];
+ struct wim_image_metadata *replace_imd;
+ struct wim_lookup_table_entry *new_lte;
+
+ if (imd->metadata_lte->resource_location != RESOURCE_IN_WIM) {
+ ERROR("Can't reset modified image that doesn't yet "
+ "exist in the on-disk WIM file!");
+ return WIMLIB_ERR_METADATA_NOT_FOUND;
+ }
+
+ /* Create 'replace_imd' structure to use for the reset original,
+ * unmodified image. */
+ replace_imd = new_image_metadata();
+ if (!replace_imd)
+ return WIMLIB_ERR_NOMEM;
+
+ /* Create new stream reference for the modified image's metadata
+ * resource, which doesn't exist yet. */
+ ret = WIMLIB_ERR_NOMEM;
+ new_lte = new_lookup_table_entry();
+ if (!new_lte)
+ goto err_put_replace_imd;
+ new_lte->flags = WIM_RESHDR_FLAG_METADATA;
+ new_lte->unhashed = 1;
+
+ /* Make the image being moved available at a new index. Increments the
+ * WIM's image count, but does not increment the reference count of the
+ * 'struct image_metadata'. */
+ ret = append_image_metadata(wim, imd);
+ if (ret)
+ goto err_free_new_lte;
+
+ ret = xml_add_image(wim, T(""));
+ if (ret)
+ goto err_undo_append;
+
+ replace_imd->metadata_lte = imd->metadata_lte;
+ imd->metadata_lte = new_lte;
+ wim->image_metadata[idx] = replace_imd;
+ wim->current_image = wim->hdr.image_count;
+ return 0;
+
+err_undo_append:
+ wim->hdr.image_count--;
+err_free_new_lte:
+ free_lookup_table_entry(new_lte);
+err_put_replace_imd:
+ put_image_metadata(replace_imd, NULL);
+ return ret;
+}
+
+static int
+msg_unmount_request_handler(const void *_msg, void *_handler_ctx)