Skip to contentSkip to conversation

SSDFS: flash-friendly LFS file system for ZNS SSD

RFC PATCH In review Viacheslav Dubeyko 5 participants 84 messages Feb 25, 2023 – Feb 28, 2023 Page 4 of 11

Subsystems (2)
lore
Share & tools
lore
#27 lore raw

RFC PATCH 26/76 ssdfs: offset translation table initialization logic

In reply to Viacheslav Dubeyko

Offset translation table is a metadata structure that is stored as metadata in every log. The responsibility of offset translation table keeps the knowledge which particular logical blocks are stored into log's payload and which offset in the payload should be used to access and retrieve the content of logical block. Offset translation table can be imagined like a sequence of fragments. Every fragment contains array of physical offset descriptors that provides the way to convert logical block ID into the physical offset in the log. Initialization logic requires to read all fragments from the volume, decompress it, and initilaize offset translation table by fragments.

CC Viacheslav Dubeyko <[email protected]>
CC Luka Perkov <[email protected]>
CC Bruno Banelli <[email protected]>
---
 fs/ssdfs/peb_read_thread.c | 2288 ++++++++++++++++++++++++++++++++++++
 1 file changed, 2288 insertions(+)
fs/ssdfs/peb_read_thread.c +2288 -0
index f6a5b67612af..317eef078521 100644
@@ -245,6 +245,2294 @@ int ssdfs_read_blk2off_table_fragment(struct ssdfs_peb_info *pebi,
  *                          READ THREAD FUNCTIONALITY                         *
  ******************************************************************************/
 
+/*
+ * __ssdfs_peb_read_log_footer() - read log's footer
+ * @fsi: file system info object
+ * @pebi: PEB object
+ * @page_off: log's starting page
+ * @desc: footer's descriptor
+ * @log_bytes: pointer on value of bytes in the log [out]
+ *
+ * This function tries to read log's footer.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ * %-ENODATA    - valid footer is not found.
+ */
+static
+int __ssdfs_peb_read_log_footer(struct ssdfs_fs_info *fsi,
+				struct ssdfs_peb_info *pebi,
+				u16 page_off,
+				struct ssdfs_metadata_descriptor *desc,
+				u32 *log_bytes)
+{
+	struct ssdfs_signature *magic = NULL;
+	struct ssdfs_partial_log_header *plh_hdr = NULL;
+	struct ssdfs_log_footer *footer = NULL;
+	u16 footer_off;
+	u32 bytes_off;
+	struct page *page;
+	void *kaddr;
+	size_t read_bytes;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!fsi || !pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!desc || !log_bytes);
+
+	SSDFS_DBG("seg %llu, peb_id %llu, page_off %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*log_bytes = U32_MAX;
+
+	bytes_off = le32_to_cpu(desc->offset);
+	footer_off = bytes_off / fsi->pagesize;
+
+	page = ssdfs_page_array_grab_page(&pebi->cache, footer_off);
+	if (unlikely(IS_ERR_OR_NULL(page))) {
+		SSDFS_ERR("fail to grab page: index %u\n",
+			  footer_off);
+		return -ENOMEM;
+	}
+
+	kaddr = kmap_local_page(page);
+
+	if (PageUptodate(page) || PageDirty(page))
+		goto check_footer_magic;
+
+	err = ssdfs_aligned_read_buffer(fsi, pebi->peb_id,
+					bytes_off,
+					(u8 *)kaddr,
+					PAGE_SIZE,
+					&read_bytes);
+	if (unlikely(err))
+		goto fail_read_footer;
+	else if (unlikely(read_bytes != PAGE_SIZE)) {
+		err = -ERANGE;
+		goto fail_read_footer;
+	}
+
+	SetPageUptodate(page);
+
+check_footer_magic:
+	magic = (struct ssdfs_signature *)kaddr;
+
+	if (!is_ssdfs_magic_valid(magic)) {
+		err = -ENODATA;
+		goto fail_read_footer;
+	}
+
+	if (is_ssdfs_partial_log_header_magic_valid(magic)) {
+		plh_hdr = SSDFS_PLH(kaddr);
+		*log_bytes = le32_to_cpu(plh_hdr->log_bytes);
+	} else if (__is_ssdfs_log_footer_magic_valid(magic)) {
+		footer = SSDFS_LF(kaddr);
+		*log_bytes = le32_to_cpu(footer->log_bytes);
+	} else {
+		err = -ENODATA;
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("log footer is corrupted: "
+			  "peb %llu, page_off %u\n",
+			  pebi->peb_id, page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+		goto fail_read_footer;
+	}
+
+fail_read_footer:
+	kunmap_local(kaddr);
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (err == -ENODATA) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("valid footer is not detected: "
+			  "seg_id %llu, peb_id %llu, "
+			  "page_off %u\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  footer_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to read footer: "
+			  "seg %llu, peb %llu, "
+			  "pages_off %u, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  footer_off,
+			  err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * __ssdfs_peb_read_log_header() - read log's header
+ * @fsi: file system info object
+ * @pebi: PEB object
+ * @page_off: log's starting page
+ * @log_bytes: pointer on value of bytes in the log [out]
+ *
+ * This function tries to read the log's header.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ * %-ENODATA    - valid footer is not found.
+ */
+static
+int __ssdfs_peb_read_log_header(struct ssdfs_fs_info *fsi,
+				struct ssdfs_peb_info *pebi,
+				u16 page_off,
+				u32 *log_bytes)
+{
+	struct ssdfs_signature *magic = NULL;
+	struct ssdfs_segment_header *seg_hdr = NULL;
+	struct ssdfs_partial_log_header *pl_hdr = NULL;
+	struct ssdfs_metadata_descriptor *desc = NULL;
+	struct page *page;
+	void *kaddr;
+	size_t read_bytes;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!fsi || !pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!log_bytes);
+
+	SSDFS_DBG("seg %llu, peb_id %llu, page_off %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*log_bytes = U32_MAX;
+
+	page = ssdfs_page_array_grab_page(&pebi->cache, page_off);
+	if (unlikely(IS_ERR_OR_NULL(page))) {
+		SSDFS_ERR("fail to grab page: index %u\n",
+			  page_off);
+		return -ENOMEM;
+	}
+
+	kaddr = kmap_local_page(page);
+
+	if (PageUptodate(page) || PageDirty(page))
+		goto check_header_magic;
+
+	err = ssdfs_aligned_read_buffer(fsi, pebi->peb_id,
+					page_off * PAGE_SIZE,
+					(u8 *)kaddr,
+					PAGE_SIZE,
+					&read_bytes);
+	if (unlikely(err))
+		goto fail_read_log_header;
+	else if (unlikely(read_bytes != PAGE_SIZE)) {
+		err = -ERANGE;
+		goto fail_read_log_header;
+	}
+
+	SetPageUptodate(page);
+
+check_header_magic:
+	magic = (struct ssdfs_signature *)kaddr;
+
+	if (!is_ssdfs_magic_valid(magic)) {
+		err = -ENODATA;
+		goto fail_read_log_header;
+	}
+
+	if (__is_ssdfs_segment_header_magic_valid(magic)) {
+		seg_hdr = SSDFS_SEG_HDR(kaddr);
+
+		err = ssdfs_check_segment_header(fsi, seg_hdr,
+						 false);
+		if (unlikely(err)) {
+			err = -ENODATA;
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("log header is corrupted: "
+				  "seg %llu, peb %llu, page_off %u\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+			goto fail_read_log_header;
+		}
+
+		desc = &seg_hdr->desc_array[SSDFS_LOG_FOOTER_INDEX];
+
+		err = __ssdfs_peb_read_log_footer(fsi, pebi, page_off,
+						   desc, log_bytes);
+		if (err == -ENODATA) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("fail to read footer: "
+				  "seg %llu, peb %llu, page_off %u, "
+				  "err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  page_off,
+				  err);
+#endif /* CONFIG_SSDFS_DEBUG */
+			goto fail_read_log_header;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to read footer: "
+				  "seg %llu, peb %llu, page_off %u, "
+				  "err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  page_off,
+				  err);
+			goto fail_read_log_header;
+		}
+	} else if (is_ssdfs_partial_log_header_magic_valid(magic)) {
+		pl_hdr = SSDFS_PLH(kaddr);
+
+		err = ssdfs_check_partial_log_header(fsi, pl_hdr,
+						     false);
+		if (unlikely(err)) {
+			err = -ENODATA;
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("partial log header is corrupted: "
+				  "seg %llu, peb %llu, page_off %u\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+			goto fail_read_log_header;
+		}
+
+		desc = &pl_hdr->desc_array[SSDFS_LOG_FOOTER_INDEX];
+
+		if (ssdfs_pl_has_footer(pl_hdr)) {
+			err = __ssdfs_peb_read_log_footer(fsi, pebi, page_off,
+							  desc, log_bytes);
+			if (err == -ENODATA) {
+#ifdef CONFIG_SSDFS_DEBUG
+				SSDFS_DBG("fail to read footer: "
+					  "seg %llu, peb %llu, page_off %u, "
+					  "err %d\n",
+					  pebi->pebc->parent_si->seg_id,
+					  pebi->peb_id,
+					  page_off,
+					  err);
+#endif /* CONFIG_SSDFS_DEBUG */
+				goto fail_read_log_header;
+			} else if (unlikely(err)) {
+				SSDFS_ERR("fail to read footer: "
+					  "seg %llu, peb %llu, page_off %u, "
+					  "err %d\n",
+					  pebi->pebc->parent_si->seg_id,
+					  pebi->peb_id,
+					  page_off,
+					  err);
+				goto fail_read_log_header;
+			}
+		} else
+			*log_bytes = le32_to_cpu(pl_hdr->log_bytes);
+	} else {
+		err = -ENODATA;
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("log header is corrupted: "
+			  "seg %llu, peb %llu, page_off %u\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+		goto fail_read_log_header;
+	}
+
+fail_read_log_header:
+	kunmap_local(kaddr);
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (err == -ENODATA) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("valid header is not detected: "
+			  "seg_id %llu, peb_id %llu, page_off %u\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to read checked log header: "
+			  "seg %llu, peb %llu, "
+			  "pages_off %u, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  page_off, err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_read_all_log_headers() - read all PEB's log headers
+ * @pebi: pointer on PEB object
+ * @req: read request
+ *
+ * This function tries to read all headers and footers of
+ * the PEB's logs.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_read_all_log_headers(struct ssdfs_peb_info *pebi,
+				   struct ssdfs_segment_request *req)
+{
+	struct ssdfs_fs_info *fsi;
+	u32 log_bytes = U32_MAX;
+	u32 page_off;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!req);
+
+	SSDFS_DBG("seg %llu, peb_index %u, "
+		  "class %#x, cmd %#x, type %#x, "
+		  "ino %llu, logical_offset %llu, data_bytes %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->pebc->peb_index,
+		  req->private.class, req->private.cmd, req->private.type,
+		  req->extent.ino, req->extent.logical_offset,
+		  req->extent.data_bytes);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	page_off = 0;
+
+	do {
+		u32 pages_per_log;
+
+		err = __ssdfs_peb_read_log_header(fsi, pebi, page_off,
+						  &log_bytes);
+		if (err == -ENODATA)
+			return 0;
+		else if (unlikely(err)) {
+			SSDFS_ERR("fail to read log header: "
+				  "seg %llu, peb %llu, page_off %u, "
+				  "err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  page_off,
+				  err);
+			return err;
+		}
+
+#ifdef CONFIG_SSDFS_DEBUG
+		BUG_ON(log_bytes >= U32_MAX);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		pages_per_log = log_bytes + fsi->pagesize - 1;
+		pages_per_log /= fsi->pagesize;
+		page_off += pages_per_log;
+	} while (page_off < fsi->pages_per_peb);
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_read_src_all_log_headers() - read all source PEB's log headers
+ * @pebi: pointer on PEB object
+ * @req: read request
+ *
+ * This function tries to read all headers and footers of
+ * the source PEB's logs.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_read_src_all_log_headers(struct ssdfs_peb_container *pebc,
+					struct ssdfs_segment_request *req)
+{
+	struct ssdfs_peb_info *pebi;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+	BUG_ON(!req);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	down_read(&pebc->lock);
+
+	pebi = pebc->src_peb;
+	if (!pebi) {
+		SSDFS_WARN("source PEB is NULL\n");
+		err = -ERANGE;
+		goto finish_read_src_all_log_headers;
+	}
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+	SSDFS_ERR("seg_id %llu, peb_index %u, peb_id %llu\n",
+		  pebc->parent_si->seg_id,
+		  pebc->peb_index,
+		  pebi->peb_id);
+#else
+	SSDFS_DBG("seg_id %llu, peb_index %u, peb_id %llu\n",
+		  pebc->parent_si->seg_id,
+		  pebc->peb_index,
+		  pebi->peb_id);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+	err = ssdfs_peb_read_all_log_headers(pebi, req);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read the log's headers: "
+			  "peb_id %llu, peb_index %u, err %d\n",
+			  pebi->peb_id, pebi->peb_index, err);
+		goto finish_read_src_all_log_headers;
+	}
+
+finish_read_src_all_log_headers:
+	up_read(&pebc->lock);
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+	SSDFS_ERR("finished: err %d\n", err);
+#else
+	SSDFS_DBG("finished: err %d\n", err);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+	return err;
+}
+
+/*
+ * ssdfs_peb_read_dst_all_log_headers() - read all dst PEB's log headers
+ * @pebi: pointer on PEB object
+ * @req: read request
+ *
+ * This function tries to read all headers and footers of
+ * the destination PEB's logs.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_read_dst_all_log_headers(struct ssdfs_peb_container *pebc,
+					struct ssdfs_segment_request *req)
+{
+	struct ssdfs_peb_info *pebi;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+	BUG_ON(!req);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	down_read(&pebc->lock);
+
+	pebi = pebc->dst_peb;
+	if (!pebi) {
+		SSDFS_WARN("destination PEB is NULL\n");
+		err = -ERANGE;
+		goto finish_read_dst_all_log_headers;
+	}
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+	SSDFS_ERR("seg_id %llu, peb_index %u, peb_id %llu\n",
+		  pebc->parent_si->seg_id,
+		  pebc->peb_index,
+		  pebi->peb_id);
+#else
+	SSDFS_DBG("seg_id %llu, peb_index %u, peb_id %llu\n",
+		  pebc->parent_si->seg_id,
+		  pebc->peb_index,
+		  pebi->peb_id);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+	err = ssdfs_peb_read_all_log_headers(pebi, req);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read the log's headers: "
+			  "peb_id %llu, peb_index %u, err %d\n",
+			  pebi->peb_id, pebi->peb_index, err);
+		goto finish_read_dst_all_log_headers;
+	}
+
+finish_read_dst_all_log_headers:
+	up_read(&pebc->lock);
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+	SSDFS_ERR("finished: err %d\n", err);
+#else
+	SSDFS_DBG("finished: err %d\n", err);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+	return err;
+}
+
+/*
+ * ssdfs_peb_get_log_pages_count() - determine count of pages in the log
+ * @fsi: file system info object
+ * @pebi: PEB object
+ * @env: init environment [in | out]
+ *
+ * This function reads segment header of the first log in
+ * segment and to retrieve log_pages field. Also it initilizes
+ * current and previous PEB migration IDs.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_get_log_pages_count(struct ssdfs_fs_info *fsi,
+				  struct ssdfs_peb_info *pebi,
+				  struct ssdfs_read_init_env *env)
+{
+	struct ssdfs_signature *magic;
+	struct page *page;
+	size_t hdr_buf_size = sizeof(struct ssdfs_segment_header);
+	u32 log_pages;
+	u32 pages_off = 0;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!fsi || !env || !env->log_hdr);
+
+	SSDFS_DBG("peb %llu, env %p\n", pebi->peb_id, env);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	page = ssdfs_page_array_get_page_locked(&pebi->cache, 0);
+	if (IS_ERR_OR_NULL(page)) {
+		err = ssdfs_read_checked_segment_header(fsi,
+							pebi->peb_id,
+							0,
+							env->log_hdr,
+							false);
+		if (err) {
+			SSDFS_ERR("fail to read checked segment header: "
+				  "peb %llu, err %d\n",
+				  pebi->peb_id, err);
+			return err;
+		}
+	} else {
+		ssdfs_memcpy_from_page(env->log_hdr, 0, hdr_buf_size,
+					page, 0, PAGE_SIZE,
+					hdr_buf_size);
+
+		ssdfs_unlock_page(page);
+		ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("page %p, count %d\n",
+			  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+	magic = (struct ssdfs_signature *)env->log_hdr;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	if (!is_ssdfs_magic_valid(magic)) {
+		SSDFS_ERR("valid magic is not detected\n");
+		return -ERANGE;
+	}
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (__is_ssdfs_segment_header_magic_valid(magic)) {
+		struct ssdfs_segment_header *seg_hdr;
+
+		seg_hdr = SSDFS_SEG_HDR(env->log_hdr);
+		log_pages = le16_to_cpu(seg_hdr->log_pages);
+		env->log_pages = log_pages;
+		env->cur_migration_id =
+			seg_hdr->peb_migration_id[SSDFS_CUR_MIGRATING_PEB];
+		env->prev_migration_id =
+			seg_hdr->peb_migration_id[SSDFS_PREV_MIGRATING_PEB];
+	} else {
+		SSDFS_ERR("log header is corrupted: "
+			  "peb %llu, pages_off %u\n",
+			  pebi->peb_id, pages_off);
+		return -ERANGE;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	if (fsi->pages_per_peb % log_pages) {
+		SSDFS_WARN("fsi->pages_per_peb %u, log_pages %u\n",
+			   fsi->pages_per_peb, log_pages);
+	}
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (log_pages > fsi->pages_per_peb) {
+		SSDFS_ERR("log_pages %u > fsi->pages_per_peb %u\n",
+			  log_pages, fsi->pages_per_peb);
+		return -ERANGE;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_find_last_partial_log() - find the last partial log
+ * @fsi: file system info object
+ * @pebi: pointer on PEB object
+ * @env: init environment [in|out]
+ * @new_log_start_page: pointer on the new log's start page [out]
+ *
+ * This function tries to find the last partial log
+ * in the PEB's cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ */
+static
+int ssdfs_find_last_partial_log(struct ssdfs_fs_info *fsi,
+				struct ssdfs_peb_info *pebi,
+				struct ssdfs_read_init_env *env,
+				u16 *new_log_start_page)
+{
+	struct ssdfs_signature *magic = NULL;
+	struct ssdfs_segment_header *seg_hdr = NULL;
+	struct ssdfs_partial_log_header *pl_hdr = NULL;
+	struct ssdfs_log_footer *footer = NULL;
+	struct page *page;
+	void *kaddr;
+	size_t hdr_buf_size = sizeof(struct ssdfs_segment_header);
+	u32 byte_offset, page_offset;
+	unsigned long last_page_idx;
+	int i;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!fsi || !pebi || !pebi->pebc || !env);
+	BUG_ON(!new_log_start_page);
+
+	SSDFS_DBG("seg %llu, peb %llu, peb_index %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, pebi->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*new_log_start_page = U16_MAX;
+
+	last_page_idx = ssdfs_page_array_get_last_page_index(&pebi->cache);
+
+	if (last_page_idx >= SSDFS_PAGE_ARRAY_INVALID_LAST_PAGE) {
+		SSDFS_ERR("empty page array: last_page_idx %lu\n",
+			  last_page_idx);
+		return -ERANGE;
+	}
+
+	if (last_page_idx >= fsi->pages_per_peb) {
+		SSDFS_ERR("corrupted page array: "
+			  "last_page_idx %lu, fsi->pages_per_peb %u\n",
+			  last_page_idx, fsi->pages_per_peb);
+		return -ERANGE;
+	}
+
+	for (i = (int)last_page_idx; i >= 0; i--) {
+		page = ssdfs_page_array_get_page_locked(&pebi->cache, i);
+		if (IS_ERR_OR_NULL(page)) {
+			if (page == NULL) {
+				SSDFS_ERR("fail to get page: "
+					  "index %d\n",
+					  i);
+				return -ERANGE;
+			} else {
+				err = PTR_ERR(page);
+
+				if (err == -ENOENT)
+					continue;
+				else {
+					SSDFS_ERR("fail to get page: "
+						  "index %d, err %d\n",
+						  i, err);
+					return err;
+				}
+			}
+		}
+
+		kaddr = kmap_local_page(page);
+		ssdfs_memcpy(env->log_hdr, 0, hdr_buf_size,
+			     kaddr, 0, PAGE_SIZE,
+			     hdr_buf_size);
+		ssdfs_memcpy(env->footer, 0, hdr_buf_size,
+			     kaddr, 0, PAGE_SIZE,
+			     hdr_buf_size);
+		kunmap_local(kaddr);
+		ssdfs_unlock_page(page);
+		ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("page_index %d, page %p, count %d\n",
+			  i, page, page_ref_count(page));
+
+		SSDFS_DBG("PAGE DUMP: cur_page %u\n",
+			  i);
+		kaddr = kmap_local_page(page);
+		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
+				     kaddr, PAGE_SIZE);
+		kunmap_local(kaddr);
+		SSDFS_DBG("\n");
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		magic = (struct ssdfs_signature *)env->log_hdr;
+
+		if (!is_ssdfs_magic_valid(magic))
+			continue;
+
+		if (__is_ssdfs_segment_header_magic_valid(magic)) {
+			seg_hdr = SSDFS_SEG_HDR(env->log_hdr);
+
+			err = ssdfs_check_segment_header(fsi, seg_hdr,
+							 false);
+			if (unlikely(err)) {
+				SSDFS_ERR("log header is corrupted: "
+					  "seg %llu, peb %llu, index %u\n",
+					  pebi->pebc->parent_si->seg_id,
+					  pebi->peb_id,
+					  i);
+				return -EIO;
+			}
+
+			if (*new_log_start_page >= U16_MAX) {
+				SSDFS_ERR("invalid new_log_start_page\n");
+				return -EIO;
+			}
+
+			byte_offset = i * fsi->pagesize;
+			byte_offset += env->log_bytes;
+			byte_offset += fsi->pagesize - 1;
+			page_offset = byte_offset / fsi->pagesize;
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("byte_offset %u, page_offset %u, "
+				  "new_log_start_page %u\n",
+				  byte_offset, page_offset, *new_log_start_page);
+			SSDFS_DBG("log_bytes %u\n", env->log_bytes);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			if (*new_log_start_page < page_offset) {
+#ifdef CONFIG_SSDFS_DEBUG
+				SSDFS_DBG("correct new log start page: "
+					  "old value %u, new value %u\n",
+					  *new_log_start_page,
+					  page_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+				*new_log_start_page = page_offset;
+			} else if (page_offset != *new_log_start_page) {
+				SSDFS_ERR("invalid new log start: "
+					  "page_offset %u, "
+					  "new_log_start_page %u\n",
+					  page_offset,
+					  *new_log_start_page);
+				return -EIO;
+			}
+
+			env->log_offset = (u16)i;
+			pebi->peb_create_time =
+				le64_to_cpu(seg_hdr->peb_create_time);
+			pebi->current_log.last_log_time =
+				le64_to_cpu(seg_hdr->timestamp);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("seg %llu, peb %llu, "
+				  "peb_create_time %llx, last_log_time %llx\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  pebi->peb_create_time,
+				  pebi->current_log.last_log_time);
+
+			BUG_ON(pebi->peb_create_time >
+				pebi->current_log.last_log_time);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			goto finish_last_log_search;
+		} else if (is_ssdfs_partial_log_header_magic_valid(magic)) {
+			u32 flags;
+
+			pl_hdr = SSDFS_PLH(env->log_hdr);
+
+			err = ssdfs_check_partial_log_header(fsi, pl_hdr,
+							     false);
+			if (unlikely(err)) {
+				SSDFS_ERR("partial log header is corrupted: "
+					  "seg %llu, peb %llu, index %u\n",
+					  pebi->pebc->parent_si->seg_id,
+					  pebi->peb_id,
+					  i);
+				return -EIO;
+			}
+
+			flags = le32_to_cpu(pl_hdr->pl_flags);
+
+			if (flags & SSDFS_PARTIAL_HEADER_INSTEAD_FOOTER) {
+				/* first partial log */
+#ifdef CONFIG_SSDFS_DEBUG
+				BUG_ON((i + 1) >= U16_MAX);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+				byte_offset = (i + 1) * fsi->pagesize;
+				byte_offset += fsi->pagesize - 1;
+
+#ifdef CONFIG_SSDFS_DEBUG
+				SSDFS_DBG("byte_offset %u, "
+					  "new_log_start_page %u\n",
+					  byte_offset, *new_log_start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+				*new_log_start_page =
+					(u16)(byte_offset / fsi->pagesize);
+				env->log_bytes =
+					le32_to_cpu(pl_hdr->log_bytes);
+
+#ifdef CONFIG_SSDFS_DEBUG
+				SSDFS_DBG("log_bytes %u\n", env->log_bytes);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+				continue;
+			} else if (flags & SSDFS_LOG_HAS_FOOTER) {
+				/* last partial log */
+
+				env->log_bytes =
+					le32_to_cpu(pl_hdr->log_bytes);
+
+				byte_offset = i * fsi->pagesize;
+				byte_offset += env->log_bytes;
+				byte_offset += fsi->pagesize - 1;
+				page_offset = byte_offset / fsi->pagesize;
+
+#ifdef CONFIG_SSDFS_DEBUG
+				SSDFS_DBG("byte_offset %u, page_offset %u, "
+					  "new_log_start_page %u\n",
+					  byte_offset, page_offset, *new_log_start_page);
+				SSDFS_DBG("log_bytes %u\n", env->log_bytes);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+				if (*new_log_start_page < page_offset) {
+#ifdef CONFIG_SSDFS_DEBUG
+					SSDFS_DBG("correct new log start page: "
+						  "old value %u, "
+						  "new value %u\n",
+						  *new_log_start_page,
+						  page_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+					*new_log_start_page = page_offset;
+				} else if (page_offset != *new_log_start_page) {
+					SSDFS_ERR("invalid new log start: "
+						  "page_offset %u, "
+						  "new_log_start_page %u\n",
+						  page_offset,
+						  *new_log_start_page);
+					return -EIO;
+				}
+
+				env->log_offset = (u16)i;
+				pebi->peb_create_time =
+					le64_to_cpu(pl_hdr->peb_create_time);
+				pebi->current_log.last_log_time =
+					le64_to_cpu(pl_hdr->timestamp);
+
+#ifdef CONFIG_SSDFS_DEBUG
+				SSDFS_DBG("seg %llu, peb %llu, "
+					  "peb_create_time %llx, last_log_time %llx\n",
+					  pebi->pebc->parent_si->seg_id,
+					  pebi->peb_id,
+					  pebi->peb_create_time,
+					  pebi->current_log.last_log_time);
+
+				BUG_ON(pebi->peb_create_time >
+					pebi->current_log.last_log_time);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+				goto finish_last_log_search;
+			} else {
+				/* intermediate partial log */
+
+				env->log_bytes =
+					le32_to_cpu(pl_hdr->log_bytes);
+
+				byte_offset = i * fsi->pagesize;
+				byte_offset += env->log_bytes;
+				byte_offset += fsi->pagesize - 1;
+				page_offset = byte_offset / fsi->pagesize;
+
+#ifdef CONFIG_SSDFS_DEBUG
+				SSDFS_DBG("byte_offset %u, page_offset %u, "
+					  "new_log_start_page %u\n",
+					  byte_offset, page_offset, *new_log_start_page);
+				SSDFS_DBG("log_bytes %u\n", env->log_bytes);
+
+				BUG_ON(page_offset >= U16_MAX);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+				*new_log_start_page = (u16)page_offset;
+				env->log_offset = (u16)i;
+				pebi->peb_create_time =
+					le64_to_cpu(pl_hdr->peb_create_time);
+				pebi->current_log.last_log_time =
+					le64_to_cpu(pl_hdr->timestamp);
+
+#ifdef CONFIG_SSDFS_DEBUG
+				SSDFS_DBG("seg %llu, peb %llu, "
+					  "peb_create_time %llx, last_log_time %llx\n",
+					  pebi->pebc->parent_si->seg_id,
+					  pebi->peb_id,
+					  pebi->peb_create_time,
+					  pebi->current_log.last_log_time);
+
+				BUG_ON(pebi->peb_create_time >
+					pebi->current_log.last_log_time);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+				goto finish_last_log_search;
+			}
+		} else if (__is_ssdfs_log_footer_magic_valid(magic)) {
+			footer = SSDFS_LF(env->footer);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			BUG_ON((i + 1) >= U16_MAX);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			byte_offset = (i + 1) * fsi->pagesize;
+			byte_offset += fsi->pagesize - 1;
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("byte_offset %u, new_log_start_page %u\n",
+				  byte_offset, *new_log_start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			*new_log_start_page =
+				(u16)(byte_offset / fsi->pagesize);
+			env->log_bytes =
+				le32_to_cpu(footer->log_bytes);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("log_bytes %u\n", env->log_bytes);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			continue;
+		} else {
+			SSDFS_ERR("log header is corrupted: "
+				  "seg %llu, peb %llu, index %u\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  i);
+			return -ERANGE;
+		}
+	}
+
+finish_last_log_search:
+	if (env->log_offset >= fsi->pages_per_peb) {
+		SSDFS_ERR("log_offset %u >= pages_per_peb %u\n",
+			  env->log_offset, fsi->pages_per_peb);
+		return -ERANGE;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	if (fsi->erasesize < env->log_bytes) {
+		SSDFS_WARN("fsi->erasesize %u, log_bytes %u\n",
+			   fsi->erasesize,
+			   env->log_bytes);
+	}
+
+	SSDFS_DBG("seg %llu, peb %llu, peb_index %u, "
+		  "new_log_start_page %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, pebi->peb_index,
+		  *new_log_start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	return 0;
+}
+
+/*
+ * ssdfs_check_log_header() - check log's header
+ * @fsi: file system info object
+ * @env: init environment [in|out]
+ *
+ * This function checks the log's header.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ENODATA    - valid magic is not detected.
+ */
+static inline
+int ssdfs_check_log_header(struct ssdfs_fs_info *fsi,
+			   struct ssdfs_read_init_env *env)
+{
+	struct ssdfs_signature *magic = NULL;
+	struct ssdfs_segment_header *seg_hdr = NULL;
+	struct ssdfs_partial_log_header *pl_hdr = NULL;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!env || !env->log_hdr || !env->footer);
+
+	SSDFS_DBG("log_offset %u, log_pages %u\n",
+		  env->log_offset, env->log_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	magic = (struct ssdfs_signature *)env->log_hdr;
+
+	if (!is_ssdfs_magic_valid(magic)) {
+		SSDFS_DBG("valid magic is not detected\n");
+		return -ENODATA;
+	}
+
+	if (__is_ssdfs_segment_header_magic_valid(magic)) {
+		seg_hdr = SSDFS_SEG_HDR(env->log_hdr);
+
+		err = ssdfs_check_segment_header(fsi, seg_hdr,
+						 false);
+		if (unlikely(err)) {
+			SSDFS_ERR("log header is corrupted\n");
+			return -EIO;
+		}
+
+		env->has_seg_hdr = true;
+		env->has_footer = ssdfs_log_has_footer(seg_hdr);
+	} else if (is_ssdfs_partial_log_header_magic_valid(magic)) {
+		pl_hdr = SSDFS_PLH(env->log_hdr);
+
+		err = ssdfs_check_partial_log_header(fsi, pl_hdr,
+						     false);
+		if (unlikely(err)) {
+			SSDFS_ERR("partial log header is corrupted\n");
+			return -EIO;
+		}
+
+		env->has_seg_hdr = false;
+		env->has_footer = ssdfs_pl_has_footer(pl_hdr);
+	} else {
+		SSDFS_DBG("log header is corrupted\n");
+		return -EIO;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_get_segment_header_blk_bmap_desc() - get block bitmap's descriptor
+ * @pebi: pointer on PEB object
+ * @env: init environment [in]
+ * @desc: block bitmap's descriptor [out]
+ *
+ * This function tries to extract the block bitmap's descriptor.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ */
+static
+int ssdfs_get_segment_header_blk_bmap_desc(struct ssdfs_peb_info *pebi,
+					struct ssdfs_read_init_env *env,
+					struct ssdfs_metadata_descriptor **desc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_segment_header *seg_hdr = NULL;
+	size_t footer_size = sizeof(struct ssdfs_log_footer);
+	u32 pages_off;
+	u32 bytes_off;
+	struct page *page;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !env || !desc);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	*desc = NULL;
+
+	if (!env->has_seg_hdr) {
+		SSDFS_ERR("segment header is absent\n");
+		return -ERANGE;
+	}
+
+	seg_hdr = SSDFS_SEG_HDR(env->log_hdr);
+
+	if (!ssdfs_seg_hdr_has_blk_bmap(seg_hdr)) {
+		if (!env->has_footer) {
+			ssdfs_fs_error(fsi->sb, __FILE__, __func__,
+					__LINE__,
+					"log hasn't footer\n");
+			return -EIO;
+		}
+
+		*desc = &seg_hdr->desc_array[SSDFS_LOG_FOOTER_INDEX];
+
+		bytes_off = le32_to_cpu((*desc)->offset);
+		pages_off = bytes_off / fsi->pagesize;
+
+		page = ssdfs_page_array_get_page_locked(&pebi->cache,
+							pages_off);
+		if (IS_ERR_OR_NULL(page)) {
+			err = ssdfs_read_checked_log_footer(fsi,
+							    env->log_hdr,
+							    pebi->peb_id,
+							    bytes_off,
+							    env->footer,
+							    false);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to read checked log footer: "
+					  "seg %llu, peb %llu, bytes_off %u\n",
+					  pebi->pebc->parent_si->seg_id,
+					  pebi->peb_id, bytes_off);
+				return err;
+			}
+		} else {
+			ssdfs_memcpy_from_page(env->footer, 0, footer_size,
+						page, 0, PAGE_SIZE,
+						footer_size);
+
+			ssdfs_unlock_page(page);
+			ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+
+		if (!ssdfs_log_footer_has_blk_bmap(env->footer)) {
+			ssdfs_fs_error(fsi->sb, __FILE__, __func__,
+					__LINE__,
+					"log hasn't block bitmap\n");
+			return -EIO;
+		}
+
+		*desc = &env->footer->desc_array[SSDFS_BLK_BMAP_INDEX];
+	} else
+		*desc = &seg_hdr->desc_array[SSDFS_BLK_BMAP_INDEX];
+
+	return 0;
+}
+
+/*
+ * ssdfs_get_partial_header_blk_bmap_desc() - get block bitmap's descriptor
+ * @pebi: pointer on PEB object
+ * @env: init environment [in]
+ * @desc: block bitmap's descriptor [out]
+ *
+ * This function tries to extract the block bitmap's descriptor.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ */
+static
+int ssdfs_get_partial_header_blk_bmap_desc(struct ssdfs_peb_info *pebi,
+					struct ssdfs_read_init_env *env,
+					struct ssdfs_metadata_descriptor **desc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_partial_log_header *pl_hdr = NULL;
+	size_t footer_size = sizeof(struct ssdfs_log_footer);
+	u32 pages_off;
+	u32 bytes_off;
+	struct page *page;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !env || !desc);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	*desc = NULL;
+
+	if (env->has_seg_hdr) {
+		SSDFS_ERR("partial log header is absent\n");
+		return -ERANGE;
+	}
+
+	pl_hdr = SSDFS_PLH(env->log_hdr);
+
+	if (!ssdfs_pl_hdr_has_blk_bmap(pl_hdr)) {
+		if (!env->has_footer) {
+			ssdfs_fs_error(fsi->sb, __FILE__, __func__,
+					__LINE__,
+					"log hasn't footer\n");
+			return -EIO;
+		}
+
+		*desc = &pl_hdr->desc_array[SSDFS_LOG_FOOTER_INDEX];
+
+		bytes_off = le32_to_cpu((*desc)->offset);
+		pages_off = bytes_off / fsi->pagesize;
+
+		page = ssdfs_page_array_get_page_locked(&pebi->cache,
+							pages_off);
+		if (IS_ERR_OR_NULL(page)) {
+			err = ssdfs_read_checked_log_footer(fsi,
+							    env->log_hdr,
+							    pebi->peb_id,
+							    bytes_off,
+							    env->footer,
+							    false);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to read checked log footer: "
+					  "seg %llu, peb %llu, bytes_off %u\n",
+					  pebi->pebc->parent_si->seg_id,
+					  pebi->peb_id, bytes_off);
+				return err;
+			}
+		} else {
+			ssdfs_memcpy_from_page(env->footer, 0, footer_size,
+						page, 0, PAGE_SIZE,
+						footer_size);
+
+			ssdfs_unlock_page(page);
+			ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+
+		if (!ssdfs_log_footer_has_blk_bmap(env->footer)) {
+			ssdfs_fs_error(fsi->sb, __FILE__, __func__,
+					__LINE__,
+					"log hasn't block bitmap\n");
+			return -EIO;
+		}
+
+		*desc = &env->footer->desc_array[SSDFS_BLK_BMAP_INDEX];
+	} else
+		*desc = &pl_hdr->desc_array[SSDFS_BLK_BMAP_INDEX];
+
+	return 0;
+}
+
+/*
+ * ssdfs_pre_fetch_block_bitmap() - pre-fetch block bitmap
+ * @pebi: pointer on PEB object
+ * @env: init environment [in|out]
+ *
+ * This function tries to check the presence of block bitmap
+ * in the PEB's cache. Otherwise, it tries to read the block
+ * bitmap from the volume into the PEB's cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_pre_fetch_block_bitmap(struct ssdfs_peb_info *pebi,
+				 struct ssdfs_read_init_env *env)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_metadata_descriptor *desc = NULL;
+	struct page *page;
+	void *kaddr;
+	u32 pages_off;
+	u32 bytes_off;
+	size_t hdr_buf_size = sizeof(struct ssdfs_segment_header);
+	u32 area_offset, area_size;
+	u32 cur_page, page_start, page_end;
+	size_t read_bytes;
+	size_t bmap_hdr_size = sizeof(struct ssdfs_block_bitmap_header);
+	u32 pebsize;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !env);
+
+	SSDFS_DBG("seg %llu, peb %llu, peb_index %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, pebi->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	pages_off = env->log_offset;
+	pebsize = fsi->pages_per_peb * fsi->pagesize;
+
+	page = ssdfs_page_array_get_page_locked(&pebi->cache, pages_off);
+	if (IS_ERR_OR_NULL(page)) {
+		err = ssdfs_read_checked_segment_header(fsi,
+							pebi->peb_id,
+							pages_off,
+							env->log_hdr,
+							false);
+		if (err) {
+			SSDFS_ERR("fail to read checked segment header: "
+				  "peb %llu, err %d\n",
+				  pebi->peb_id, err);
+			return err;
+		}
+	} else {
+		ssdfs_memcpy_from_page(env->log_hdr, 0, hdr_buf_size,
+					page, 0, PAGE_SIZE,
+					hdr_buf_size);
+
+		ssdfs_unlock_page(page);
+		ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("page %p, count %d\n",
+			  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+	err = ssdfs_check_log_header(fsi, env);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to check log header: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	if (env->has_seg_hdr)
+		err = ssdfs_get_segment_header_blk_bmap_desc(pebi, env, &desc);
+	else
+		err = ssdfs_get_partial_header_blk_bmap_desc(pebi, env, &desc);
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to get descriptor: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	if (!desc) {
+		SSDFS_ERR("invalid descriptor pointer\n");
+		return -ERANGE;
+	}
+
+	area_offset = le32_to_cpu(desc->offset);
+	area_size = le32_to_cpu(desc->size);
+
+	if (bmap_hdr_size != le16_to_cpu(desc->check.bytes)) {
+		ssdfs_fs_error(fsi->sb, __FILE__, __func__, __LINE__,
+				"bmap_hdr_size %zu != desc->check.bytes %u\n",
+				bmap_hdr_size,
+				le16_to_cpu(desc->check.bytes));
+		return -EIO;
+	}
+
+	if (area_offset >= pebsize) {
+		ssdfs_fs_error(fsi->sb, __FILE__, __func__, __LINE__,
+				"desc->offset %u >= pebsize %u\n",
+				area_offset, pebsize);
+		return -EIO;
+	}
+
+	bytes_off = area_offset;
+	page_start = bytes_off / fsi->pagesize;
+	bytes_off += area_size - 1;
+	page_end = bytes_off / fsi->pagesize;
+
+	for (cur_page = page_start; cur_page <= page_end; cur_page++) {
+		page = ssdfs_page_array_get_page_locked(&pebi->cache,
+							cur_page);
+		if (IS_ERR_OR_NULL(page)) {
+			page = ssdfs_page_array_grab_page(&pebi->cache,
+							  cur_page);
+			if (unlikely(IS_ERR_OR_NULL(page))) {
+				SSDFS_ERR("fail to grab page: index %u\n",
+					  cur_page);
+				return -ENOMEM;
+			}
+
+			kaddr = kmap_local_page(page);
+			err = ssdfs_aligned_read_buffer(fsi, pebi->peb_id,
+							cur_page * PAGE_SIZE,
+							(u8 *)kaddr,
+							PAGE_SIZE,
+							&read_bytes);
+			kunmap_local(kaddr);
+
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to read memory page: "
+					  "index %u, err %d\n",
+					  cur_page, err);
+				goto finish_read_page;
+			} else if (unlikely(read_bytes != PAGE_SIZE)) {
+				err = -ERANGE;
+				SSDFS_ERR("invalid read_bytes %zu\n",
+					  read_bytes);
+				goto finish_read_page;
+			}
+
+			SetPageUptodate(page);
+
+finish_read_page:
+			ssdfs_unlock_page(page);
+			ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+		} else {
+			ssdfs_unlock_page(page);
+			ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+	}
+
+	return err;
+}
+
+/*
+ * ssdfs_get_segment_header_blk2off_tbl_desc() - get blk2off tbl's descriptor
+ * @pebi: pointer on PEB object
+ * @env: init environment [in]
+ * @desc: blk2off tbl's descriptor [out]
+ *
+ * This function tries to extract the blk2off table's descriptor.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ * %-ENOENT     - blk2off table's descriptor is absent.
+ */
+static inline
+int ssdfs_get_segment_header_blk2off_tbl_desc(struct ssdfs_peb_info *pebi,
+					struct ssdfs_read_init_env *env,
+					struct ssdfs_metadata_descriptor **desc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_segment_header *seg_hdr = NULL;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !env || !desc);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*desc = NULL;
+	fsi = pebi->pebc->parent_si->fsi;
+
+	if (!env->has_seg_hdr) {
+		SSDFS_ERR("segment header is absent\n");
+		return -ERANGE;
+	}
+
+	seg_hdr = SSDFS_SEG_HDR(env->log_hdr);
+
+	if (!ssdfs_seg_hdr_has_offset_table(seg_hdr)) {
+		if (!env->has_footer) {
+			ssdfs_fs_error(fsi->sb, __FILE__,
+					__func__, __LINE__,
+					"log hasn't footer\n");
+			return -EIO;
+		}
+
+		if (!ssdfs_log_footer_has_offset_table(env->footer)) {
+			SSDFS_DBG("log hasn't blk2off table\n");
+			return -ENOENT;
+		}
+
+		*desc = &env->footer->desc_array[SSDFS_OFF_TABLE_INDEX];
+	} else
+		*desc = &seg_hdr->desc_array[SSDFS_OFF_TABLE_INDEX];
+
+	return 0;
+}
+
+/*
+ * ssdfs_get_segment_header_blk_desc_tbl_desc() - get blk desc tbl's descriptor
+ * @pebi: pointer on PEB object
+ * @env: init environment [in]
+ * @desc: blk desc tbl's descriptor [out]
+ *
+ * This function tries to extract the block descriptor table's descriptor.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ * %-ENOENT     - block descriptor table's descriptor is absent.
+ */
+static inline
+int ssdfs_get_segment_header_blk_desc_tbl_desc(struct ssdfs_peb_info *pebi,
+					struct ssdfs_read_init_env *env,
+					struct ssdfs_metadata_descriptor **desc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_segment_header *seg_hdr = NULL;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !env || !desc);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*desc = NULL;
+	fsi = pebi->pebc->parent_si->fsi;
+
+	if (!env->has_seg_hdr) {
+		SSDFS_ERR("segment header is absent\n");
+		return -ERANGE;
+	}
+
+	seg_hdr = SSDFS_SEG_HDR(env->log_hdr);
+
+	if (!ssdfs_log_has_blk_desc_chain(seg_hdr)) {
+		SSDFS_DBG("log hasn't block descriptor table\n");
+		return -ENOENT;
+	} else
+		*desc = &seg_hdr->desc_array[SSDFS_BLK_DESC_AREA_INDEX];
+
+	return 0;
+}
+
+/*
+ * ssdfs_get_partial_header_blk2off_tbl_desc() - get blk2off tbl's descriptor
+ * @pebi: pointer on PEB object
+ * @env: init environment [in]
+ * @desc: blk2off tbl's descriptor [out]
+ *
+ * This function tries to extract the blk2off table's descriptor.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ * %-ENOENT     - blk2off table's descriptor is absent.
+ */
+static inline
+int ssdfs_get_partial_header_blk2off_tbl_desc(struct ssdfs_peb_info *pebi,
+					struct ssdfs_read_init_env *env,
+					struct ssdfs_metadata_descriptor **desc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_partial_log_header *pl_hdr = NULL;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !env || !desc);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*desc = NULL;
+	fsi = pebi->pebc->parent_si->fsi;
+
+	if (env->has_seg_hdr) {
+		SSDFS_ERR("partial log header is absent\n");
+		return -ERANGE;
+	}
+
+	pl_hdr = SSDFS_PLH(env->log_hdr);
+
+	if (!ssdfs_pl_hdr_has_offset_table(pl_hdr)) {
+		if (!env->has_footer) {
+			SSDFS_DBG("log hasn't blk2off table\n");
+			return -ENOENT;
+		}
+
+		if (!ssdfs_log_footer_has_offset_table(env->footer)) {
+			SSDFS_DBG("log hasn't blk2off table\n");
+			return -ENOENT;
+		}
+
+		*desc = &env->footer->desc_array[SSDFS_OFF_TABLE_INDEX];
+	} else
+		*desc = &pl_hdr->desc_array[SSDFS_OFF_TABLE_INDEX];
+
+	return 0;
+}
+
+/*
+ * ssdfs_get_partial_header_blk_desc_tbl_desc() - get blk desc tbl's descriptor
+ * @pebi: pointer on PEB object
+ * @env: init environment [in]
+ * @desc: blk desc tbl's descriptor [out]
+ *
+ * This function tries to extract the block descriptor table's descriptor.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ * %-ENOENT     - block descriptor table's descriptor is absent.
+ */
+static inline
+int ssdfs_get_partial_header_blk_desc_tbl_desc(struct ssdfs_peb_info *pebi,
+					struct ssdfs_read_init_env *env,
+					struct ssdfs_metadata_descriptor **desc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_partial_log_header *pl_hdr = NULL;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !env || !desc);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*desc = NULL;
+	fsi = pebi->pebc->parent_si->fsi;
+
+	if (env->has_seg_hdr) {
+		SSDFS_ERR("partial log header is absent\n");
+		return -ERANGE;
+	}
+
+	pl_hdr = SSDFS_PLH(env->log_hdr);
+
+	if (!ssdfs_pl_has_blk_desc_chain(pl_hdr)) {
+		SSDFS_DBG("log hasn't block descriptor table\n");
+		return -ENOENT;
+	} else
+		*desc = &pl_hdr->desc_array[SSDFS_BLK_DESC_AREA_INDEX];
+
+	return 0;
+}
+
+/*
+ * ssdfs_pre_fetch_metadata_area() - pre-fetch metadata area
+ * @pebi: pointer on PEB object
+ * @desc: metadata area's descriptor
+ *
+ * This function tries to check the presence of metadata area
+ * in the PEB's cache. Otherwise, it tries to read the metadata area
+ * from the volume into the PEB's cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_pre_fetch_metadata_area(struct ssdfs_peb_info *pebi,
+				  struct ssdfs_metadata_descriptor *desc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct page *page;
+	void *kaddr;
+	u32 bytes_off;
+	u32 area_offset, area_size;
+	u32 cur_page, page_start, page_end;
+	size_t read_bytes;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !desc);
+
+	SSDFS_DBG("seg %llu, peb %llu, peb_index %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, pebi->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	area_offset = le32_to_cpu(desc->offset);
+	area_size = le32_to_cpu(desc->size);
+
+	bytes_off = area_offset;
+	page_start = bytes_off / fsi->pagesize;
+	bytes_off += area_size - 1;
+	page_end = bytes_off / fsi->pagesize;
+
+	for (cur_page = page_start; cur_page <= page_end; cur_page++) {
+		page = ssdfs_page_array_get_page_locked(&pebi->cache,
+							cur_page);
+		if (IS_ERR_OR_NULL(page)) {
+			page = ssdfs_page_array_grab_page(&pebi->cache,
+							  cur_page);
+			if (unlikely(IS_ERR_OR_NULL(page))) {
+				SSDFS_ERR("fail to grab page: index %u\n",
+					  cur_page);
+				return -ENOMEM;
+			}
+
+			kaddr = kmap_local_page(page);
+			err = ssdfs_aligned_read_buffer(fsi, pebi->peb_id,
+							cur_page * PAGE_SIZE,
+							(u8 *)kaddr,
+							PAGE_SIZE,
+							&read_bytes);
+			flush_dcache_page(page);
+			kunmap_local(kaddr);
+
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to read memory page: "
+					  "index %u, err %d\n",
+					  cur_page, err);
+				goto finish_read_page;
+			} else if (unlikely(read_bytes != PAGE_SIZE)) {
+				err = -ERANGE;
+				SSDFS_ERR("invalid read_bytes %zu\n",
+					  read_bytes);
+				goto finish_read_page;
+			}
+
+			SetPageUptodate(page);
+
+finish_read_page:
+			ssdfs_unlock_page(page);
+			ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+		} else {
+			ssdfs_unlock_page(page);
+			ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+	}
+
+	return err;
+}
+
+/*
+ * ssdfs_pre_fetch_blk2off_table_area() - pre-fetch blk2off table
+ * @pebi: pointer on PEB object
+ * @env: init environment [in|out]
+ *
+ * This function tries to check the presence of blk2off table
+ * in the PEB's cache. Otherwise, it tries to read the blk2off table
+ * from the volume into the PEB's cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ * %-ENOMEM     - fail to allocate memory.
+ * %-ENOENT     - blk2off table is absent.
+ */
+static
+int ssdfs_pre_fetch_blk2off_table_area(struct ssdfs_peb_info *pebi,
+					struct ssdfs_read_init_env *env)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_metadata_descriptor *desc = NULL;
+	struct page *page;
+	u32 pages_off;
+	size_t hdr_buf_size = sizeof(struct ssdfs_segment_header);
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !env);
+
+	SSDFS_DBG("seg %llu, peb %llu, peb_index %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, pebi->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	pages_off = env->log_offset;
+
+	page = ssdfs_page_array_get_page_locked(&pebi->cache, pages_off);
+	if (IS_ERR_OR_NULL(page)) {
+		err = ssdfs_read_checked_segment_header(fsi,
+							pebi->peb_id,
+							pages_off,
+							env->log_hdr,
+							false);
+		if (err) {
+			SSDFS_ERR("fail to read checked segment header: "
+				  "peb %llu, err %d\n",
+				  pebi->peb_id, err);
+			return err;
+		}
+	} else {
+		ssdfs_memcpy_from_page(env->log_hdr, 0, hdr_buf_size,
+					page, 0, PAGE_SIZE,
+					hdr_buf_size);
+
+		ssdfs_unlock_page(page);
+		ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("page %p, count %d\n",
+			  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+	err = ssdfs_check_log_header(fsi, env);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to check log header: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	if (env->has_seg_hdr) {
+		err = ssdfs_get_segment_header_blk2off_tbl_desc(pebi, env,
+								&desc);
+	} else {
+		err = ssdfs_get_partial_header_blk2off_tbl_desc(pebi, env,
+								&desc);
+	}
+
+	if (err == -ENOENT) {
+		SSDFS_DBG("blk2off table is absent\n");
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to get descriptor: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	if (!desc) {
+		SSDFS_ERR("invalid descriptor pointer\n");
+		return -ERANGE;
+	}
+
+	err = ssdfs_pre_fetch_metadata_area(pebi, desc);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to pre-fetch a metadata area: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_read_blk_desc_byte_stream() - read blk desc's byte stream
+ * @pebi: pointer on PEB object
+ * @read_bytes: amount of bytes for reading
+ * @env: init environment [in|out]
+ *
+ * This function tries to read blk desc table's byte stream.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ * %-EIO        - I/O error.
+ */
+static
+int ssdfs_read_blk_desc_byte_stream(struct ssdfs_peb_info *pebi,
+				    u32 read_bytes,
+				    struct ssdfs_read_init_env *env)
+{
+	struct ssdfs_fs_info *fsi;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!env);
+
+	SSDFS_DBG("seg %llu, peb %llu, read_bytes %u, "
+		  "read_off %u, write_off %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  read_bytes, env->bdt_init.read_off,
+		  env->bdt_init.write_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	while (read_bytes > 0) {
+		struct page *page = NULL;
+		void *kaddr;
+		pgoff_t page_index = env->bdt_init.write_off >> PAGE_SHIFT;
+		u32 capacity = pagevec_count(&env->bdt_init.pvec) << PAGE_SHIFT;
+		u32 offset, bytes;
+
+		if (env->bdt_init.write_off >= capacity) {
+			if (pagevec_space(&env->bdt_init.pvec) == 0) {
+				/*
+				 * Block descriptor table byte stream could be
+				 * bigger than page vector capacity.
+				 * As a result, not complete byte stream will
+				 * read and initialization will be done only
+				 * partially. The rest byte stream will be
+				 * extracted and be used for initialization
+				 * for request of particular logical block.
+				 */
+				SSDFS_DBG("pagevec is full\n");
+				return 0;
+			}
+
+			page = ssdfs_read_add_pagevec_page(&env->bdt_init.pvec);
+			if (unlikely(IS_ERR_OR_NULL(page))) {
+				err = !page ? -ENOMEM : PTR_ERR(page);
+				SSDFS_ERR("fail to add pagevec page: err %d\n",
+					  err);
+				return err;
+			}
+		} else {
+			page = env->bdt_init.pvec.pages[page_index];
+			if (unlikely(!page)) {
+				err = -ERANGE;
+				SSDFS_ERR("fail to get page: err %d\n",
+					  err);
+				return err;
+			}
+		}
+
+		offset = env->bdt_init.write_off % PAGE_SIZE;
+		bytes = min_t(u32, read_bytes, PAGE_SIZE - offset);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("offset %u, bytes %u\n",
+			  offset, bytes);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		ssdfs_lock_page(page);
+		kaddr = kmap_local_page(page);
+		err = ssdfs_unaligned_read_cache(pebi,
+						 env->bdt_init.read_off, bytes,
+						 (u8 *)kaddr + offset);
+		flush_dcache_page(page);
+		kunmap_local(kaddr);
+		ssdfs_unlock_page(page);
+
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to read page: "
+				  "seg %llu, peb %llu, offset %u, "
+				  "size %u, err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id, env->bdt_init.read_off,
+				  bytes, err);
+			return err;
+		}
+
+		read_bytes -= bytes;
+		env->bdt_init.read_off += bytes;
+		env->bdt_init.write_off += bytes;
+	};
+
+	return 0;
+}
+
+/*
+ * ssdfs_read_blk_desc_compressed_byte_stream() - read blk desc's byte stream
+ * @pebi: pointer on PEB object
+ * @read_bytes: amount of bytes for reading
+ * @env: init environment [in|out]
+ *
+ * This function tries to read blk desc table's byte stream.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ * %-EIO        - I/O error.
+ */
+static
+int ssdfs_read_blk_desc_compressed_byte_stream(struct ssdfs_peb_info *pebi,
+						u32 read_bytes,
+						struct ssdfs_read_init_env *env)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_area_block_table table;
+	struct ssdfs_fragment_desc *frag;
+	struct page *page = NULL;
+	void *kaddr;
+	size_t tbl_size = sizeof(struct ssdfs_area_block_table);
+	u32 area_offset;
+	u16 fragments_count;
+	u16 frag_uncompr_size;
+	int i;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!env);
+
+	SSDFS_DBG("seg %llu, peb %llu, read_bytes %u, "
+		  "read_off %u, write_off %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  read_bytes, env->bdt_init.read_off,
+		  env->bdt_init.write_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	area_offset = env->bdt_init.read_off;
+
+	err = ssdfs_unaligned_read_cache(pebi, area_offset, tbl_size, &table);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read area block table: "
+			  "area_offset %u, tbl_size %zu, err %d\n",
+			  area_offset, tbl_size, err);
+		return err;
+	}
+
+	if (table.chain_hdr.magic != SSDFS_CHAIN_HDR_MAGIC) {
+		SSDFS_ERR("corrupted area block table: "
+			  "magic (expected %#x, found %#x)\n",
+			  SSDFS_CHAIN_HDR_MAGIC,
+			  table.chain_hdr.magic);
+		return -EIO;
+	}
+
+	switch (table.chain_hdr.type) {
+	case SSDFS_BLK_DESC_ZLIB_CHAIN_HDR:
+	case SSDFS_BLK_DESC_LZO_CHAIN_HDR:
+		/* expected type */
+		break;
+
+	default:
+		SSDFS_ERR("unexpected area block table's type %#x\n",
+			  table.chain_hdr.type);
+		return -EIO;
+	}
+
+	fragments_count = le16_to_cpu(table.chain_hdr.fragments_count);
+
+	for (i = 0; i < fragments_count; i++) {
+		frag = &table.blk[i];
+
+		if (frag->magic != SSDFS_FRAGMENT_DESC_MAGIC) {
+			SSDFS_ERR("corrupted area block table: "
+				  "magic (expected %#x, found %#x)\n",
+				  SSDFS_FRAGMENT_DESC_MAGIC,
+				  frag->magic);
+			return -EIO;
+		}
+
+		switch (frag->type) {
+		case SSDFS_DATA_BLK_DESC_ZLIB:
+		case SSDFS_DATA_BLK_DESC_LZO:
+			/* expected type */
+			break;
+
+		default:
+			SSDFS_ERR("unexpected fragment's type %#x\n",
+				  frag->type);
+			return -EIO;
+		}
+
+		frag_uncompr_size = le16_to_cpu(frag->uncompr_size);
+
+		page = ssdfs_read_add_pagevec_page(&env->bdt_init.pvec);
+		if (unlikely(IS_ERR_OR_NULL(page))) {
+			err = !page ? -ENOMEM : PTR_ERR(page);
+			SSDFS_ERR("fail to add pagevec page: err %d\n",
+				  err);
+			return err;
+		}
+
+		ssdfs_lock_page(page);
+		kaddr = kmap_local_page(page);
+		err = __ssdfs_decompress_blk_desc_fragment(pebi, frag,
+							   area_offset,
+							   kaddr, PAGE_SIZE);
+		flush_dcache_page(page);
+		kunmap_local(kaddr);
+		ssdfs_unlock_page(page);
+
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to read page: "
+				  "seg %llu, peb %llu, offset %u, "
+				  "size %u, err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id, env->bdt_init.read_off,
+				  frag_uncompr_size, err);
+			return err;
+		}
+
+		env->bdt_init.read_off += frag_uncompr_size;
+		env->bdt_init.write_off += frag_uncompr_size;
+	}
+
+	return err;
+}
+
+/*
+ * ssdfs_pre_fetch_blk_desc_table_area() - pre-fetch blk desc table
+ * @pebi: pointer on PEB object
+ * @env: init environment [in|out]
+ *
+ * This function tries to check the presence of blk desc table
+ * in the PEB's cache. Otherwise, it tries to read the blk desc table
+ * from the volume into the PEB's cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EIO        - I/O error.
+ * %-ENOMEM     - fail to allocate memory.
+ * %-ENOENT     - blk desc table is absent.
+ */
+static
+int ssdfs_pre_fetch_blk_desc_table_area(struct ssdfs_peb_info *pebi,
+					struct ssdfs_read_init_env *env)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_metadata_descriptor *desc = NULL;
+	struct page *page;
+	u32 pages_off;
+	size_t hdr_buf_size = sizeof(struct ssdfs_segment_header);
+	u16 flags;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !env);
+
+	SSDFS_DBG("seg %llu, peb %llu, peb_index %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, pebi->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	pages_off = env->log_offset;
+	env->bdt_init.read_off = 0;
+	env->bdt_init.write_off = 0;
+
+	page = ssdfs_page_array_get_page_locked(&pebi->cache, pages_off);
+	if (IS_ERR_OR_NULL(page)) {
+		err = ssdfs_read_checked_segment_header(fsi,
+							pebi->peb_id,
+							pages_off,
+							env->log_hdr,
+							false);
+		if (err) {
+			SSDFS_ERR("fail to read checked segment header: "
+				  "peb %llu, err %d\n",
+				  pebi->peb_id, err);
+			return err;
+		}
+	} else {
+		ssdfs_memcpy_from_page(env->log_hdr, 0, hdr_buf_size,
+					page, 0, PAGE_SIZE,
+					hdr_buf_size);
+
+		ssdfs_unlock_page(page);
+		ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("page %p, count %d\n",
+			  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+	err = ssdfs_check_log_header(fsi, env);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to check log header: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	if (env->has_seg_hdr) {
+		err = ssdfs_get_segment_header_blk_desc_tbl_desc(pebi, env,
+								 &desc);
+	} else {
+		err = ssdfs_get_partial_header_blk_desc_tbl_desc(pebi, env,
+								 &desc);
+	}
+
+	if (err == -ENOENT) {
+		SSDFS_DBG("blk descriptor table is absent\n");
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to get descriptor: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	if (!desc) {
+		SSDFS_ERR("invalid descriptor pointer\n");
+		return -ERANGE;
+	}
+
+	env->bdt_init.read_off = le32_to_cpu(desc->offset);
+
+	err = ssdfs_pre_fetch_metadata_area(pebi, desc);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to pre-fetch a metadata area: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	flags = le16_to_cpu(desc->check.flags);
+
+	if ((flags & SSDFS_ZLIB_COMPRESSED) && (flags & SSDFS_LZO_COMPRESSED)) {
+		SSDFS_ERR("invalid set of flags: "
+			  "flags %#x\n",
+			  flags);
+		return -ERANGE;
+	}
+
+	if ((flags & SSDFS_ZLIB_COMPRESSED) || (flags & SSDFS_LZO_COMPRESSED)) {
+		err = ssdfs_read_blk_desc_compressed_byte_stream(pebi,
+						      le32_to_cpu(desc->size),
+						      env);
+	} else {
+		u32 read_bytes = le32_to_cpu(desc->size);
+		size_t area_tbl_size = sizeof(struct ssdfs_area_block_table);
+
+		env->bdt_init.read_off += area_tbl_size;
+
+		if (read_bytes <= area_tbl_size) {
+			SSDFS_ERR("corrupted area blocks table: "
+				  "read_bytes %u, area_tbl_size %zu\n",
+				  read_bytes, area_tbl_size);
+			return -EIO;
+		}
+
+		read_bytes -= area_tbl_size;
+
+		err = ssdfs_read_blk_desc_byte_stream(pebi, read_bytes, env);
+	}
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to prepare block descriptor table: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	return 0;
+}
+
 /*
  * ssdfs_read_checked_block_bitmap_header() - read and check block bitmap header
  * @pebi: pointer on PEB object
Signature · 1 line
2.34.1
#28 lore raw

RFC PATCH 27/76 ssdfs: read/readahead logic of PEB's thread

In reply to Viacheslav Dubeyko

This patch implements read and readahead logic.

CC Viacheslav Dubeyko <[email protected]>
CC Luka Perkov <[email protected]>
CC Bruno Banelli <[email protected]>
---
 fs/ssdfs/peb_read_thread.c | 3076 ++++++++++++++++++++++++++++++++++++
 1 file changed, 3076 insertions(+)
fs/ssdfs/peb_read_thread.c +3076 -0
index 317eef078521..764f4fdf5b0c 100644
@@ -245,6 +245,3082 @@ int ssdfs_read_blk2off_table_fragment(struct ssdfs_peb_info *pebi,
  *                          READ THREAD FUNCTIONALITY                         *
  ******************************************************************************/
 
+/*
+ * __ssdfs_peb_release_pages() - release memory pages
+ * @pebi: pointer on PEB object
+ *
+ * This method tries to release the used pages from the page
+ * array upon the init has been finished.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int __ssdfs_peb_release_pages(struct ssdfs_peb_info *pebi)
+{
+	u16 last_log_start_page = U16_MAX;
+	u16 log_pages = 0;
+	pgoff_t start, end;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi);
+	BUG_ON(!rwsem_is_locked(&pebi->pebc->lock));
+
+	SSDFS_DBG("seg_id %llu, peb_index %u, peb_id %llu\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->pebc->peb_index,
+		  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	switch (atomic_read(&pebi->current_log.state)) {
+	case SSDFS_LOG_INITIALIZED:
+	case SSDFS_LOG_CREATED:
+	case SSDFS_LOG_COMMITTED:
+		/* expected state */
+		break;
+
+	default:
+		SSDFS_ERR("invalid current log's state: "
+			  "%#x\n",
+			  atomic_read(&pebi->current_log.state));
+		return -ERANGE;
+	}
+
+	ssdfs_peb_current_log_lock(pebi);
+	last_log_start_page = pebi->current_log.start_page;
+	log_pages = pebi->log_pages;
+	ssdfs_peb_current_log_unlock(pebi);
+
+	if (last_log_start_page > 0 && last_log_start_page <= log_pages) {
+		start = 0;
+		end = last_log_start_page - 1;
+
+		err = ssdfs_page_array_release_pages(&pebi->cache,
+						     &start, end);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to release pages: "
+				  "seg_id %llu, peb_id %llu, "
+				  "start %lu, end %lu, err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id, start, end, err);
+		}
+	}
+
+	if (!err && is_ssdfs_page_array_empty(&pebi->cache)) {
+		err = -ENODATA;
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("cache is empty: "
+			  "seg_id %llu, peb_index %u, peb_id %llu\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->pebc->peb_index,
+			  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+	return err;
+}
+
+/*
+ * ssdfs_peb_release_pages_after_init() - release memory pages
+ * @pebc: pointer on PEB container
+ * @req: read request
+ *
+ * This method tries to release the used pages from the page
+ * array upon the init has been finished.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_release_pages(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_peb_info *pebi;
+	int err1 = 0, err2 = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+
+	SSDFS_DBG("seg_id %llu, peb_index %u\n",
+		  pebc->parent_si->seg_id, pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	down_write(&pebc->lock);
+
+	pebi = pebc->src_peb;
+	if (pebi) {
+		err1 = __ssdfs_peb_release_pages(pebi);
+		if (err1 == -ENODATA) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("cache is empty: "
+				  "seg_id %llu, peb_index %u\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+		} else if (unlikely(err1)) {
+			SSDFS_ERR("fail to release source PEB pages: "
+				  "seg_id %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err1);
+		}
+	}
+
+	pebi = pebc->dst_peb;
+	if (pebi) {
+		err2 = __ssdfs_peb_release_pages(pebi);
+		if (err2 == -ENODATA) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("cache is empty: "
+				  "seg_id %llu, peb_index %u\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+		} else if (unlikely(err2)) {
+			SSDFS_ERR("fail to release dest PEB pages: "
+				  "seg_id %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err2);
+		}
+	}
+
+	up_write(&pebc->lock);
+
+	if (err1 || err2) {
+		if (err1 == -ENODATA && err2 == -ENODATA)
+			return -ENODATA;
+		else if (!err1) {
+			if (err2 != -ENODATA)
+				return err2;
+			else
+				return 0;
+		} else if (!err2) {
+			if (err1 != -ENODATA)
+				return err1;
+			else
+				return 0;
+		} else
+			return -ERANGE;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_unaligned_read_cache() - unaligned read from PEB's cache
+ * @pebi: pointer on PEB object
+ * @area_offset: offset from the log's beginning
+ * @area_size: size of the data portion
+ * @buf: buffer for read
+ *
+ * This function tries to read some data portion from
+ * the PEB's cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+int ssdfs_unaligned_read_cache(struct ssdfs_peb_info *pebi,
+				u32 area_offset, u32 area_size,
+				void *buf)
+{
+	struct ssdfs_fs_info *fsi;
+	struct page *page;
+	u32 page_off;
+	u32 bytes_off;
+	size_t read_bytes = 0;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si || !buf);
+
+	SSDFS_DBG("seg %llu, peb %llu, "
+		  "area_offset %u, area_size %u, buf %p\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  area_offset, area_size, buf);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	do {
+		size_t iter_read_bytes;
+		size_t offset;
+
+		bytes_off = area_offset + read_bytes;
+		page_off = bytes_off / PAGE_SIZE;
+		offset = bytes_off % PAGE_SIZE;
+
+		iter_read_bytes = min_t(size_t,
+					(size_t)(area_size - read_bytes),
+					(size_t)(PAGE_SIZE - offset));
+
+		page = ssdfs_page_array_get_page_locked(&pebi->cache, page_off);
+		if (IS_ERR_OR_NULL(page)) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("fail to get page: index %u\n",
+				  page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return -ERANGE;
+		}
+
+		err = ssdfs_memcpy_from_page(buf, read_bytes, area_size,
+					     page, offset, PAGE_SIZE,
+					     iter_read_bytes);
+
+		ssdfs_unlock_page(page);
+		ssdfs_put_page(page);
+
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to copy: "
+				  "read_bytes %zu, offset %zu, "
+				  "iter_read_bytes %zu, err %d\n",
+				  read_bytes, offset,
+				  iter_read_bytes, err);
+			return err;
+		}
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("page %p, count %d\n",
+			  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		read_bytes += iter_read_bytes;
+	} while (read_bytes < area_size);
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_read_log_hdr_desc_array() - read log's header area's descriptors
+ * @pebi: pointer on PEB object
+ * @req: request
+ * @log_start_page: starting page of the log
+ * @array: array of area's descriptors [out]
+ * @array_size: count of items into array
+ *
+ * This function tries to read log's header area's descriptors.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ * %-ENOENT     - cache hasn't the requested page.
+ */
+int ssdfs_peb_read_log_hdr_desc_array(struct ssdfs_peb_info *pebi,
+				      struct ssdfs_segment_request *req,
+				      u16 log_start_page,
+				      struct ssdfs_metadata_descriptor *array,
+				      size_t array_size)
+{
+	struct ssdfs_fs_info *fsi;
+	struct page *page;
+	void *kaddr;
+	struct ssdfs_signature *magic = NULL;
+	struct ssdfs_segment_header *seg_hdr = NULL;
+	struct ssdfs_partial_log_header *plh_hdr = NULL;
+	size_t desc_size = sizeof(struct ssdfs_metadata_descriptor);
+	size_t array_bytes = array_size * desc_size;
+	u32 page_off;
+	size_t read_bytes;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!array);
+	BUG_ON(array_size != SSDFS_SEG_HDR_DESC_MAX);
+
+	SSDFS_DBG("seg %llu, peb %llu, log_start_page %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  log_start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	page_off = log_start_page;
+
+	page = ssdfs_page_array_get_page_locked(&pebi->cache, page_off);
+	if (unlikely(IS_ERR_OR_NULL(page))) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("unable to get page: index %u\n",
+			   page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (req->private.flags & SSDFS_REQ_READ_ONLY_CACHE)
+			return -ENOENT;
+
+		page = ssdfs_page_array_grab_page(&pebi->cache, page_off);
+		if (unlikely(IS_ERR_OR_NULL(page))) {
+			SSDFS_ERR("fail to grab page: index %u\n",
+				  page_off);
+			return -ENOMEM;
+		}
+
+		kaddr = kmap_local_page(page);
+
+		err = ssdfs_aligned_read_buffer(fsi, pebi->peb_id,
+						(page_off * PAGE_SIZE),
+						(u8 *)kaddr,
+						PAGE_SIZE,
+						&read_bytes);
+		if (unlikely(err))
+			goto fail_copy_desc_array;
+		else if (unlikely(read_bytes != (PAGE_SIZE))) {
+			err = -ERANGE;
+			goto fail_copy_desc_array;
+		}
+
+		SetPageUptodate(page);
+		flush_dcache_page(page);
+	} else
+		kaddr = kmap_local_page(page);
+
+	magic = (struct ssdfs_signature *)kaddr;
+
+	if (!is_ssdfs_magic_valid(magic)) {
+		err = -ERANGE;
+		SSDFS_ERR("valid magic is not detected\n");
+		goto fail_copy_desc_array;
+	}
+
+	if (__is_ssdfs_segment_header_magic_valid(magic)) {
+		seg_hdr = SSDFS_SEG_HDR(kaddr);
+		ssdfs_memcpy(array, 0, array_bytes,
+			     seg_hdr->desc_array, 0, array_bytes,
+			     array_bytes);
+	} else if (is_ssdfs_partial_log_header_magic_valid(magic)) {
+		plh_hdr = SSDFS_PLH(kaddr);
+		ssdfs_memcpy(array, 0, array_bytes,
+			     plh_hdr->desc_array, 0, array_bytes,
+			     array_bytes);
+	} else {
+		err = -EIO;
+		SSDFS_ERR("log header is corrupted: "
+			  "seg %llu, peb %llu, log_start_page %u\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  log_start_page);
+		goto fail_copy_desc_array;
+	}
+
+fail_copy_desc_array:
+	kunmap_local(kaddr);
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read checked segment header: "
+			  "seg %llu, peb %llu, pages_off %u, err %d\n",
+			  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			  page_off, err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_read_page_locked() - read locked page into PEB's cache
+ * @pebi: pointer on PEB object
+ * @req: request
+ * @page_off: page index
+ *
+ * This function tries to read locked page into PEB's cache.
+ */
+static
+struct page *ssdfs_peb_read_page_locked(struct ssdfs_peb_info *pebi,
+					struct ssdfs_segment_request *req,
+					u32 page_off)
+{
+	struct ssdfs_fs_info *fsi;
+	struct page *page;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+
+	SSDFS_DBG("seg %llu, peb %llu, page_off %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	page = ssdfs_page_array_get_page_locked(&pebi->cache, page_off);
+	if (unlikely(IS_ERR_OR_NULL(page))) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("unable to get page: index %u\n",
+			   page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (req->private.flags & SSDFS_REQ_READ_ONLY_CACHE)
+			return ERR_PTR(-ENOENT);
+
+		page = ssdfs_page_array_grab_page(&pebi->cache, page_off);
+		if (unlikely(IS_ERR_OR_NULL(page))) {
+			SSDFS_ERR("fail to grab page: index %u\n",
+				  page_off);
+			return NULL;
+		}
+
+		if (PageUptodate(page) || PageDirty(page))
+			goto finish_page_read;
+
+		err = ssdfs_read_page_from_volume(fsi, pebi->peb_id,
+						  page_off << PAGE_SHIFT,
+						  page);
+
+		/*
+		 * ->readpage() unlock the page
+		 * But caller expects that page is locked
+		 */
+		ssdfs_lock_page(page);
+
+		if (unlikely(err))
+			goto fail_read_page;
+
+		SetPageUptodate(page);
+	}
+
+finish_page_read:
+	return page;
+
+fail_read_page:
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	SSDFS_ERR("fail to read locked page: "
+		  "seg %llu, peb %llu, page_off %u, err %d\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  page_off, err);
+
+	return NULL;
+}
+
+/*
+ * __ssdfs_decompress_blk_desc_fragment() - decompress blk desc fragment
+ * @pebi: pointer on PEB object
+ * @frag: fragment descriptor
+ * @area_offset: area offset in bytes
+ * @read_buffer: buffer to read [out]
+ * @buf_size: size of buffer in bytes
+ *
+ * This function tries to decompress block descriptor fragment.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int __ssdfs_decompress_blk_desc_fragment(struct ssdfs_peb_info *pebi,
+					 struct ssdfs_fragment_desc *frag,
+					 u32 area_offset,
+					 void *read_buffer, size_t buf_size)
+{
+	void *cdata_buf = NULL;
+	u32 frag_offset;
+	u16 compr_size;
+	u16 uncompr_size;
+	int compr_type = SSDFS_COMPR_NONE;
+	__le32 checksum = 0;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!frag || !read_buffer);
+
+	SSDFS_DBG("seg %llu, peb %llu, area_offset %u, buf_size %zu\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, area_offset, buf_size);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	frag_offset = le32_to_cpu(frag->offset);
+	compr_size = le16_to_cpu(frag->compr_size);
+	uncompr_size = le16_to_cpu(frag->uncompr_size);
+
+	if (buf_size < uncompr_size) {
+		SSDFS_ERR("invalid request: buf_size %zu < uncompr_size %u\n",
+			  buf_size, uncompr_size);
+		return -E2BIG;
+	}
+
+	cdata_buf = ssdfs_read_kzalloc(compr_size, GFP_KERNEL);
+	if (!cdata_buf) {
+		err = -ENOMEM;
+		SSDFS_ERR("fail to allocate cdata_buf\n");
+		goto free_buf;
+	}
+
+	err = ssdfs_unaligned_read_cache(pebi,
+					 area_offset + frag_offset,
+					 compr_size,
+					 cdata_buf);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read blk desc fragment: "
+			  "frag_offset %u, compr_size %u, "
+			  "err %d\n",
+			  frag_offset, compr_size, err);
+		goto free_buf;
+	}
+
+	switch (frag->type) {
+	case SSDFS_DATA_BLK_DESC_ZLIB:
+		compr_type = SSDFS_COMPR_ZLIB;
+		break;
+
+	case SSDFS_DATA_BLK_DESC_LZO:
+		compr_type = SSDFS_COMPR_LZO;
+		break;
+
+	default:
+		BUG();
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("compr_type %#x, cdata_buf %px, read_buffer %px, "
+		  "buf_size %zu, compr_size %u, uncompr_size %u\n",
+		  compr_type, cdata_buf, read_buffer,
+		  buf_size, compr_size, uncompr_size);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_decompress(compr_type,
+				cdata_buf, read_buffer,
+				compr_size, uncompr_size);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to decompress fragment: "
+			  "seg %llu, peb %llu, "
+			  "compr_size %u, uncompr_size %u, "
+			  "err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  compr_size, uncompr_size,
+			  err);
+		goto free_buf;
+	}
+
+	if (frag->flags & SSDFS_FRAGMENT_HAS_CSUM) {
+		checksum = ssdfs_crc32_le(read_buffer, uncompr_size);
+		if (checksum != frag->checksum) {
+			err = -EIO;
+			SSDFS_ERR("invalid checksum: "
+				  "(calculated %#x, csum %#x)\n",
+				  le32_to_cpu(checksum),
+				  le32_to_cpu(frag->checksum));
+			goto free_buf;
+		}
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("BLK DESC FRAGMENT DUMP\n");
+	print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
+			     read_buffer, buf_size);
+	SSDFS_DBG("\n");
+#endif /* CONFIG_SSDFS_DEBUG */
+
+free_buf:
+	if (cdata_buf)
+		ssdfs_read_kfree(cdata_buf);
+
+	return err;
+}
+
+/*
+ * ssdfs_decompress_blk_desc_fragment() - decompress blk desc fragment
+ * @pebi: pointer on PEB object
+ * @frag: fragment descriptor
+ * @area_offset: area offset in bytes
+ *
+ * This function tries to decompress block descriptor fragment.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_decompress_blk_desc_fragment(struct ssdfs_peb_info *pebi,
+					struct ssdfs_fragment_desc *frag,
+					u32 area_offset)
+{
+	struct ssdfs_peb_read_buffer *buf;
+	u16 uncompr_size;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!frag);
+	BUG_ON(!rwsem_is_locked(&pebi->read_buffer.lock));
+
+	SSDFS_DBG("seg %llu, peb %llu, area_offset %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id,
+		  area_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	buf = &pebi->read_buffer.blk_desc;
+	uncompr_size = le16_to_cpu(frag->uncompr_size);
+
+	if (buf->size < uncompr_size) {
+		err = ssdfs_peb_realloc_read_buffer(buf, uncompr_size);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to realloc read buffer: "
+				  "old_size %zu, new_size %u, err %d\n",
+				  buf->size, uncompr_size, err);
+			return err;
+		}
+	}
+
+	return __ssdfs_decompress_blk_desc_fragment(pebi, frag, area_offset,
+						    buf->ptr, buf->size);
+}
+
+/*
+ * ssdfs_peb_decompress_blk_desc_fragment() - decompress blk desc fragment
+ * @pebi: pointer on PEB object
+ * @meta_desc: area descriptor
+ * @offset: offset in bytes to read block descriptor
+ *
+ * This function tries to decompress block descriptor fragment.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_decompress_blk_desc_fragment(struct ssdfs_peb_info *pebi,
+				struct ssdfs_metadata_descriptor *meta_desc,
+				u32 offset)
+{
+	struct ssdfs_area_block_table table;
+	size_t tbl_size = sizeof(struct ssdfs_area_block_table);
+	u32 area_offset;
+	u32 area_size;
+	u32 tbl_offset = 0;
+	u32 compr_bytes = 0;
+	u32 uncompr_bytes = 0;
+	u16 flags;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!meta_desc);
+	BUG_ON(!rwsem_is_locked(&pebi->read_buffer.lock));
+
+	SSDFS_DBG("seg %llu, peb %llu, offset %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id,
+		  offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	area_offset = le32_to_cpu(meta_desc->offset);
+	area_size = le32_to_cpu(meta_desc->size);
+
+try_read_area_block_table:
+	if ((tbl_offset + tbl_size) > area_size) {
+		SSDFS_ERR("area block table out of area: "
+			  "tbl_offset %u, tbl_size %zu, area_size %u\n",
+			  tbl_offset, tbl_size, area_size);
+		return -ERANGE;
+	}
+
+	err = ssdfs_unaligned_read_cache(pebi,
+					 area_offset + tbl_offset,
+					 tbl_size,
+					 &table);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read area block table: "
+			  "area_offset %u, area_size %u, "
+			  "tbl_offset %u, tbl_size %zu, err %d\n",
+			  area_offset, area_size,
+			  tbl_offset, tbl_size, err);
+		return err;
+	}
+
+	if (table.chain_hdr.magic != SSDFS_CHAIN_HDR_MAGIC) {
+		SSDFS_ERR("corrupted area block table: "
+			  "magic (expected %#x, found %#x)\n",
+			  SSDFS_CHAIN_HDR_MAGIC,
+			  table.chain_hdr.magic);
+		return -EIO;
+	}
+
+	switch (table.chain_hdr.type) {
+	case SSDFS_BLK_DESC_ZLIB_CHAIN_HDR:
+	case SSDFS_BLK_DESC_LZO_CHAIN_HDR:
+		/* expected type */
+		break;
+
+	default:
+		SSDFS_ERR("unexpected area block table's type %#x\n",
+			  table.chain_hdr.type);
+		return -EIO;
+	}
+
+	compr_bytes = le32_to_cpu(table.chain_hdr.compr_bytes);
+	uncompr_bytes += le32_to_cpu(table.chain_hdr.uncompr_bytes);
+
+	if (offset < uncompr_bytes) {
+		struct ssdfs_fragment_desc *frag;
+		u16 fragments_count;
+		u16 frag_uncompr_size;
+		int i;
+
+		uncompr_bytes -= le32_to_cpu(table.chain_hdr.uncompr_bytes);
+		fragments_count = le16_to_cpu(table.chain_hdr.fragments_count);
+
+		for (i = 0; i < fragments_count; i++) {
+			frag = &table.blk[i];
+
+			if (frag->magic != SSDFS_FRAGMENT_DESC_MAGIC) {
+				SSDFS_ERR("corrupted area block table: "
+					  "magic (expected %#x, found %#x)\n",
+					  SSDFS_FRAGMENT_DESC_MAGIC,
+					  frag->magic);
+				return -EIO;
+			}
+
+			switch (frag->type) {
+			case SSDFS_DATA_BLK_DESC_ZLIB:
+			case SSDFS_DATA_BLK_DESC_LZO:
+				/* expected type */
+				break;
+
+			default:
+				SSDFS_ERR("unexpected fragment's type %#x\n",
+					  frag->type);
+				return -EIO;
+			}
+
+			frag_uncompr_size = le16_to_cpu(frag->uncompr_size);
+			uncompr_bytes += frag_uncompr_size;
+
+			if (offset < uncompr_bytes) {
+				err = ssdfs_decompress_blk_desc_fragment(pebi,
+							    frag, area_offset);
+				if (unlikely(err)) {
+					SSDFS_ERR("fail to decompress: "
+						  "err %d\n", err);
+					return err;
+				}
+
+				break;
+			}
+		}
+
+		if (i >= fragments_count) {
+			SSDFS_ERR("corrupted area block table: "
+				  "i %d >= fragments_count %u\n",
+				  i, fragments_count);
+			return -EIO;
+		}
+	} else {
+		flags = le16_to_cpu(table.chain_hdr.flags);
+
+		if (!(flags & SSDFS_MULTIPLE_HDR_CHAIN)) {
+			SSDFS_ERR("corrupted area block table: "
+				  "invalid flags set %#x\n",
+				  flags);
+			return -EIO;
+		}
+
+		tbl_offset += compr_bytes;
+		goto try_read_area_block_table;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_read_block_descriptor() - read block descriptor
+ * @pebi: pointer on PEB object
+ * @meta_desc: area descriptor
+ * @offset: offset in bytes to read block descriptor
+ * @blk_desc: block descriptor [out]
+ *
+ * This function tries to read block descriptor.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_read_block_descriptor(struct ssdfs_peb_info *pebi,
+				    struct ssdfs_metadata_descriptor *meta_desc,
+				    u32 offset,
+				    struct ssdfs_block_descriptor *blk_desc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_peb_temp_read_buffers *buf;
+	size_t blk_desc_size = sizeof(struct ssdfs_block_descriptor);
+	int compr_type = SSDFS_COMPR_NONE;
+	u32 lower_bound = U32_MAX;
+	u32 upper_bound = U32_MAX;
+	u16 flags;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!meta_desc || !blk_desc);
+
+	SSDFS_DBG("seg %llu, peb %llu, offset %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id,
+		  offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	flags = le16_to_cpu(meta_desc->check.flags);
+
+	if ((flags & SSDFS_ZLIB_COMPRESSED) && (flags & SSDFS_LZO_COMPRESSED)) {
+		SSDFS_ERR("invalid set of flags: "
+			  "flags %#x\n",
+			  flags);
+		return -ERANGE;
+	}
+
+	if (flags & SSDFS_ZLIB_COMPRESSED)
+		compr_type = SSDFS_COMPR_ZLIB;
+	else if (flags & SSDFS_LZO_COMPRESSED)
+		compr_type = SSDFS_COMPR_LZO;
+
+	if (compr_type != SSDFS_COMPR_NONE) {
+		buf = &pebi->read_buffer;
+
+		down_write(&buf->lock);
+
+		if (!buf->blk_desc.ptr) {
+			err = -ENOMEM;
+			SSDFS_ERR("buffer is not allocated\n");
+			goto finish_decompress;
+		}
+
+		lower_bound = buf->blk_desc.offset;
+		upper_bound = buf->blk_desc.offset + buf->blk_desc.size;
+
+		if (buf->blk_desc.offset >= U32_MAX) {
+			err = ssdfs_peb_decompress_blk_desc_fragment(pebi,
+								     meta_desc,
+								     offset);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to decompress: err %d\n",
+					  err);
+				goto finish_decompress;
+			}
+		} else if (offset < lower_bound || offset >= upper_bound) {
+			err = ssdfs_peb_decompress_blk_desc_fragment(pebi,
+								     meta_desc,
+								     offset);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to decompress: err %d\n",
+					  err);
+				goto finish_decompress;
+			}
+		}
+
+finish_decompress:
+		downgrade_write(&buf->lock);
+
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to decompress portion: "
+				  "err %d\n", err);
+			goto finish_read_compressed_blk_desc;
+		}
+
+		err = ssdfs_memcpy(blk_desc,
+				   0, blk_desc_size,
+				   buf->blk_desc.ptr,
+				   offset - lower_bound, buf->blk_desc.size,
+				   blk_desc_size);
+		if (unlikely(err)) {
+			SSDFS_ERR("invalid buffer state: "
+				  "offset %u, buffer (offset %u, size %zu)\n",
+				  offset,
+				  buf->blk_desc.offset,
+				  buf->blk_desc.size);
+			goto finish_read_compressed_blk_desc;
+		}
+
+finish_read_compressed_blk_desc:
+		up_read(&buf->lock);
+
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to read compressed block descriptor: "
+				  "offset %u, err %d\n",
+				  offset, err);
+			return err;
+		}
+	} else {
+		err = ssdfs_unaligned_read_cache(pebi, offset,
+						 blk_desc_size,
+						 blk_desc);
+		if (err) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to read block descriptor: "
+				  "seg %llu, peb %llu, "
+				  "offset %u, err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  offset, err);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_find_block_descriptor() - find block descriptor
+ * @pebi: pointer on PEB object
+ * @req: request
+ * @array: array of area's descriptors
+ * @array_size: count of items into array
+ * @desc_off: descriptor of physical offset
+ * @blk_desc: block descriptor [out]
+ *
+ * This function tries to get block descriptor.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_find_block_descriptor(struct ssdfs_peb_info *pebi,
+				struct ssdfs_segment_request *req,
+				struct ssdfs_metadata_descriptor *array,
+				size_t array_size,
+				struct ssdfs_phys_offset_descriptor *desc_off,
+				struct ssdfs_block_descriptor *blk_desc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_blk_state_offset *blk_state;
+	struct page *page;
+	struct pagevec pvec;
+	size_t blk_desc_size = sizeof(struct ssdfs_block_descriptor);
+	int area_index;
+	u32 area_offset;
+	u32 area_size;
+	u32 blk_desc_off;
+	u64 calculated;
+	u32 page_off;
+	u32 pages_count;
+	u32 i;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!req || !array || !desc_off || !blk_desc);
+	BUG_ON(array_size != SSDFS_SEG_HDR_DESC_MAX);
+
+	SSDFS_DBG("seg %llu, peb %llu, "
+		  "log_start_page %u, log_area %#x, "
+		  "peb_migration_id %u, byte_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  le16_to_cpu(desc_off->blk_state.log_start_page),
+		  desc_off->blk_state.log_area,
+		  desc_off->blk_state.peb_migration_id,
+		  le32_to_cpu(desc_off->blk_state.byte_offset));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	blk_state = &desc_off->blk_state;
+
+	err = ssdfs_peb_read_log_hdr_desc_array(pebi, req,
+					le16_to_cpu(blk_state->log_start_page),
+					array, array_size);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read log's header desc array: "
+			  "seg %llu, peb %llu, log_start_page %u, err %d\n",
+			  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			  le16_to_cpu(blk_state->log_start_page),
+			  err);
+		return err;
+	}
+
+	area_index = SSDFS_AREA_TYPE2INDEX(blk_state->log_area);
+
+	if (area_index >= SSDFS_SEG_HDR_DESC_MAX) {
+		SSDFS_ERR("invalid area index %#x\n", area_index);
+		return -ERANGE;
+	}
+
+	area_offset = le32_to_cpu(array[area_index].offset);
+	area_size = le32_to_cpu(array[area_index].size);
+	blk_desc_off = le32_to_cpu(blk_state->byte_offset);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("area_offset %u, blk_desc_off %u\n",
+		  area_offset, blk_desc_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_read_block_descriptor(pebi, &array[area_index],
+					      area_offset + blk_desc_off,
+					      blk_desc);
+	if (err) {
+		page_off = (area_offset + blk_desc_off) / PAGE_SIZE;
+		pages_count = (area_size + PAGE_SIZE - 1) / PAGE_SIZE;
+		pages_count = min_t(u32, pages_count, PAGEVEC_SIZE);
+
+		pagevec_init(&pvec);
+
+		for (i = 0; i < pages_count; i++) {
+			page = ssdfs_page_array_grab_page(&pebi->cache,
+							  page_off + i);
+			if (unlikely(IS_ERR_OR_NULL(page))) {
+				SSDFS_ERR("fail to grab page: index %u\n",
+					  page_off);
+				return -ENOMEM;
+			}
+
+			if (PageUptodate(page) || PageDirty(page))
+				break;
+
+			pagevec_add(&pvec, page);
+		}
+
+		err = ssdfs_read_pagevec_from_volume(fsi, pebi->peb_id,
+						     page_off << PAGE_SHIFT,
+						     &pvec);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to read pagevec: "
+				  "peb_id %llu, page_off %u, "
+				  "pages_count %u, err %d\n",
+				  pebi->peb_id, page_off,
+				  pages_count, err);
+			return err;
+		}
+
+		for (i = 0; i < pagevec_count(&pvec); i++) {
+			page = pvec.pages[i];
+
+			if (!page) {
+#ifdef CONFIG_SSDFS_DEBUG
+				SSDFS_DBG("page %d is NULL\n", i);
+#endif /* CONFIG_SSDFS_DEBUG */
+				continue;
+			}
+
+			pvec.pages[i] = NULL;
+		}
+
+		pagevec_reinit(&pvec);
+
+		err = ssdfs_peb_read_block_descriptor(pebi, &array[area_index],
+						     area_offset + blk_desc_off,
+						     blk_desc);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to read block descriptor: "
+				  "peb %llu, area_offset %u, byte_offset %u, "
+				  "buf_size %zu, err %d\n",
+				  pebi->peb_id, area_offset, blk_desc_off,
+				  blk_desc_size, err);
+			return err;
+		}
+	}
+
+	if (le64_to_cpu(blk_desc->ino) != req->extent.ino) {
+		SSDFS_ERR("seg %llu, peb %llu, "
+			  "blk_desc->ino %llu != req->extent.ino %llu\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  le64_to_cpu(blk_desc->ino), req->extent.ino);
+		return -ERANGE;
+	}
+
+	calculated = (u64)req->result.processed_blks * fsi->pagesize;
+
+	if (calculated >= req->extent.data_bytes) {
+		SSDFS_ERR("calculated %llu >= req->extent.data_bytes %u\n",
+			  calculated, req->extent.data_bytes);
+		return -ERANGE;
+	}
+
+	return 0;
+}
+
+/*
+ * __ssdfs_peb_get_block_state_desc() - get block state descriptor
+ * @pebi: pointer on PEB object
+ * @req: segment request
+ * @area_desc: area descriptor
+ * @desc: block state descriptor [out]
+ * @cno: checkpoint ID [out]
+ * @parent_snapshot: parent snapshot ID [out]
+ *
+ * This function tries to get block state descriptor.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+int __ssdfs_peb_get_block_state_desc(struct ssdfs_peb_info *pebi,
+				struct ssdfs_segment_request *req,
+				struct ssdfs_metadata_descriptor *area_desc,
+				struct ssdfs_block_state_descriptor *desc,
+				u64 *cno, u64 *parent_snapshot)
+{
+	struct ssdfs_fs_info *fsi;
+	size_t state_desc_size = sizeof(struct ssdfs_block_state_descriptor);
+	u32 area_offset;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!area_desc || !desc);
+	BUG_ON(!cno || !parent_snapshot);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	area_offset = le32_to_cpu(area_desc->offset);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb %llu, area_offset %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, area_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_unaligned_read_cache(pebi,
+					 area_offset,
+					 state_desc_size,
+					 desc);
+	if (err) {
+		SSDFS_DBG("cache hasn't requested page\n");
+
+		if (req->private.flags & SSDFS_REQ_READ_ONLY_CACHE)
+			return -ENOENT;
+
+		err = ssdfs_unaligned_read_buffer(fsi, pebi->peb_id,
+						  area_offset,
+						  desc, state_desc_size);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to read buffer: "
+				  "peb %llu, area_offset %u, "
+				  "buf_size %zu, err %d\n",
+				  pebi->peb_id, area_offset,
+				  state_desc_size, err);
+			return err;
+		}
+	}
+
+	if (desc->chain_hdr.magic != SSDFS_CHAIN_HDR_MAGIC) {
+		SSDFS_ERR("chain header magic invalid\n");
+		return -EIO;
+	}
+
+	if (desc->chain_hdr.type != SSDFS_BLK_STATE_CHAIN_HDR) {
+		SSDFS_ERR("chain header type invalid\n");
+		return -EIO;
+	}
+
+	if (le16_to_cpu(desc->chain_hdr.desc_size) !=
+	    sizeof(struct ssdfs_fragment_desc)) {
+		SSDFS_ERR("fragment descriptor size is invalid\n");
+		return -EIO;
+	}
+
+	*cno = le64_to_cpu(desc->cno);
+	*parent_snapshot = le64_to_cpu(desc->parent_snapshot);
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_get_block_state_desc() - get block state descriptor
+ * @pebi: pointer on PEB object
+ * @req: request
+ * @area_desc: area descriptor
+ * @desc: block state descriptor [out]
+ *
+ * This function tries to get block state descriptor.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_get_block_state_desc(struct ssdfs_peb_info *pebi,
+				   struct ssdfs_segment_request *req,
+				   struct ssdfs_metadata_descriptor *area_desc,
+				   struct ssdfs_block_state_descriptor *desc)
+{
+	u64 cno;
+	u64 parent_snapshot;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!req || !area_desc || !desc);
+
+	SSDFS_DBG("seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = __ssdfs_peb_get_block_state_desc(pebi, req, area_desc,
+						desc, &cno, &parent_snapshot);
+	if (err == -ENOENT) {
+		SSDFS_DBG("cache hasn't requested page\n");
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to get block state descriptor: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	if (req->extent.cno != cno) {
+		SSDFS_ERR("req->extent.cno %llu != cno %llu\n",
+			  req->extent.cno, cno);
+		return -EIO;
+	}
+
+	if (req->extent.parent_snapshot != parent_snapshot) {
+		SSDFS_ERR("req->extent.parent_snapshot %llu != "
+			  "parent_snapshot %llu\n",
+			  req->extent.parent_snapshot,
+			  parent_snapshot);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_get_fragment_desc_array() - get fragment descriptors array
+ * @pebi: pointer on PEB object
+ * @req: segment request
+ * @array_offset: offset of array from the log's beginning
+ * @array: array of fragment descriptors [out]
+ * @array_size: count of items into array
+ *
+ * This function tries to get array of fragment descriptors.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_get_fragment_desc_array(struct ssdfs_peb_info *pebi,
+					struct ssdfs_segment_request *req,
+					u32 array_offset,
+					struct ssdfs_fragment_desc *array,
+					size_t array_size)
+{
+	struct ssdfs_fs_info *fsi;
+	u32 page_index, page_off;
+	struct page *page;
+	size_t frag_desc_size = sizeof(struct ssdfs_fragment_desc);
+	size_t array_bytes = frag_desc_size * array_size;
+	size_t size = array_bytes;
+	size_t read_size = 0;
+	u32 buf_off = 0;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!array);
+
+	SSDFS_DBG("seg %llu, peb %llu, "
+		  "array_offset %u, array_size %zu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  array_offset, array_size);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+read_next_page:
+	page_off = array_offset % PAGE_SIZE;
+	read_size = min_t(size_t, size, PAGE_SIZE - page_off);
+
+	page_index = array_offset >> PAGE_SHIFT;
+	page = ssdfs_peb_read_page_locked(pebi, req, page_index);
+	if (IS_ERR_OR_NULL(page)) {
+		err = IS_ERR(page) ? PTR_ERR(page) : -ERANGE;
+		if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("cache hasn't page: index %u\n",
+				  page_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+		} else {
+			SSDFS_ERR("fail to read locked page: index %u\n",
+				  page_index);
+		}
+		return err;
+	}
+
+	err = ssdfs_memcpy_from_page(array, buf_off, array_bytes,
+				     page, page_off, PAGE_SIZE,
+				     read_size);
+
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to copy: "
+			  "page_off %u, buf_off %u, "
+			  "read_size %zu, size %zu, err %d\n",
+			  page_off, buf_off,
+			  read_size, array_bytes, err);
+		return err;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	size -= read_size;
+	buf_off += read_size;
+	array_offset += read_size;
+
+	if (size != 0)
+		goto read_next_page;
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_unaligned_read_fragment() - unaligned read fragment
+ * @pebi: pointer on PEB object
+ * @req: request
+ * @byte_off: offset in bytes from PEB's begin
+ * @size: size of fragment in bytes
+ * @buf: buffer pointer
+ *
+ * This function tries to read fragment.
+ *
+ * RETURN:
+ * [success] - fragment has been read successfully.
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_unaligned_read_fragment(struct ssdfs_peb_info *pebi,
+				      struct ssdfs_segment_request *req,
+				      u32 byte_off,
+				      size_t size,
+				      void *buf)
+{
+	u32 page_index, page_off;
+	struct page *page;
+	size_t read_size = 0;
+	u32 buf_off = 0;
+	size_t array_bytes = size;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(byte_off > pebi->pebc->parent_si->fsi->erasesize);
+	BUG_ON(size > PAGE_SIZE);
+	WARN_ON(size == 0);
+	BUG_ON(!buf);
+
+	SSDFS_DBG("seg %llu, peb %llu, "
+		  "offset %u, size %zu, buf %p\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  byte_off, size, buf);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+read_next_page:
+	if (byte_off > pebi->pebc->parent_si->fsi->erasesize) {
+		SSDFS_ERR("offset %u > erasesize %u\n",
+			  byte_off,
+			  pebi->pebc->parent_si->fsi->erasesize);
+		return -ERANGE;
+	}
+
+	page_off = byte_off % PAGE_SIZE;
+	read_size = min_t(size_t, size, PAGE_SIZE - page_off);
+
+	page_index = byte_off >> PAGE_SHIFT;
+	page = ssdfs_peb_read_page_locked(pebi, req, page_index);
+	if (IS_ERR_OR_NULL(page)) {
+		err = IS_ERR(page) ? PTR_ERR(page) : -ERANGE;
+		if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("cache hasn't page: page_off %u\n",
+				  page_off);
+#endif /* CONFIG_SSDFS_DEBUG */
+		} else {
+			SSDFS_ERR("fail to read locked page: index %u\n",
+				  page_off);
+		}
+		return err;
+	}
+
+	err = ssdfs_memcpy_from_page(buf, buf_off, array_bytes,
+				     page, page_off, PAGE_SIZE,
+				     read_size);
+
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to copy: "
+			  "page_off %u, buf_off %u, "
+			  "read_size %zu, size %zu, err %d\n",
+			  page_off, buf_off,
+			  read_size, array_bytes, err);
+		return err;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	size -= read_size;
+	buf_off += read_size;
+	byte_off += read_size;
+
+	if (size != 0)
+		goto read_next_page;
+
+	return 0;
+}
+
+/*
+ * ssdfs_read_checked_fragment() - read and check data fragment
+ * @pebi: pointer on PEB object
+ * @req: segment request
+ * @area_offset: offset in bytes from log's begin
+ * @sequence_id: fragment identification number
+ * @desc: fragment descriptor
+ * @cdata_buf: compressed data buffer
+ * @page: buffer for uncompressed data
+ *
+ * This function reads data fragment, uncompressed it
+ * (if neccessary) and check fragment's checksum.
+ *
+ * RETURN:
+ * [success] - fragment has been read successfully.
+ * [failure] - error code:
+ *
+ * %-EINVAL     - invalid input.
+ * %-ERANGE     - internal calculation error.
+ * %-EIO        - I/O error.
+ */
+static
+int ssdfs_read_checked_fragment(struct ssdfs_peb_info *pebi,
+				struct ssdfs_segment_request *req,
+				u32 area_offset,
+				int sequence_id,
+				struct ssdfs_fragment_desc *desc,
+				void *cdata_buf,
+				struct page *page)
+{
+	struct ssdfs_fs_info *fsi;
+	u32 pebsize;
+	u32 offset;
+	size_t compr_size, uncompr_size;
+	bool is_compressed;
+	void *kaddr;
+	__le32 checksum;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!desc || !cdata_buf || !page);
+
+	SSDFS_DBG("seg %llu, peb %llu, area_offset %u, sequence_id %u, "
+		  "offset %u, compr_size %u, uncompr_size %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  area_offset,
+		  le16_to_cpu(desc->sequence_id),
+		  le32_to_cpu(desc->offset),
+		  le16_to_cpu(desc->compr_size),
+		  le16_to_cpu(desc->uncompr_size));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	if (sequence_id != le16_to_cpu(desc->sequence_id)) {
+		SSDFS_ERR("sequence_id %d != desc->sequence_id %u\n",
+			  sequence_id, le16_to_cpu(desc->sequence_id));
+		return -EINVAL;
+	}
+
+	pebsize = fsi->pages_per_peb * fsi->pagesize;
+	offset = area_offset + le32_to_cpu(desc->offset);
+	compr_size = le16_to_cpu(desc->compr_size);
+	uncompr_size = le16_to_cpu(desc->uncompr_size);
+
+	if (offset >= pebsize) {
+		ssdfs_fs_error(fsi->sb, __FILE__, __func__, __LINE__,
+				"desc->offset %u >= pebsize %u\n",
+				offset, pebsize);
+		return -EIO;
+	}
+
+	if (uncompr_size > PAGE_SIZE) {
+		SSDFS_ERR("uncompr_size %zu > PAGE_SIZE %lu\n",
+			  uncompr_size, PAGE_SIZE);
+		return -ERANGE;
+	}
+
+	is_compressed = (desc->type == SSDFS_FRAGMENT_ZLIB_BLOB ||
+			 desc->type == SSDFS_FRAGMENT_LZO_BLOB);
+
+	if (desc->type == SSDFS_FRAGMENT_UNCOMPR_BLOB) {
+		if (compr_size != uncompr_size) {
+			ssdfs_fs_error(fsi->sb, __FILE__, __func__, __LINE__,
+					"compr_size %zu != uncompr_size %zu\n",
+					compr_size, uncompr_size);
+			return -EIO;
+		}
+
+		if (uncompr_size > PAGE_SIZE) {
+			ssdfs_fs_error(fsi->sb, __FILE__, __func__, __LINE__,
+					"uncompr_size %zu > PAGE_CACHE %lu\n",
+					uncompr_size, PAGE_SIZE);
+			return -EIO;
+		}
+
+		kaddr = kmap_local_page(page);
+		err = ssdfs_peb_unaligned_read_fragment(pebi, req, offset,
+							uncompr_size,
+							kaddr);
+		if (!err)
+			checksum = ssdfs_crc32_le(kaddr, uncompr_size);
+		flush_dcache_page(page);
+		kunmap_local(kaddr);
+
+		if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("cache hasn't requested page: "
+				  "seg %llu, peb %llu, offset %u, size %zu\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  offset, uncompr_size);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to read fragment: "
+				  "seg %llu, peb %llu, offset %u, size %zu, "
+				  "err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  offset, uncompr_size, err);
+			return err;
+		}
+	} else if (is_compressed) {
+		int type;
+
+		err = ssdfs_peb_unaligned_read_fragment(pebi, req, offset,
+							compr_size,
+							cdata_buf);
+		if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("cache hasn't requested page: "
+				  "seg %llu, peb %llu, offset %u, size %zu\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  offset, uncompr_size);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to read fragment: "
+				  "seg %llu, peb %llu, offset %u, size %zu, "
+				  "err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  offset, compr_size, err);
+			return err;
+		}
+
+		if (desc->type == SSDFS_FRAGMENT_ZLIB_BLOB)
+			type = SSDFS_COMPR_ZLIB;
+		else if (desc->type == SSDFS_FRAGMENT_LZO_BLOB)
+			type = SSDFS_COMPR_LZO;
+		else
+			BUG();
+
+		kaddr = kmap_local_page(page);
+		err = ssdfs_decompress(type, cdata_buf, kaddr,
+					compr_size, uncompr_size);
+		if (!err)
+			checksum = ssdfs_crc32_le(kaddr, uncompr_size);
+		flush_dcache_page(page);
+		kunmap_local(kaddr);
+
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to decompress fragment: "
+				  "seg %llu, peb %llu, offset %u, "
+				  "compr_size %zu, uncompr_size %zu"
+				  "err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  offset, compr_size, uncompr_size, err);
+			return err;
+		}
+	} else
+		BUG();
+
+	if (desc->checksum != checksum) {
+		ssdfs_fs_error(fsi->sb, __FILE__, __func__, __LINE__,
+				"desc->checksum %#x != checksum %#x\n",
+				le32_to_cpu(desc->checksum),
+				le32_to_cpu(checksum));
+		return -EIO;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_read_main_area_page() - read main area's page
+ * @pebi: pointer on PEB object
+ * @req: request
+ * @array: array of area's descriptors
+ * @array_size: count of items into array
+ * @blk_state_off: block state offset
+ *
+ * This function tries to read main area's page.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_read_main_area_page(struct ssdfs_peb_info *pebi,
+				  struct ssdfs_segment_request *req,
+				  struct ssdfs_metadata_descriptor *array,
+				  size_t array_size,
+				  struct ssdfs_blk_state_offset *blk_state_off)
+{
+	struct ssdfs_fs_info *fsi;
+	u8 area_index;
+	u32 area_offset;
+	u32 data_bytes;
+	u32 read_bytes;
+	u32 byte_offset;
+	int page_index;
+	struct page *page;
+	void *kaddr;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!req || !array || !blk_state_off);
+
+	SSDFS_DBG("seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	area_index = SSDFS_AREA_TYPE2INDEX(blk_state_off->log_area);
+	if (area_index >= array_size) {
+		SSDFS_ERR("area_index %u >= array_size %zu\n",
+			  area_index, array_size);
+		return -EIO;
+	}
+
+	read_bytes = req->result.processed_blks * fsi->pagesize;
+
+	if (read_bytes > req->extent.data_bytes) {
+		SSDFS_ERR("read_bytes %u > req->extent.data_bytes %u\n",
+			  read_bytes, req->extent.data_bytes);
+		return -ERANGE;
+	} else if (read_bytes == req->extent.data_bytes) {
+		SSDFS_WARN("read_bytes %u == req->extent.data_bytes %u\n",
+			   read_bytes, req->extent.data_bytes);
+		return -ERANGE;
+	}
+
+	data_bytes = req->extent.data_bytes - read_bytes;
+
+	if (fsi->pagesize > PAGE_SIZE)
+		data_bytes = min_t(u32, data_bytes, fsi->pagesize);
+	else
+		data_bytes = min_t(u32, data_bytes, PAGE_SIZE);
+
+	area_offset = le32_to_cpu(array[area_index].offset);
+	byte_offset = le32_to_cpu(blk_state_off->byte_offset);
+
+	page_index = (int)(read_bytes >> PAGE_SHIFT);
+	BUG_ON(page_index >= U16_MAX);
+
+	if (req->private.flags & SSDFS_REQ_PREPARE_DIFF) {
+		if (pagevec_count(&req->result.old_state) <= page_index) {
+			SSDFS_ERR("page_index %d >= pagevec_count %u\n",
+				  page_index,
+				  pagevec_count(&req->result.old_state));
+			return -EIO;
+		}
+
+		page = req->result.old_state.pages[page_index];
+	} else {
+		if (pagevec_count(&req->result.pvec) <= page_index) {
+			SSDFS_ERR("page_index %d >= pagevec_count %u\n",
+				  page_index,
+				  pagevec_count(&req->result.pvec));
+			return -EIO;
+		}
+
+		page = req->result.pvec.pages[page_index];
+	}
+
+	kaddr = kmap_local_page(page);
+	err = ssdfs_peb_unaligned_read_fragment(pebi, req,
+						area_offset + byte_offset,
+						data_bytes,
+						kaddr);
+	flush_dcache_page(page);
+	kunmap_local(kaddr);
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read page: "
+			  "seg %llu, peb %llu, offset %u, size %u, "
+			  "err %d\n",
+			  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			  area_offset + byte_offset, data_bytes, err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_read_area_fragment() - read area's fragment
+ * @pebi: pointer on PEB object
+ * @req: request
+ * @array: array of area's descriptors
+ * @array_size: count of items into array
+ * @blk_state_off: block state offset
+ *
+ * This function tries to read area's fragment.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_read_area_fragment(struct ssdfs_peb_info *pebi,
+				 struct ssdfs_segment_request *req,
+				 struct ssdfs_metadata_descriptor *array,
+				 size_t array_size,
+				 struct ssdfs_blk_state_offset *blk_state_off)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_block_state_descriptor found_blk_state;
+	size_t state_desc_size = sizeof(struct ssdfs_block_state_descriptor);
+	struct ssdfs_fragment_desc *frag_descs = NULL;
+	size_t frag_desc_size = sizeof(struct ssdfs_fragment_desc);
+	void *cdata_buf = NULL;
+	u8 area_index;
+	u32 area_offset;
+	u32 frag_desc_offset;
+	u32 full_offset;
+	u32 data_bytes;
+	u32 read_bytes;
+	int page_index;
+	u16 fragments;
+	u32 uncompr_bytes;
+	int i;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!req || !array || !blk_state_off);
+
+	SSDFS_DBG("seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	area_index = SSDFS_AREA_TYPE2INDEX(blk_state_off->log_area);
+	if (area_index >= array_size) {
+		SSDFS_ERR("area_index %u >= array_size %zu\n",
+			  area_index, array_size);
+		return -EIO;
+	}
+
+	read_bytes = req->result.processed_blks * fsi->pagesize;
+
+	if (read_bytes > req->extent.data_bytes) {
+		SSDFS_ERR("read_bytes %u > req->extent.data_bytes %u\n",
+			  read_bytes, req->extent.data_bytes);
+		return -ERANGE;
+	} else if (read_bytes == req->extent.data_bytes) {
+		SSDFS_WARN("read_bytes %u == req->extent.data_bytes %u\n",
+			   read_bytes, req->extent.data_bytes);
+		return -ERANGE;
+	}
+
+	data_bytes = req->extent.data_bytes - read_bytes;
+
+	if (fsi->pagesize > PAGE_SIZE)
+		data_bytes = min_t(u32, data_bytes, fsi->pagesize);
+	else
+		data_bytes = min_t(u32, data_bytes, PAGE_SIZE);
+
+	err = ssdfs_peb_get_block_state_desc(pebi, req, &array[area_index],
+					     &found_blk_state);
+	if (err == -ENOENT) {
+		SSDFS_DBG("cache hasn't requested page\n");
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to get block state descriptor: "
+			  "area_offset %u, err %d\n",
+			  le32_to_cpu(array[area_index].offset),
+			  err);
+		return err;
+	}
+
+	uncompr_bytes = le32_to_cpu(found_blk_state.chain_hdr.uncompr_bytes);
+	if (data_bytes > uncompr_bytes) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("data_bytes %u > uncompr_bytes %u\n",
+			  data_bytes, uncompr_bytes);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		req->extent.data_bytes -= data_bytes - uncompr_bytes;
+		data_bytes = uncompr_bytes;
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("CORRECTED VALUE: data_bytes %u\n",
+			  data_bytes);
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+	fragments = le16_to_cpu(found_blk_state.chain_hdr.fragments_count);
+	if (fragments == 0 || fragments > SSDFS_FRAGMENTS_CHAIN_MAX) {
+		SSDFS_ERR("invalid fragments count %u\n", fragments);
+		return -EIO;
+	}
+
+	frag_descs = ssdfs_read_kcalloc(fragments, frag_desc_size, GFP_KERNEL);
+	if (!frag_descs) {
+		SSDFS_ERR("fail to allocate fragment descriptors array\n");
+		return -ENOMEM;
+	}
+
+	area_offset = le32_to_cpu(array[area_index].offset);
+	frag_desc_offset = le32_to_cpu(blk_state_off->byte_offset);
+	frag_desc_offset += state_desc_size;
+	full_offset = area_offset + frag_desc_offset;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("area_offset %u, blk_state_off->byte_offset %u, "
+		  "state_desc_size %zu, frag_desc_offset %u, "
+		  "full_offset %u\n",
+		  area_offset, le32_to_cpu(blk_state_off->byte_offset),
+		  state_desc_size, frag_desc_offset, full_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_get_fragment_desc_array(pebi, req, full_offset,
+						frag_descs, fragments);
+	if (err == -ENOENT) {
+		SSDFS_DBG("cache hasn't requested page\n");
+		goto free_bufs;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to get fragment descriptor array: "
+			  "offset %u, fragments %u, err %d\n",
+			  full_offset, fragments, err);
+		goto free_bufs;
+	}
+
+	cdata_buf = ssdfs_read_kzalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!cdata_buf) {
+		err = -ENOMEM;
+		SSDFS_ERR("fail to allocate cdata_buf\n");
+		goto free_bufs;
+	}
+
+	page_index = (int)(read_bytes >> PAGE_SHIFT);
+	BUG_ON(page_index >= U16_MAX);
+
+	for (i = 0; i < fragments; i++) {
+		struct pagevec *pvec;
+		struct page *page;
+		struct ssdfs_fragment_desc *cur_desc;
+		u32 compr_size;
+
+		if (req->private.flags & SSDFS_REQ_PREPARE_DIFF) {
+			pvec = &req->result.old_state;
+
+			if (pagevec_count(pvec) <= i) {
+				err = -EIO;
+				SSDFS_ERR("page_index %d >= pagevec_count %u\n",
+					  i, pagevec_count(pvec));
+				goto free_bufs;
+			}
+		} else {
+			pvec = &req->result.pvec;
+
+			if (pagevec_count(pvec) <= (page_index + i)) {
+				err = -EIO;
+				SSDFS_ERR("page_index %d >= pagevec_count %u\n",
+					  page_index + i,
+					  pagevec_count(pvec));
+				goto free_bufs;
+			}
+		}
+
+		cur_desc = &frag_descs[i];
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("FRAGMENT DESC DUMP: index %d\n", i);
+		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
+				     cur_desc,
+				     sizeof(struct ssdfs_fragment_desc));
+		SSDFS_DBG("\n");
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (cur_desc->magic != SSDFS_FRAGMENT_DESC_MAGIC) {
+			err = -EIO;
+			SSDFS_ERR("invalid fragment descriptor magic\n");
+			goto free_bufs;
+		}
+
+		if (cur_desc->type < SSDFS_FRAGMENT_UNCOMPR_BLOB ||
+		    cur_desc->type > SSDFS_FRAGMENT_LZO_BLOB) {
+			err = -EIO;
+			SSDFS_ERR("invalid fragment descriptor type\n");
+			goto free_bufs;
+		}
+
+		if (cur_desc->sequence_id != i) {
+			err = -EIO;
+			SSDFS_ERR("invalid fragment's sequence id\n");
+			goto free_bufs;
+		}
+
+		compr_size = le16_to_cpu(cur_desc->compr_size);
+
+		if (compr_size > PAGE_SIZE) {
+			err = -EIO;
+			SSDFS_ERR("compr_size %u > PAGE_SIZE %lu\n",
+				  compr_size, PAGE_SIZE);
+			goto free_bufs;
+		}
+
+		if (req->private.flags & SSDFS_REQ_PREPARE_DIFF)
+			page = pvec->pages[i];
+		else
+			page = pvec->pages[page_index + i];
+
+		err = ssdfs_read_checked_fragment(pebi, req, area_offset,
+						  i, cur_desc,
+						  cdata_buf,
+						  page);
+		if (err == -ENOENT) {
+			SSDFS_DBG("cache hasn't requested page\n");
+			goto free_bufs;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to read fragment: "
+				  "index %d, err %d\n",
+				  i, err);
+			goto free_bufs;
+		}
+	}
+
+free_bufs:
+	ssdfs_read_kfree(frag_descs);
+	ssdfs_read_kfree(cdata_buf);
+
+	return err;
+}
+
+/*
+ * ssdfs_peb_read_base_block_state() - read base state of block
+ * @pebi: pointer on PEB object
+ * @req: request
+ * @array: array of area's descriptors
+ * @array_size: count of items into array
+ * @offset: block state offset
+ *
+ * This function tries to extract a base state of block.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ * %-ENOENT     - cache hasn't requested page.
+ */
+static
+int ssdfs_peb_read_base_block_state(struct ssdfs_peb_info *pebi,
+				    struct ssdfs_segment_request *req,
+				    struct ssdfs_metadata_descriptor *array,
+				    size_t array_size,
+				    struct ssdfs_blk_state_offset *offset)
+{
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!req || !array || !offset);
+	BUG_ON(array_size != SSDFS_SEG_HDR_DESC_MAX);
+
+	SSDFS_DBG("seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_read_log_hdr_desc_array(pebi, req,
+					le16_to_cpu(offset->log_start_page),
+					array, array_size);
+	if (err == -ENOENT) {
+		SSDFS_DBG("cache hasn't requested page\n");
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to read log's header desc array: "
+			  "seg %llu, peb %llu, log_start_page %u, err %d\n",
+			  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			  le16_to_cpu(offset->log_start_page),
+			  err);
+		return err;
+	}
+
+	if (offset->log_area == SSDFS_LOG_MAIN_AREA) {
+		err = ssdfs_peb_read_main_area_page(pebi, req,
+						    array, array_size,
+						    offset);
+		if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("cache hasn't requested page: "
+				  "seg %llu, peb %llu, "
+				  "ino %llu, logical_offset %llu\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  req->extent.ino,
+				  req->extent.logical_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to read main area's page: "
+				  "seg %llu, peb %llu, "
+				  "ino %llu, logical_offset %llu, "
+				  "err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  req->extent.ino,
+				  req->extent.logical_offset,
+				  err);
+			return err;
+		}
+	} else {
+		err = ssdfs_peb_read_area_fragment(pebi, req,
+						   array, array_size,
+						   offset);
+		if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("cache hasn't requested page: "
+				  "seg %llu, peb %llu, "
+				  "ino %llu, logical_offset %llu\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  req->extent.ino,
+				  req->extent.logical_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to read area's fragment: "
+				  "seg %llu, peb %llu, "
+				  "ino %llu, logical_offset %llu, "
+				  "err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  req->extent.ino,
+				  req->extent.logical_offset,
+				  err);
+			return err;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_read_area_diff_fragment() - read diff fragment
+ * @pebi: pointer on PEB object
+ * @req: request
+ * @array: array of area's descriptors
+ * @array_size: count of items into array
+ * @blk_state_off: block state offset
+ * @page: page with current diff blob
+ * @sequence_id: sequence ID of the fragment
+ *
+ * This function tries to extract a diff blob into @page.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_read_area_diff_fragment(struct ssdfs_peb_info *pebi,
+				 struct ssdfs_segment_request *req,
+				 struct ssdfs_metadata_descriptor *array,
+				 size_t array_size,
+				 struct ssdfs_blk_state_offset *blk_state_off,
+				 struct page *page,
+				 int sequence_id)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_block_state_descriptor found_blk_state;
+	size_t state_desc_size = sizeof(struct ssdfs_block_state_descriptor);
+	struct ssdfs_fragment_desc frag_desc = {0};
+	void *cdata_buf = NULL;
+	u8 area_index;
+	u32 area_offset;
+	u32 frag_desc_offset;
+	u32 full_offset;
+	u16 fragments;
+	u64 cno;
+	u64 parent_snapshot;
+	u32 compr_size;
+#ifdef CONFIG_SSDFS_DEBUG
+	void *kaddr;
+#endif /* CONFIG_SSDFS_DEBUG */
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!array || !blk_state_off || !page);
+
+	SSDFS_DBG("seg %llu, peb %llu, sequence_id %d\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, sequence_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	err = ssdfs_peb_read_log_hdr_desc_array(pebi, req,
+				le16_to_cpu(blk_state_off->log_start_page),
+				array, array_size);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read log's header desc array: "
+			  "seg %llu, peb %llu, log_start_page %u, err %d\n",
+			  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			  le16_to_cpu(blk_state_off->log_start_page),
+			  err);
+		return err;
+	}
+
+	area_index = SSDFS_AREA_TYPE2INDEX(blk_state_off->log_area);
+	if (area_index >= array_size) {
+		SSDFS_ERR("area_index %u >= array_size %zu\n",
+			  area_index, array_size);
+		return -EIO;
+	}
+
+	err = __ssdfs_peb_get_block_state_desc(pebi, req, &array[area_index],
+						&found_blk_state,
+						&cno, &parent_snapshot);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to get block state descriptor: "
+			  "area_offset %u, err %d\n",
+			  le32_to_cpu(array[area_index].offset),
+			  err);
+		return err;
+	}
+
+	fragments = le16_to_cpu(found_blk_state.chain_hdr.fragments_count);
+	if (fragments == 0 || fragments > 1) {
+		SSDFS_ERR("invalid fragments count %u\n", fragments);
+		return -EIO;
+	}
+
+	area_offset = le32_to_cpu(array[area_index].offset);
+	frag_desc_offset = le32_to_cpu(blk_state_off->byte_offset);
+	frag_desc_offset += state_desc_size;
+	full_offset = area_offset + frag_desc_offset;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("area_offset %u, blk_state_off->byte_offset %u, "
+		  "state_desc_size %zu, frag_desc_offset %u, "
+		  "full_offset %u\n",
+		  area_offset, le32_to_cpu(blk_state_off->byte_offset),
+		  state_desc_size, frag_desc_offset, full_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_get_fragment_desc_array(pebi, req, full_offset,
+						&frag_desc, 1);
+	if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("cache hasn't requested page: "
+			  "seg %llu, peb %llu\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to get fragment descriptor array: "
+			  "offset %u, fragments %u, err %d\n",
+			  full_offset, fragments, err);
+		return err;
+	}
+
+	cdata_buf = ssdfs_read_kzalloc(PAGE_SIZE, GFP_KERNEL);
+	if (!cdata_buf) {
+		SSDFS_ERR("fail to allocate cdata_buf\n");
+		return -ENOMEM;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("FRAGMENT DESC DUMP: index %d\n", sequence_id);
+	print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
+			     &frag_desc,
+			     sizeof(struct ssdfs_fragment_desc));
+	SSDFS_DBG("\n");
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (frag_desc.magic != SSDFS_FRAGMENT_DESC_MAGIC) {
+		err = -EIO;
+		SSDFS_ERR("invalid fragment descriptor magic\n");
+		goto free_bufs;
+	}
+
+	if (frag_desc.type < SSDFS_FRAGMENT_UNCOMPR_BLOB ||
+	    frag_desc.type > SSDFS_FRAGMENT_LZO_BLOB) {
+		err = -EIO;
+		SSDFS_ERR("invalid fragment descriptor type\n");
+		goto free_bufs;
+	}
+
+	compr_size = le16_to_cpu(frag_desc.compr_size);
+
+	if (compr_size > PAGE_SIZE) {
+		err = -EIO;
+		SSDFS_ERR("compr_size %u > PAGE_SIZE %lu\n",
+			  compr_size, PAGE_SIZE);
+		goto free_bufs;
+	}
+
+	err = ssdfs_read_checked_fragment(pebi, req, area_offset,
+					  0, &frag_desc,
+					  cdata_buf,
+					  page);
+	if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("cache hasn't requested page: "
+			  "seg %llu, peb %llu\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+		goto free_bufs;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to read fragment: "
+			  "index %d, err %d\n",
+			  sequence_id, err);
+		goto free_bufs;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	kaddr = kmap_local_page(page);
+	SSDFS_DBG("DIFF DUMP: index %d\n",
+		  sequence_id);
+	print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
+			     kaddr,
+			     PAGE_SIZE);
+	SSDFS_DBG("\n");
+	kunmap_local(kaddr);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+free_bufs:
+	if (cdata_buf)
+		ssdfs_read_kfree(cdata_buf);
+
+	return err;
+}
+
+/*
+ * ssdfs_peb_read_diff_block_state() - read diff blob
+ * @pebi: pointer on PEB object
+ * @req: request
+ * @array: array of area's descriptors
+ * @array_size: count of items into array
+ * @offset: block state offset
+ *
+ * This function tries to extract a diff blob.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+static
+int ssdfs_peb_read_diff_block_state(struct ssdfs_peb_info *pebi,
+				    struct ssdfs_segment_request *req,
+				    struct ssdfs_metadata_descriptor *array,
+				    size_t array_size,
+				    struct ssdfs_blk_state_offset *offset)
+{
+	struct page *page = NULL;
+	int sequence_id;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->parent_si);
+	BUG_ON(!pebi->pebc->parent_si->fsi);
+	BUG_ON(!array || !offset);
+	BUG_ON(array_size != SSDFS_SEG_HDR_DESC_MAX);
+
+	SSDFS_DBG("seg %llu, peb %llu, pagevec_size %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pagevec_count(&req->result.diffs));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	page = ssdfs_request_allocate_and_add_diff_page(req);
+	if (unlikely(IS_ERR_OR_NULL(page))) {
+		err = !page ? -ENOMEM : PTR_ERR(page);
+		SSDFS_ERR("fail to add pagevec page: err %d\n",
+			  err);
+		return err;
+	}
+
+	ssdfs_lock_page(page);
+
+	sequence_id = pagevec_count(&req->result.diffs) - 1;
+	err = ssdfs_peb_read_area_diff_fragment(pebi, req, array, array_size,
+						offset, page, sequence_id);
+	if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("cache hasn't requested page: "
+			  "seg %llu, peb %llu\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to read area's fragment: "
+			  "seg %llu, peb %llu, "
+			  "err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id, err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_blk_desc_buffer_init() - init block descriptor buffer
+ * @pebc: pointer on PEB container
+ * @req: request
+ * @desc_off: block descriptor offset
+ * @pos: offset position
+ * @array: array of area's descriptors
+ * @array_size: count of items into array
+ *
+ * This function tries to init block descriptor buffer.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+int ssdfs_blk_desc_buffer_init(struct ssdfs_peb_container *pebc,
+				struct ssdfs_segment_request *req,
+				struct ssdfs_phys_offset_descriptor *desc_off,
+				struct ssdfs_offset_position *pos,
+				struct ssdfs_metadata_descriptor *array,
+				size_t array_size)
+{
+	struct ssdfs_peb_info *pebi = NULL;
+	struct ssdfs_blk2off_table *table;
+	u8 peb_migration_id;
+	u16 logical_blk;
+#ifdef CONFIG_SSDFS_DEBUG
+	struct ssdfs_blk_state_offset *state_off;
+	int j;
+#endif /* CONFIG_SSDFS_DEBUG */
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+	BUG_ON(!desc_off || !pos);
+
+	SSDFS_DBG("seg %llu, peb_index %u, blk_desc.status %#x\n",
+		  pebc->parent_si->seg_id, pebc->peb_index,
+		  pos->blk_desc.status);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	switch (pos->blk_desc.status) {
+	case SSDFS_BLK_DESC_BUF_UNKNOWN_STATE:
+	case SSDFS_BLK_DESC_BUF_ALLOCATED:
+		peb_migration_id = desc_off->blk_state.peb_migration_id;
+
+		pebi = ssdfs_get_peb_for_migration_id(pebc, peb_migration_id);
+		if (IS_ERR_OR_NULL(pebi)) {
+			err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+			SSDFS_ERR("fail to get PEB object: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err);
+			goto finish_blk_desc_buffer_init;
+		}
+
+		err = ssdfs_peb_find_block_descriptor(pebi, req,
+						      array, array_size,
+						      desc_off,
+						      &pos->blk_desc.buf);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to find block descriptor: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err);
+			goto finish_blk_desc_buffer_init;
+		}
+
+		pos->blk_desc.status = SSDFS_BLK_DESC_BUF_INITIALIZED;
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("status %#x, ino %llu, "
+			  "logical_offset %u, peb_index %u, peb_page %u\n",
+			  pos->blk_desc.status,
+			  le64_to_cpu(pos->blk_desc.buf.ino),
+			  le32_to_cpu(pos->blk_desc.buf.logical_offset),
+			  le16_to_cpu(pos->blk_desc.buf.peb_index),
+			  le16_to_cpu(pos->blk_desc.buf.peb_page));
+
+		for (j = 0; j < SSDFS_BLK_STATE_OFF_MAX; j++) {
+			state_off = &pos->blk_desc.buf.state[j];
+
+			SSDFS_DBG("BLK STATE OFFSET %d: "
+				  "log_start_page %u, log_area %#x, "
+				  "byte_offset %u, peb_migration_id %u\n",
+				  j,
+				  le16_to_cpu(state_off->log_start_page),
+				  state_off->log_area,
+				  le32_to_cpu(state_off->byte_offset),
+				  state_off->peb_migration_id);
+		}
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		table = pebi->pebc->parent_si->blk2off_table;
+		logical_blk = req->place.start.blk_index +
+					req->result.processed_blks;
+
+		err = ssdfs_blk2off_table_blk_desc_init(table, logical_blk,
+							pos);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to init blk desc: "
+				  "logical_blk %u, err %d\n",
+				  logical_blk, err);
+			goto finish_blk_desc_buffer_init;
+		}
+		break;
+
+	case SSDFS_BLK_DESC_BUF_INITIALIZED:
+		/* do nothing */
+		SSDFS_DBG("descriptor buffer is initialized already\n");
+		break;
+
+	default:
+		SSDFS_ERR("pos->blk_desc.status %#x\n",
+			  pos->blk_desc.status);
+		BUG();
+	}
+
+finish_blk_desc_buffer_init:
+	return err;
+}
+
+/*
+ * ssdfs_peb_read_block_state() - read state of the block
+ * @pebc: pointer on PEB container
+ * @req: request
+ * @desc_off: block descriptor offset
+ * @pos: offset position
+ * @array: array of area's descriptors
+ * @array_size: count of items into array
+ *
+ * This function tries to read block state.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EIO        - I/O error.
+ * %-ERANGE     - internal error.
+ * %-ENOMEM     - fail to allocate memory.
+ */
+int ssdfs_peb_read_block_state(struct ssdfs_peb_container *pebc,
+				struct ssdfs_segment_request *req,
+				struct ssdfs_phys_offset_descriptor *desc_off,
+				struct ssdfs_offset_position *pos,
+				struct ssdfs_metadata_descriptor *array,
+				size_t array_size)
+{
+	struct ssdfs_peb_info *pebi = NULL;
+	struct ssdfs_blk_state_offset *offset = NULL;
+	u64 ino;
+	u32 logical_offset;
+	int i;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+	BUG_ON(!req || !array);
+	BUG_ON(array_size != SSDFS_SEG_HDR_DESC_MAX);
+
+	SSDFS_DBG("seg %llu, peb_index %u, processed_blks %d\n",
+		  pebc->parent_si->seg_id, pebc->peb_index,
+		  req->result.processed_blks);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_blk_desc_buffer_init(pebc, req, desc_off, pos,
+					 array, array_size);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to init blk desc buffer: err %d\n",
+			  err);
+		goto finish_prepare_pvec;
+	}
+
+	ino = le64_to_cpu(pos->blk_desc.buf.ino);
+	logical_offset = le32_to_cpu(pos->blk_desc.buf.logical_offset);
+
+	offset = &pos->blk_desc.buf.state[0];
+
+	if (IS_SSDFS_BLK_STATE_OFFSET_INVALID(offset)) {
+		err = -ERANGE;
+		SSDFS_ERR("block state offset invalid\n");
+		SSDFS_ERR("log_start_page %u, log_area %u, "
+			  "peb_migration_id %u, byte_offset %u\n",
+			  le16_to_cpu(offset->log_start_page),
+			  offset->log_area,
+			  offset->peb_migration_id,
+			  le32_to_cpu(offset->byte_offset));
+		goto finish_prepare_pvec;
+	}
+
+	pebi = ssdfs_get_peb_for_migration_id(pebc, offset->peb_migration_id);
+	if (IS_ERR_OR_NULL(pebi)) {
+		err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+		SSDFS_ERR("fail to get PEB object: "
+			  "seg %llu, peb_index %u, err %d\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index, err);
+		goto finish_prepare_pvec;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	DEBUG_BLOCK_DESCRIPTOR(pebi->pebc->parent_si->seg_id,
+				pebi->peb_id, &pos->blk_desc.buf);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_read_base_block_state(pebi, req,
+					      array, array_size,
+					      offset);
+	if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("unable to read block state: "
+			  "seg %llu, peb_index %u, ino %llu, "
+			  "logical_offset %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index,
+			  ino, logical_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+		goto finish_prepare_pvec;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to read block state: "
+			  "seg %llu, peb_index %u, ino %llu, "
+			  "logical_offset %u, err %d\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index,
+			  ino, logical_offset,
+			  err);
+		goto finish_prepare_pvec;
+	}
+
+	for (i = 0; i < SSDFS_BLK_STATE_OFF_MAX; i++) {
+		offset = &pos->blk_desc.buf.state[i];
+
+		if (i == 0) {
+			/*
+			 * base block state has been read already
+			 */
+			continue;
+		} else {
+			if (IS_SSDFS_BLK_STATE_OFFSET_INVALID(offset))
+				goto finish_prepare_pvec;
+
+			pebi = ssdfs_get_peb_for_migration_id(pebc,
+						offset->peb_migration_id);
+			if (IS_ERR_OR_NULL(pebi)) {
+				err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+				SSDFS_ERR("fail to get PEB object: "
+					  "seg %llu, peb_index %u, err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				goto finish_prepare_pvec;
+			}
+
+			err = ssdfs_peb_read_diff_block_state(pebi,
+							      req,
+							      array,
+							      array_size,
+							      offset);
+		}
+
+		if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("cache hasn't requested page: "
+				  "seg %llu, peb_index %u, ino %llu, "
+				  "logical_offset %u\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index,
+				  ino, logical_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+			goto finish_prepare_pvec;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to read block state: "
+				  "seg %llu, peb_index %u, ino %llu, "
+				  "logical_offset %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index,
+				  ino, logical_offset, err);
+			goto finish_prepare_pvec;
+		}
+	}
+
+finish_prepare_pvec:
+	if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("unable to read the block state: "
+			  "seg %llu, peb_index %u, ino %llu, "
+			  "logical_offset %u, peb_index %u, "
+			  "peb_page %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index,
+			  le64_to_cpu(pos->blk_desc.buf.ino),
+			  le32_to_cpu(pos->blk_desc.buf.logical_offset),
+			  le16_to_cpu(pos->blk_desc.buf.peb_index),
+			  le16_to_cpu(pos->blk_desc.buf.peb_page));
+#endif /* CONFIG_SSDFS_DEBUG */
+		goto finish_read_block_state;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to read the block state: "
+			  "seg %llu, peb_index %u, ino %llu, "
+			  "logical_offset %u, peb_index %u, "
+			  "peb_page %u, err %d\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index,
+			  le64_to_cpu(pos->blk_desc.buf.ino),
+			  le32_to_cpu(pos->blk_desc.buf.logical_offset),
+			  le16_to_cpu(pos->blk_desc.buf.peb_index),
+			  le16_to_cpu(pos->blk_desc.buf.peb_page),
+			  err);
+		goto finish_read_block_state;
+	}
+
+	if (pagevec_count(&req->result.diffs) == 0) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("diffs pagevec is empty: "
+			  "seg %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+		goto finish_read_block_state;
+	}
+
+	switch (pebi->pebc->peb_type) {
+	case SSDFS_MAPTBL_DATA_PEB_TYPE:
+		err = ssdfs_user_data_apply_diffs(pebi, req);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to apply diffs on base state: "
+				  "seg %llu, peb_index %u, ino %llu, "
+				  "logical_offset %u, peb_index %u, "
+				  "peb_page %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index,
+				  le64_to_cpu(pos->blk_desc.buf.ino),
+				  le32_to_cpu(pos->blk_desc.buf.logical_offset),
+				  le16_to_cpu(pos->blk_desc.buf.peb_index),
+				  le16_to_cpu(pos->blk_desc.buf.peb_page),
+				  err);
+			goto finish_read_block_state;
+		}
+		break;
+
+	case SSDFS_MAPTBL_LNODE_PEB_TYPE:
+	case SSDFS_MAPTBL_HNODE_PEB_TYPE:
+	case SSDFS_MAPTBL_IDXNODE_PEB_TYPE:
+		err = ssdfs_btree_node_apply_diffs(pebi, req);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to apply diffs on base state: "
+				  "seg %llu, peb_index %u, ino %llu, "
+				  "logical_offset %u, peb_index %u, "
+				  "peb_page %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index,
+				  le64_to_cpu(pos->blk_desc.buf.ino),
+				  le32_to_cpu(pos->blk_desc.buf.logical_offset),
+				  le16_to_cpu(pos->blk_desc.buf.peb_index),
+				  le16_to_cpu(pos->blk_desc.buf.peb_page),
+				  err);
+			goto finish_read_block_state;
+		}
+		break;
+
+	default:
+		err = -EOPNOTSUPP;
+		SSDFS_ERR("diff-on-write is not supported: "
+			  "seg %llu, peb_index %u, peb_type %#x, ino %llu, "
+			  "logical_offset %u, peb_index %u, "
+			  "peb_page %u, err %d\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index,
+			  pebi->pebc->peb_type,
+			  le64_to_cpu(pos->blk_desc.buf.ino),
+			  le32_to_cpu(pos->blk_desc.buf.logical_offset),
+			  le16_to_cpu(pos->blk_desc.buf.peb_index),
+			  le16_to_cpu(pos->blk_desc.buf.peb_page),
+			  err);
+		goto finish_read_block_state;
+	}
+
+finish_read_block_state:
+	if (!err && !(req->private.flags & SSDFS_REQ_PREPARE_DIFF))
+		req->result.processed_blks++;
+
+	if (err)
+		ssdfs_request_unlock_and_remove_old_state(req);
+
+	ssdfs_request_unlock_and_remove_diffs(req);
+
+	return err;
+}
+
+/*
+ * ssdfs_peb_read_page() - read page from PEB
+ * @pebc: pointer on PEB container
+ * @req: request [in|out]
+ * @end: pointer on waiting queue [out]
+ *
+ * This function tries to read PEB's page.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EAGAIN     - PEB object is not initialized yet.
+ */
+int ssdfs_peb_read_page(struct ssdfs_peb_container *pebc,
+			struct ssdfs_segment_request *req,
+			struct completion **end)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_blk2off_table *table;
+	struct ssdfs_phys_offset_descriptor *desc_off = NULL;
+	struct ssdfs_blk_state_offset *blk_state = NULL;
+	u16 logical_blk;
+	u16 log_start_page;
+	struct ssdfs_metadata_descriptor desc_array[SSDFS_SEG_HDR_DESC_MAX];
+	u8 peb_migration_id;
+	u16 peb_index;
+	int migration_state = SSDFS_LBLOCK_UNKNOWN_STATE;
+	struct ssdfs_offset_position pos = {0};
+	int i;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+	BUG_ON(!req);
+
+	SSDFS_DBG("seg %llu, peb_index %u, "
+		  "class %#x, cmd %#x, type %#x, "
+		  "ino %llu, logical_offset %llu, data_bytes %u\n",
+		  pebc->parent_si->seg_id, pebc->peb_index,
+		  req->private.class, req->private.cmd, req->private.type,
+		  req->extent.ino, req->extent.logical_offset,
+		  req->extent.data_bytes);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebc->parent_si->fsi;
+
+	if (req->extent.data_bytes == 0) {
+		SSDFS_WARN("empty read request: ino %llu, logical_offset %llu\n",
+			   req->extent.ino, req->extent.logical_offset);
+		return 0;
+	}
+
+	table = pebc->parent_si->blk2off_table;
+	logical_blk = req->place.start.blk_index + req->result.processed_blks;
+
+	desc_off = ssdfs_blk2off_table_convert(table, logical_blk,
+						&peb_index,
+						&migration_state,
+						&pos);
+	if (IS_ERR(desc_off) && PTR_ERR(desc_off) == -EAGAIN) {
+		struct completion *init_end = &table->full_init_end;
+
+		err = SSDFS_WAIT_COMPLETION(init_end);
+		if (unlikely(err)) {
+			SSDFS_ERR("blk2off init failed: "
+				  "err %d\n", err);
+			return err;
+		}
+
+		desc_off = ssdfs_blk2off_table_convert(table, logical_blk,
+							&peb_index,
+							&migration_state,
+							&pos);
+	}
+
+	if (IS_ERR_OR_NULL(desc_off)) {
+		err = (desc_off == NULL ? -ERANGE : PTR_ERR(desc_off));
+		SSDFS_ERR("fail to convert: "
+			  "logical_blk %u, err %d\n",
+			  logical_blk, err);
+		return err;
+	}
+
+	peb_migration_id = desc_off->blk_state.peb_migration_id;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("logical_blk %u, peb_index %u, "
+		  "logical_offset %u, logical_blk %u, peb_page %u, "
+		  "log_start_page %u, log_area %u, "
+		  "peb_migration_id %u, byte_offset %u\n",
+		  logical_blk, pebc->peb_index,
+		  le32_to_cpu(desc_off->page_desc.logical_offset),
+		  le16_to_cpu(desc_off->page_desc.logical_blk),
+		  le16_to_cpu(desc_off->page_desc.peb_page),
+		  le16_to_cpu(desc_off->blk_state.log_start_page),
+		  desc_off->blk_state.log_area,
+		  desc_off->blk_state.peb_migration_id,
+		  le32_to_cpu(desc_off->blk_state.byte_offset));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (is_ssdfs_logical_block_migrating(migration_state)) {
+		err = ssdfs_blk2off_table_get_block_state(table, req);
+		if (err == -EAGAIN) {
+			desc_off = ssdfs_blk2off_table_convert(table,
+							    logical_blk,
+							    &peb_index,
+							    &migration_state,
+							    &pos);
+			if (IS_ERR_OR_NULL(desc_off)) {
+				err = (desc_off == NULL ?
+						-ERANGE : PTR_ERR(desc_off));
+				SSDFS_ERR("fail to convert: "
+					  "logical_blk %u, err %d\n",
+					  logical_blk, err);
+				return err;
+			}
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to get migrating block state: "
+				  "logical_blk %u, peb_index %u, err %d\n",
+				  logical_blk, pebc->peb_index, err);
+			return err;
+		} else
+			return 0;
+	}
+
+	down_read(&pebc->lock);
+
+	blk_state = &desc_off->blk_state;
+	log_start_page = le16_to_cpu(blk_state->log_start_page);
+
+	if (log_start_page >= fsi->pages_per_peb) {
+		err = -ERANGE;
+		SSDFS_ERR("invalid log_start_page %u\n", log_start_page);
+		goto finish_read_page;
+	}
+
+	err = ssdfs_peb_read_block_state(pebc, req,
+					 desc_off, &pos,
+					 desc_array,
+					 SSDFS_SEG_HDR_DESC_MAX);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read block state: "
+			  "seg %llu, peb_index %u, "
+			  "class %#x, cmd %#x, type %#x, "
+			  "ino %llu, logical_offset %llu, "
+			  "data_bytes %u, migration_state %#x, "
+			  "err %d\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index,
+			  req->private.class, req->private.cmd,
+			  req->private.type,
+			  req->extent.ino,
+			  req->extent.logical_offset,
+			  req->extent.data_bytes,
+			  migration_state,
+			  err);
+
+		SSDFS_ERR("seg_id %llu, peb_index %u, ino %llu, "
+			  "logical_offset %u, peb_index %u, "
+			  "peb_page %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index,
+			  le64_to_cpu(pos.blk_desc.buf.ino),
+			  le32_to_cpu(pos.blk_desc.buf.logical_offset),
+			  le16_to_cpu(pos.blk_desc.buf.peb_index),
+			  le16_to_cpu(pos.blk_desc.buf.peb_page));
+
+		for (i = 0; i < SSDFS_BLK_STATE_OFF_MAX; i++) {
+			blk_state = &pos.blk_desc.buf.state[i];
+
+			SSDFS_ERR("BLK STATE OFFSET %d: "
+				  "log_start_page %u, log_area %#x, "
+				  "byte_offset %u, peb_migration_id %u\n",
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.34.1

Part of the diff is not shown here. View the raw message.

#29 lore raw

RFC PATCH 28/76 ssdfs: PEB flush thread's finite state machine

In reply to Viacheslav Dubeyko

"Physical" Erase Block (PEB) has flush thread that implements logic of log preparation and commit/flush. Segment object has create queue that could contain requests for adding new data or metadata. PEB container object has update queue that contains requests to update existing data. Caller is responsible for allocation, preparation, and adding request(s) into particular queue (create or update). Then flush thread needs to be woken up. Flush thread checks the state of queues, takes request by request, and execute requests. If full log is prepared in memory or commit is requested, then flush thread commits the prepared log into storage device to make it persistent.

CC Viacheslav Dubeyko <[email protected]>
CC Luka Perkov <[email protected]>
CC Bruno Banelli <[email protected]>
---
 fs/ssdfs/peb_flush_thread.c | 3364 +++++++++++++++++++++++++++++++++++
 1 file changed, 3364 insertions(+)
 create mode 100644 fs/ssdfs/peb_flush_thread.c
fs/ssdfs/peb_flush_thread.c new file +3364 -0
new file mode 100644
index 000000000000..6a9032762ea6
@@ -0,0 +1,3364 @@
+// SPDX-License-Identifier: BSD-3-Clause-Clear
+/*
+ * SSDFS -- SSD-oriented File System.
+ *
+ * fs/ssdfs/peb_flush_thread.c - flush thread functionality.
+ *
+ * Copyright (c) 2014-2019 HGST, a Western Digital Company.
+ *              http://www.hgst.com/
+ * Copyright (c) 2014-2023 Viacheslav Dubeyko <[email protected]>
+ *              http://www.ssdfs.org/
+ * Copyright (c) 2022-2023 Bytedance Ltd. and/or its affiliates.
+ *              https://www.bytedance.com/
+ *
+ * (C) Copyright 2014-2019, HGST, Inc., All rights reserved.
+ *
+ * Created by HGST, San Jose Research Center, Storage Architecture Group
+ *
+ * Authors: Viacheslav Dubeyko <[email protected]>
+ *
+ * Acknowledgement: Cyril Guyot
+ *                  Zvonimir Bandic
+ *                  Cong Wang
+ */
+
+#include <linux/pagemap.h>
+#include <linux/slab.h>
+#include <linux/kthread.h>
+#include <linux/pagevec.h>
+#include <linux/delay.h>
+
+#include "peb_mapping_queue.h"
+#include "peb_mapping_table_cache.h"
+#include "ssdfs.h"
+#include "compression.h"
+#include "page_vector.h"
+#include "block_bitmap.h"
+#include "peb_block_bitmap.h"
+#include "segment_block_bitmap.h"
+#include "offset_translation_table.h"
+#include "page_array.h"
+#include "peb.h"
+#include "peb_container.h"
+#include "segment_bitmap.h"
+#include "segment.h"
+#include "current_segment.h"
+#include "peb_mapping_table.h"
+#include "btree_search.h"
+#include "btree_node.h"
+#include "btree.h"
+#include "extents_tree.h"
+#include "diff_on_write.h"
+#include "invalidated_extents_tree.h"
+
+#include <trace/events/ssdfs.h>
+
+#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING
+atomic64_t ssdfs_flush_page_leaks;
+atomic64_t ssdfs_flush_memory_leaks;
+atomic64_t ssdfs_flush_cache_leaks;
+#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */
+
+/*
+ * void ssdfs_flush_cache_leaks_increment(void *kaddr)
+ * void ssdfs_flush_cache_leaks_decrement(void *kaddr)
+ * void *ssdfs_flush_kmalloc(size_t size, gfp_t flags)
+ * void *ssdfs_flush_kzalloc(size_t size, gfp_t flags)
+ * void *ssdfs_flush_kcalloc(size_t n, size_t size, gfp_t flags)
+ * void ssdfs_flush_kfree(void *kaddr)
+ * struct page *ssdfs_flush_alloc_page(gfp_t gfp_mask)
+ * struct page *ssdfs_flush_add_pagevec_page(struct pagevec *pvec)
+ * void ssdfs_flush_free_page(struct page *page)
+ * void ssdfs_flush_pagevec_release(struct pagevec *pvec)
+ */
+#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING
+	SSDFS_MEMORY_LEAKS_CHECKER_FNS(flush)
+#else
+	SSDFS_MEMORY_ALLOCATOR_FNS(flush)
+#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */
+
+void ssdfs_flush_memory_leaks_init(void)
+{
+#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING
+	atomic64_set(&ssdfs_flush_page_leaks, 0);
+	atomic64_set(&ssdfs_flush_memory_leaks, 0);
+	atomic64_set(&ssdfs_flush_cache_leaks, 0);
+#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */
+}
+
+void ssdfs_flush_check_memory_leaks(void)
+{
+#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING
+	if (atomic64_read(&ssdfs_flush_page_leaks) != 0) {
+		SSDFS_ERR("FLUSH THREAD: "
+			  "memory leaks include %lld pages\n",
+			  atomic64_read(&ssdfs_flush_page_leaks));
+	}
+
+	if (atomic64_read(&ssdfs_flush_memory_leaks) != 0) {
+		SSDFS_ERR("FLUSH THREAD: "
+			  "memory allocator suffers from %lld leaks\n",
+			  atomic64_read(&ssdfs_flush_memory_leaks));
+	}
+
+	if (atomic64_read(&ssdfs_flush_cache_leaks) != 0) {
+		SSDFS_ERR("FLUSH THREAD: "
+			  "caches suffers from %lld leaks\n",
+			  atomic64_read(&ssdfs_flush_cache_leaks));
+	}
+#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */
+}
+
+/******************************************************************************
+ *                         FLUSH THREAD FUNCTIONALITY                         *
+ ******************************************************************************/
+
+/*
+ * __ssdfs_finish_request() - common logic of request's finishing
+ * @pebc: pointer on PEB container
+ * @req: request
+ * @wait: wait queue head
+ * @err: error of processing request
+ */
+static
+void __ssdfs_finish_request(struct ssdfs_peb_container *pebc,
+			    struct ssdfs_segment_request *req,
+			    wait_queue_head_t *wait,
+			    int err)
+{
+	u32 pagesize;
+	u32 processed_bytes_max;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+	BUG_ON(!req);
+
+	SSDFS_DBG("req %p, cmd %#x, type %#x, err %d\n",
+		  req, req->private.cmd, req->private.type, err);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	pagesize = pebc->parent_si->fsi->pagesize;
+	processed_bytes_max = req->result.processed_blks * pagesize;
+
+	if (req->extent.data_bytes > processed_bytes_max) {
+		SSDFS_WARN("data_bytes %u > processed_bytes_max %u\n",
+			   req->extent.data_bytes,
+			   processed_bytes_max);
+	}
+
+	req->result.err = err;
+
+	switch (req->private.type) {
+	case SSDFS_REQ_SYNC:
+		/* do nothing */
+		break;
+
+	case SSDFS_REQ_ASYNC:
+		ssdfs_free_flush_request_pages(req);
+		pagevec_reinit(&req->result.pvec);
+		break;
+
+	case SSDFS_REQ_ASYNC_NO_FREE:
+		ssdfs_free_flush_request_pages(req);
+		pagevec_reinit(&req->result.pvec);
+		break;
+
+	default:
+		BUG();
+	};
+
+	switch (req->private.type) {
+	case SSDFS_REQ_SYNC:
+		if (err) {
+			SSDFS_DBG("failure: req %p, err %d\n", req, err);
+			atomic_set(&req->result.state, SSDFS_REQ_FAILED);
+		} else
+			atomic_set(&req->result.state, SSDFS_REQ_FINISHED);
+
+		complete(&req->result.wait);
+		wake_up_all(&req->private.wait_queue);
+		break;
+
+	case SSDFS_REQ_ASYNC:
+		ssdfs_put_request(req);
+
+		if (err) {
+			SSDFS_DBG("failure: req %p, err %d\n", req, err);
+			atomic_set(&req->result.state, SSDFS_REQ_FAILED);
+		} else
+			atomic_set(&req->result.state, SSDFS_REQ_FINISHED);
+
+		complete(&req->result.wait);
+
+		if (atomic_read(&req->private.refs_count) != 0) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("start waiting: refs_count %d\n",
+				   atomic_read(&req->private.refs_count));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			err = wait_event_killable_timeout(*wait,
+			    atomic_read(&req->private.refs_count) == 0,
+			    SSDFS_DEFAULT_TIMEOUT);
+			if (err < 0)
+				WARN_ON(err < 0);
+			else
+				err = 0;
+		}
+
+		wake_up_all(&req->private.wait_queue);
+		ssdfs_request_free(req);
+		break;
+
+	case SSDFS_REQ_ASYNC_NO_FREE:
+		ssdfs_put_request(req);
+
+		if (err) {
+			SSDFS_DBG("failure: req %p, err %d\n", req, err);
+			atomic_set(&req->result.state, SSDFS_REQ_FAILED);
+		} else
+			atomic_set(&req->result.state, SSDFS_REQ_FINISHED);
+
+		complete(&req->result.wait);
+
+		if (atomic_read(&req->private.refs_count) != 0) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("start waiting: refs_count %d\n",
+				   atomic_read(&req->private.refs_count));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			err = wait_event_killable_timeout(*wait,
+			    atomic_read(&req->private.refs_count) == 0,
+			    SSDFS_DEFAULT_TIMEOUT);
+			if (err < 0)
+				WARN_ON(err < 0);
+			else
+				err = 0;
+		}
+
+		wake_up_all(&req->private.wait_queue);
+		break;
+
+	default:
+		atomic_set(&req->result.state, SSDFS_REQ_FAILED);
+		BUG();
+	};
+}
+
+/*
+ * ssdfs_finish_pre_allocate_request() - finish pre-allocate request
+ * @pebc: pointer on PEB container
+ * @req: request
+ * @wait: wait queue head
+ * @err: error of processing request
+ *
+ * This function finishes pre-allocate request processing. If attempt of
+ * pre-allocate an extent has been resulted with %-EAGAIN error then
+ * function returns request into create queue for final
+ * processing.
+ */
+static
+void ssdfs_finish_pre_allocate_request(struct ssdfs_peb_container *pebc,
+					struct ssdfs_segment_request *req,
+					wait_queue_head_t *wait,
+					int err)
+{
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+	BUG_ON(!req);
+
+	SSDFS_DBG("req %p, cmd %#x, type %#x, err %d\n",
+		  req, req->private.cmd, req->private.type, err);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (!err) {
+		WARN_ON(pagevec_count(&req->result.pvec) != 0);
+		ssdfs_flush_pagevec_release(&req->result.pvec);
+	}
+
+	if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("return request into queue: "
+			  "seg %llu, peb_index %u, "
+			  "ino %llu, logical_offset %llu, "
+			  "data_bytes %u, cno %llu, parent_snapshot %llu"
+			  "seg %llu, logical_block %u, cmd %#x, type %#x, "
+			  "processed_blks %d\n",
+			  pebc->parent_si->seg_id, pebc->peb_index,
+			  req->extent.ino, req->extent.logical_offset,
+			  req->extent.data_bytes, req->extent.cno,
+			  req->extent.parent_snapshot,
+			  req->place.start.seg_id, req->place.start.blk_index,
+			  req->private.cmd, req->private.type,
+			  req->result.processed_blks);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		atomic_set(&req->result.state, SSDFS_REQ_CREATED);
+
+		spin_lock(&pebc->crq_ptr_lock);
+		ssdfs_requests_queue_add_head(pebc->create_rq, req);
+		spin_unlock(&pebc->crq_ptr_lock);
+	} else
+		__ssdfs_finish_request(pebc, req, wait, err);
+}
+
+/*
+ * ssdfs_finish_create_request() - finish create request
+ * @pebc: pointer on PEB container
+ * @req: request
+ * @wait: wait queue head
+ * @err: error of processing request
+ *
+ * This function finishes create request processing. If attempt of
+ * adding data block has been resulted with %-EAGAIN error then
+ * function returns request into create queue for final
+ * processing.
+ */
+static
+void ssdfs_finish_create_request(struct ssdfs_peb_container *pebc,
+				 struct ssdfs_segment_request *req,
+				 wait_queue_head_t *wait,
+				 int err)
+{
+	int i;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+	BUG_ON(!req);
+
+	SSDFS_DBG("req %p, cmd %#x, type %#x, err %d\n",
+		  req, req->private.cmd, req->private.type, err);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (!err) {
+		for (i = 0; i < req->result.processed_blks; i++)
+			ssdfs_peb_mark_request_block_uptodate(pebc, req, i);
+	}
+
+	if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("return request into queue: "
+			  "seg %llu, peb_index %u, "
+			  "ino %llu, logical_offset %llu, "
+			  "data_bytes %u, cno %llu, parent_snapshot %llu"
+			  "seg %llu, logical_block %u, cmd %#x, type %#x, "
+			  "processed_blks %d\n",
+			  pebc->parent_si->seg_id, pebc->peb_index,
+			  req->extent.ino, req->extent.logical_offset,
+			  req->extent.data_bytes, req->extent.cno,
+			  req->extent.parent_snapshot,
+			  req->place.start.seg_id, req->place.start.blk_index,
+			  req->private.cmd, req->private.type,
+			  req->result.processed_blks);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		atomic_set(&req->result.state, SSDFS_REQ_CREATED);
+
+		spin_lock(&pebc->crq_ptr_lock);
+		ssdfs_requests_queue_add_head(pebc->create_rq, req);
+		spin_unlock(&pebc->crq_ptr_lock);
+	} else
+		__ssdfs_finish_request(pebc, req, wait, err);
+}
+
+/*
+ * ssdfs_finish_update_request() - finish update request
+ * @pebc: pointer on PEB container
+ * @req: request
+ * @wait: wait queue head
+ * @err: error of processing request
+ *
+ * This function finishes update request processing.
+ */
+static
+void ssdfs_finish_update_request(struct ssdfs_peb_container *pebc,
+				 struct ssdfs_segment_request *req,
+				 wait_queue_head_t *wait,
+				 int err)
+{
+	int i;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+	BUG_ON(!req);
+
+	SSDFS_DBG("req %p, cmd %#x, type %#x, err %d\n",
+		  req, req->private.cmd, req->private.type, err);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (!err) {
+		for (i = 0; i < req->result.processed_blks; i++)
+			ssdfs_peb_mark_request_block_uptodate(pebc, req, i);
+	}
+
+	__ssdfs_finish_request(pebc, req, wait, err);
+}
+
+/*
+ * ssdfs_finish_flush_request() - finish flush request
+ * @pebc: pointer on PEB container
+ * @req: request
+ * @wait: wait queue head
+ * @err: error of processing request
+ */
+static inline
+void ssdfs_finish_flush_request(struct ssdfs_peb_container *pebc,
+				struct ssdfs_segment_request *req,
+				wait_queue_head_t *wait,
+				int err)
+{
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si || !pebc->parent_si->fsi);
+	BUG_ON(!req);
+
+	SSDFS_DBG("req %p, cmd %#x, type %#x, err %d\n",
+		  req, req->private.cmd, req->private.type, err);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	switch (req->private.class) {
+	case SSDFS_PEB_PRE_ALLOCATE_DATA_REQ:
+	case SSDFS_PEB_PRE_ALLOCATE_LNODE_REQ:
+	case SSDFS_PEB_PRE_ALLOCATE_HNODE_REQ:
+	case SSDFS_PEB_PRE_ALLOCATE_IDXNODE_REQ:
+		ssdfs_finish_pre_allocate_request(pebc, req, wait, err);
+		break;
+
+	case SSDFS_PEB_CREATE_DATA_REQ:
+	case SSDFS_PEB_CREATE_LNODE_REQ:
+	case SSDFS_PEB_CREATE_HNODE_REQ:
+	case SSDFS_PEB_CREATE_IDXNODE_REQ:
+		ssdfs_finish_create_request(pebc, req, wait, err);
+		break;
+
+	case SSDFS_PEB_UPDATE_REQ:
+	case SSDFS_PEB_PRE_ALLOC_UPDATE_REQ:
+	case SSDFS_PEB_DIFF_ON_WRITE_REQ:
+	case SSDFS_PEB_COLLECT_GARBAGE_REQ:
+	case SSDFS_ZONE_USER_DATA_MIGRATE_REQ:
+		ssdfs_finish_update_request(pebc, req, wait, err);
+		break;
+
+	default:
+		BUG();
+	};
+
+	ssdfs_forget_user_data_flush_request(pebc->parent_si);
+	ssdfs_segment_finish_request_cno(pebc->parent_si);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("flush_reqs %lld\n",
+		  atomic64_read(&pebc->parent_si->fsi->flush_reqs));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	WARN_ON(atomic64_dec_return(&pebc->parent_si->fsi->flush_reqs) < 0);
+}
+
+/*
+ * ssdfs_peb_clear_current_log_pages() - clear dirty pages of current log
+ * @pebi: pointer on PEB object
+ */
+static inline
+void ssdfs_peb_clear_current_log_pages(struct ssdfs_peb_info *pebi)
+{
+	struct ssdfs_page_array *area_pages;
+	int i;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	for (i = 0; i < SSDFS_LOG_AREA_MAX; i++) {
+		area_pages = &pebi->current_log.area[i].array;
+		err = ssdfs_page_array_clear_all_dirty_pages(area_pages);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to clear dirty pages: "
+				  "area_type %#x, err %d\n",
+				  i, err);
+		}
+	}
+}
+
+/*
+ * ssdfs_peb_clear_current_log_pages() - clear dirty pages of PEB's cache
+ * @pebi: pointer on PEB object
+ */
+static inline
+void ssdfs_peb_clear_cache_dirty_pages(struct ssdfs_peb_info *pebi)
+{
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_page_array_clear_all_dirty_pages(&pebi->cache);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to clear dirty pages: "
+			  "err %d\n",
+			  err);
+	}
+}
+
+/*
+ * ssdfs_peb_commit_log_on_thread_stop() - commit log on thread stopping
+ * @pebi: pointer on PEB object
+ * @cur_segs: current segment IDs array
+ * @size: size of segment IDs array size in bytes
+ */
+static
+int ssdfs_peb_commit_log_on_thread_stop(struct ssdfs_peb_info *pebi,
+					__le64 *cur_segs, size_t size)
+{
+	struct ssdfs_fs_info *fsi;
+	u64 reserved_new_user_data_pages;
+	u64 updated_user_data_pages;
+	u64 flushing_user_data_requests;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb_index %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	if (ssdfs_peb_has_dirty_pages(pebi)) {
+		/*
+		 * Unexpected situation.
+		 * Try to commit anyway.
+		 */
+
+		spin_lock(&fsi->volume_state_lock);
+		reserved_new_user_data_pages =
+			fsi->reserved_new_user_data_pages;
+		updated_user_data_pages =
+			fsi->updated_user_data_pages;
+		flushing_user_data_requests =
+			fsi->flushing_user_data_requests;
+		spin_unlock(&fsi->volume_state_lock);
+
+		SSDFS_WARN("PEB has dirty pages: "
+			   "seg %llu, peb %llu, peb_type %#x, "
+			   "global_fs_state %#x, "
+			   "reserved_new_user_data_pages %llu, "
+			   "updated_user_data_pages %llu, "
+			   "flushing_user_data_requests %llu\n",
+			   pebi->pebc->parent_si->seg_id,
+			   pebi->peb_id, pebi->pebc->peb_type,
+			   atomic_read(&fsi->global_fs_state),
+			   reserved_new_user_data_pages,
+			   updated_user_data_pages,
+			   flushing_user_data_requests);
+
+		err = ssdfs_peb_commit_log(pebi, cur_segs, size);
+		if (unlikely(err)) {
+			SSDFS_CRIT("fail to commit log: "
+				   "seg %llu, peb_index %u, err %d\n",
+				   pebi->pebc->parent_si->seg_id,
+				   pebi->peb_index, err);
+
+			ssdfs_peb_clear_current_log_pages(pebi);
+			ssdfs_peb_clear_cache_dirty_pages(pebi);
+		}
+	}
+
+	return err;
+}
+
+/*
+ * ssdfs_peb_get_current_log_state() - get state of PEB's current log
+ * @pebc: pointer on PEB container
+ */
+static
+int ssdfs_peb_get_current_log_state(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_peb_info *pebi = NULL;
+	bool is_peb_exhausted;
+	int state;
+	int err = 0;
+
+	fsi = pebc->parent_si->fsi;
+
+try_get_current_state:
+	down_read(&pebc->lock);
+
+	switch (atomic_read(&pebc->migration_state)) {
+	case SSDFS_PEB_NOT_MIGRATING:
+		pebi = pebc->src_peb;
+		if (!pebi) {
+			err = -ERANGE;
+			SSDFS_WARN("source PEB is NULL\n");
+			goto finish_get_current_log_state;
+		}
+		state = atomic_read(&pebi->current_log.state);
+		break;
+
+	case SSDFS_PEB_UNDER_MIGRATION:
+		pebi = pebc->src_peb;
+		if (!pebi) {
+			err = -ERANGE;
+			SSDFS_WARN("source PEB is NULL\n");
+			goto finish_get_current_log_state;
+		}
+
+		ssdfs_peb_current_log_lock(pebi);
+		is_peb_exhausted = is_ssdfs_peb_exhausted(fsi, pebi);
+		ssdfs_peb_current_log_unlock(pebi);
+
+		if (is_peb_exhausted) {
+			pebi = pebc->dst_peb;
+			if (!pebi) {
+				err = -ERANGE;
+				SSDFS_WARN("destination PEB is NULL\n");
+				goto finish_get_current_log_state;
+			}
+		}
+
+		state = atomic_read(&pebi->current_log.state);
+		break;
+
+	case SSDFS_PEB_MIGRATION_PREPARATION:
+	case SSDFS_PEB_RELATION_PREPARATION:
+	case SSDFS_PEB_FINISHING_MIGRATION:
+		err = -EAGAIN;
+		break;
+
+	default:
+		err = -ERANGE;
+		SSDFS_WARN("invalid state: %#x\n",
+			   atomic_read(&pebc->migration_state));
+		goto finish_get_current_log_state;
+		break;
+	}
+
+finish_get_current_log_state:
+	up_read(&pebc->lock);
+
+	if (err == -EAGAIN) {
+		DEFINE_WAIT(wait);
+
+		err = 0;
+		prepare_to_wait(&pebc->migration_wq, &wait,
+				TASK_UNINTERRUPTIBLE);
+		schedule();
+		finish_wait(&pebc->migration_wq, &wait);
+		goto try_get_current_state;
+	} else if (unlikely(err))
+		state = SSDFS_LOG_UNKNOWN;
+
+	return state;
+}
+
+bool is_ssdfs_peb_exhausted(struct ssdfs_fs_info *fsi,
+			    struct ssdfs_peb_info *pebi)
+{
+	bool is_exhausted = false;
+	u16 start_page;
+	u32 pages_per_peb;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!fsi || !pebi);
+	BUG_ON(!mutex_is_locked(&pebi->current_log.lock));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	start_page = pebi->current_log.start_page;
+	pages_per_peb = min_t(u32, fsi->leb_pages_capacity,
+				   fsi->peb_pages_capacity);
+
+	switch (atomic_read(&pebi->current_log.state)) {
+	case SSDFS_LOG_INITIALIZED:
+	case SSDFS_LOG_COMMITTED:
+	case SSDFS_LOG_CREATED:
+		is_exhausted = start_page >= pages_per_peb;
+		break;
+
+	default:
+		is_exhausted = false;
+		break;
+	};
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg_id %llu, peb_id %llu, start_page %u, "
+		  "pages_per_peb %u, is_exhausted %#x\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, start_page,
+		  pages_per_peb, is_exhausted);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	return is_exhausted;
+}
+
+bool is_ssdfs_peb_ready_to_exhaust(struct ssdfs_fs_info *fsi,
+				   struct ssdfs_peb_info *pebi)
+{
+	bool is_ready_to_exhaust = false;
+	u16 start_page;
+	u32 pages_per_peb;
+	u16 free_data_pages;
+	u16 reserved_pages;
+	u16 min_partial_log_pages;
+	int empty_pages;
+	int migration_state;
+	int migration_phase;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi);
+	BUG_ON(!mutex_is_locked(&pebi->current_log.lock));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	migration_state = atomic_read(&pebi->pebc->migration_state);
+	migration_phase = atomic_read(&pebi->pebc->migration_phase);
+
+	switch (migration_state) {
+	case SSDFS_PEB_NOT_MIGRATING:
+		/* continue logic */
+		break;
+
+	case SSDFS_PEB_UNDER_MIGRATION:
+		switch (migration_phase) {
+		case SSDFS_SRC_PEB_NOT_EXHAUSTED:
+			is_ready_to_exhaust = false;
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("peb under migration: "
+				  "src_peb %llu is not exhausted\n",
+				  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return is_ready_to_exhaust;
+
+		default:
+			/* continue logic */
+			break;
+		}
+		break;
+
+	case SSDFS_PEB_MIGRATION_PREPARATION:
+	case SSDFS_PEB_RELATION_PREPARATION:
+	case SSDFS_PEB_FINISHING_MIGRATION:
+		is_ready_to_exhaust = true;
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("peb is going to migrate: "
+			  "src_peb %llu is exhausted\n",
+			  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return is_ready_to_exhaust;
+
+	default:
+		SSDFS_WARN("migration_state %#x, migration_phase %#x\n",
+			   migration_state, migration_phase);
+		BUG();
+		break;
+	}
+
+	start_page = pebi->current_log.start_page;
+	pages_per_peb = min_t(u32, fsi->leb_pages_capacity,
+				   fsi->peb_pages_capacity);
+	empty_pages = pages_per_peb - start_page;
+	free_data_pages = pebi->current_log.free_data_pages;
+	reserved_pages = pebi->current_log.reserved_pages;
+	min_partial_log_pages = ssdfs_peb_estimate_min_partial_log_pages(pebi);
+
+	switch (atomic_read(&pebi->current_log.state)) {
+	case SSDFS_LOG_INITIALIZED:
+	case SSDFS_LOG_COMMITTED:
+	case SSDFS_LOG_CREATED:
+		if (empty_pages > min_partial_log_pages)
+			is_ready_to_exhaust = false;
+		else if (reserved_pages == 0) {
+			if (free_data_pages <= min_partial_log_pages)
+				is_ready_to_exhaust = true;
+			else
+				is_ready_to_exhaust = false;
+		} else {
+			if (free_data_pages < min_partial_log_pages)
+				is_ready_to_exhaust = true;
+			else
+				is_ready_to_exhaust = false;
+		}
+		break;
+
+	default:
+		is_ready_to_exhaust = false;
+		break;
+	};
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg_id %llu, peb_id %llu, free_data_pages %u, "
+		  "reserved_pages %u, min_partial_log_pages %u, "
+		  "is_ready_to_exhaust %#x\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, free_data_pages,
+		  reserved_pages, min_partial_log_pages,
+		  is_ready_to_exhaust);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	return is_ready_to_exhaust;
+}
+
+static inline
+bool ssdfs_peb_has_partial_empty_log(struct ssdfs_fs_info *fsi,
+				     struct ssdfs_peb_info *pebi)
+{
+	bool has_partial_empty_log = false;
+	u16 start_page;
+	u32 pages_per_peb;
+	u16 log_pages;
+	int empty_pages;
+	u16 min_partial_log_pages;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!fsi || !pebi);
+	BUG_ON(!mutex_is_locked(&pebi->current_log.lock));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	start_page = pebi->current_log.start_page;
+	pages_per_peb = min_t(u32, fsi->leb_pages_capacity,
+				   fsi->peb_pages_capacity);
+	log_pages = pebi->log_pages;
+	min_partial_log_pages = ssdfs_peb_estimate_min_partial_log_pages(pebi);
+
+	switch (atomic_read(&pebi->current_log.state)) {
+	case SSDFS_LOG_INITIALIZED:
+	case SSDFS_LOG_COMMITTED:
+	case SSDFS_LOG_CREATED:
+		empty_pages = pages_per_peb - start_page;
+		if (empty_pages < 0)
+			has_partial_empty_log = false;
+		else if (empty_pages < min_partial_log_pages)
+			has_partial_empty_log = true;
+		else
+			has_partial_empty_log = false;
+		break;
+
+	default:
+		has_partial_empty_log = false;
+		break;
+	};
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg_id %llu, peb_id %llu, start_page %u, "
+		  "pages_per_peb %u, log_pages %u, "
+		  "min_partial_log_pages %u, "
+		  "has_partial_empty_log %#x\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, start_page,
+		  pages_per_peb, log_pages,
+		  min_partial_log_pages,
+		  has_partial_empty_log);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	return has_partial_empty_log;
+}
+
+static inline
+bool has_commit_log_now_requested(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_segment_request *req = NULL;
+	bool commit_log_now = false;
+	int err;
+
+	if (is_ssdfs_requests_queue_empty(&pebc->update_rq))
+		return false;
+
+	err = ssdfs_requests_queue_remove_first(&pebc->update_rq, &req);
+	if (err || !req)
+		return false;
+
+	commit_log_now = req->private.cmd == SSDFS_COMMIT_LOG_NOW;
+	ssdfs_requests_queue_add_head(&pebc->update_rq, req);
+	return commit_log_now;
+}
+
+static inline
+bool has_start_migration_now_requested(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_segment_request *req = NULL;
+	bool start_migration_now = false;
+	int err;
+
+	if (is_ssdfs_requests_queue_empty(&pebc->update_rq))
+		return false;
+
+	err = ssdfs_requests_queue_remove_first(&pebc->update_rq, &req);
+	if (err || !req)
+		return false;
+
+	start_migration_now = req->private.cmd == SSDFS_START_MIGRATION_NOW;
+	ssdfs_requests_queue_add_head(&pebc->update_rq, req);
+	return start_migration_now;
+}
+
+static inline
+void ssdfs_peb_check_update_queue(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_segment_request *req = NULL;
+	int err;
+
+	if (is_ssdfs_requests_queue_empty(&pebc->update_rq)) {
+		SSDFS_DBG("update request queue is empty\n");
+		return;
+	}
+
+	err = ssdfs_requests_queue_remove_first(&pebc->update_rq, &req);
+	if (err || !req)
+		return;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg_id %llu, peb_index %u\n",
+		  pebc->parent_si->seg_id,
+		  pebc->peb_index);
+	SSDFS_DBG("req->private.class %#x, req->private.cmd %#x\n",
+		  req->private.class, req->private.cmd);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	ssdfs_requests_queue_add_head(&pebc->update_rq, req);
+	return;
+}
+
+static inline
+int __ssdfs_peb_finish_migration(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_segment_info *si = pebc->parent_si;
+	struct ssdfs_segment_request *req;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb_index %u\n",
+		  pebc->parent_si->seg_id, pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_finish_migration(pebc);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to finish migration: "
+			  "seg %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+		return err;
+	}
+
+	/*
+	 * The responsibility of finish migration code
+	 * is to copy the state of valid blocks of
+	 * source erase block into the buffers.
+	 * So, this buffered state of valid blocks
+	 * should be commited ASAP. It needs to send
+	 * the COMMIT_LOG_NOW command to guarantee
+	 * that valid blocks will be flushed on the volume.
+	 */
+
+	req = ssdfs_request_alloc();
+	if (IS_ERR_OR_NULL(req)) {
+		err = (req == NULL ? -ENOMEM : PTR_ERR(req));
+		SSDFS_ERR("fail to allocate request: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	ssdfs_request_init(req);
+	ssdfs_get_request(req);
+
+	err = ssdfs_segment_commit_log_async2(si, SSDFS_REQ_ASYNC,
+						pebc->peb_index, req);
+	if (unlikely(err)) {
+		SSDFS_ERR("commit log request failed: "
+			  "err %d\n", err);
+		ssdfs_put_request(req);
+		ssdfs_request_free(req);
+		return err;
+	}
+
+	return 0;
+}
+
+static inline
+bool need_wait_next_create_data_request(struct ssdfs_peb_info *pebi)
+{
+	struct ssdfs_segment_info *si = pebi->pebc->parent_si;
+	struct ssdfs_fs_info *fsi = si->fsi;
+	bool has_pending_pages = false;
+	bool has_reserved_pages = false;
+	int state;
+	bool is_current_seg = false;
+	u64 reserved_pages = 0;
+	u64 pending_pages = 0;
+	bool need_wait = false;
+
+	if (!is_ssdfs_peb_containing_user_data(pebi->pebc))
+		goto finish_check;
+
+	spin_lock(&si->pending_lock);
+	pending_pages = si->pending_new_user_data_pages;
+	has_pending_pages = si->pending_new_user_data_pages > 0;
+	spin_unlock(&si->pending_lock);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("pending_pages %llu\n", pending_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (has_pending_pages) {
+		need_wait = true;
+		goto finish_check;
+	}
+
+	spin_lock(&fsi->volume_state_lock);
+	reserved_pages = fsi->reserved_new_user_data_pages;
+	has_reserved_pages = fsi->reserved_new_user_data_pages > 0;
+	spin_unlock(&fsi->volume_state_lock);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("reserved_pages %llu\n", reserved_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	state = atomic_read(&si->obj_state);
+	is_current_seg = (state == SSDFS_CURRENT_SEG_OBJECT);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("is_current_seg %#x, has_reserved_pages %#x\n",
+		  is_current_seg, has_reserved_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	need_wait = is_current_seg && has_reserved_pages;
+
+finish_check:
+	return need_wait;
+}
+
+static inline
+bool need_wait_next_update_request(struct ssdfs_peb_info *pebi)
+{
+	struct ssdfs_segment_info *si = pebi->pebc->parent_si;
+	struct ssdfs_fs_info *fsi = si->fsi;
+	bool has_pending_pages = false;
+	bool has_updated_pages = false;
+	u64 updated_pages = 0;
+	u64 pending_pages = 0;
+	bool need_wait = false;
+
+	if (!is_ssdfs_peb_containing_user_data(pebi->pebc))
+		goto finish_check;
+
+	spin_lock(&pebi->pebc->pending_lock);
+	pending_pages = pebi->pebc->pending_updated_user_data_pages;
+	has_pending_pages = pending_pages > 0;
+	spin_unlock(&pebi->pebc->pending_lock);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("pending_pages %llu\n", pending_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (has_pending_pages) {
+		need_wait = true;
+		goto finish_check;
+	}
+
+	spin_lock(&fsi->volume_state_lock);
+	updated_pages = fsi->updated_user_data_pages;
+	has_updated_pages = fsi->updated_user_data_pages > 0;
+	spin_unlock(&fsi->volume_state_lock);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("updated_pages %llu\n", updated_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	need_wait = has_updated_pages;
+
+finish_check:
+	return need_wait;
+}
+
+static inline
+bool no_more_updated_pages(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_segment_info *si = pebc->parent_si;
+	struct ssdfs_fs_info *fsi = si->fsi;
+	bool has_updated_pages = false;
+
+	if (!is_ssdfs_peb_containing_user_data(pebc))
+		return true;
+
+	spin_lock(&fsi->volume_state_lock);
+	has_updated_pages = fsi->updated_user_data_pages > 0;
+	spin_unlock(&fsi->volume_state_lock);
+
+	return !has_updated_pages;
+}
+
+static inline
+bool is_regular_fs_operations(struct ssdfs_peb_container *pebc)
+{
+	int state;
+
+	state = atomic_read(&pebc->parent_si->fsi->global_fs_state);
+	return state == SSDFS_REGULAR_FS_OPERATIONS;
+}
+
+/* Flush thread possible states */
+enum {
+	SSDFS_FLUSH_THREAD_ERROR,
+	SSDFS_FLUSH_THREAD_FREE_SPACE_ABSENT,
+	SSDFS_FLUSH_THREAD_RO_STATE,
+	SSDFS_FLUSH_THREAD_NEED_CREATE_LOG,
+	SSDFS_FLUSH_THREAD_CHECK_STOP_CONDITION,
+	SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST,
+	SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST,
+	SSDFS_FLUSH_THREAD_PROCESS_CREATE_REQUEST,
+	SSDFS_FLUSH_THREAD_PROCESS_UPDATE_REQUEST,
+	SSDFS_FLUSH_THREAD_PROCESS_INVALIDATED_EXTENT,
+	SSDFS_FLUSH_THREAD_CHECK_MIGRATION_NEED,
+	SSDFS_FLUSH_THREAD_START_MIGRATION_NOW,
+	SSDFS_FLUSH_THREAD_COMMIT_LOG,
+	SSDFS_FLUSH_THREAD_DELEGATE_CREATE_ROLE,
+};
+
+#define FLUSH_THREAD_WAKE_CONDITION(pebc) \
+	(kthread_should_stop() || have_flush_requests(pebc))
+#define FLUSH_FAILED_THREAD_WAKE_CONDITION() \
+	(kthread_should_stop())
+#define FLUSH_THREAD_CUR_SEG_WAKE_CONDITION(pebc) \
+	(kthread_should_stop() || have_flush_requests(pebc) || \
+	 !is_regular_fs_operations(pebc) || \
+	 atomic_read(&pebc->parent_si->obj_state) != SSDFS_CURRENT_SEG_OBJECT)
+#define FLUSH_THREAD_UPDATE_WAKE_CONDITION(pebc) \
+	(kthread_should_stop() || have_flush_requests(pebc) || \
+	 no_more_updated_pages(pebc) || !is_regular_fs_operations(pebc))
+#define FLUSH_THREAD_INVALIDATE_WAKE_CONDITION(pebc) \
+	(kthread_should_stop() || have_flush_requests(pebc) || \
+	 !is_regular_fs_operations(pebc))
+
+static
+int wait_next_create_request(struct ssdfs_peb_container *pebc,
+			     int *thread_state)
+{
+	struct ssdfs_segment_info *si = pebc->parent_si;
+	struct ssdfs_fs_info *fsi = si->fsi;
+	struct ssdfs_peb_info *pebi;
+	u64 reserved_pages = 0;
+	bool has_reserved_pages = false;
+	int state;
+	bool is_current_seg = false;
+	bool has_dirty_pages = false;
+	bool need_commit_log = false;
+	wait_queue_head_t *wq = NULL;
+	struct ssdfs_segment_request *req;
+	int err;
+
+	if (!is_ssdfs_peb_containing_user_data(pebc)) {
+		*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+		return -ERANGE;
+	}
+
+	spin_lock(&fsi->volume_state_lock);
+	reserved_pages = fsi->reserved_new_user_data_pages;
+	has_reserved_pages = reserved_pages > 0;
+	spin_unlock(&fsi->volume_state_lock);
+
+	state = atomic_read(&si->obj_state);
+	is_current_seg = (state == SSDFS_CURRENT_SEG_OBJECT);
+
+	if (is_current_seg && has_reserved_pages) {
+		wq = &fsi->pending_wq;
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("wait next data request: reserved_pages %llu\n",
+			  reserved_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		err = wait_event_killable_timeout(*wq,
+				FLUSH_THREAD_CUR_SEG_WAKE_CONDITION(pebc),
+				SSDFS_DEFAULT_TIMEOUT);
+		if (err < 0)
+			WARN_ON(err < 0);
+		else
+			err = 0;
+	}
+
+	state = atomic_read(&si->obj_state);
+	is_current_seg = (state == SSDFS_CURRENT_SEG_OBJECT);
+
+	pebi = ssdfs_get_current_peb_locked(pebc);
+	if (!IS_ERR_OR_NULL(pebi)) {
+		ssdfs_peb_current_log_lock(pebi);
+		has_dirty_pages = ssdfs_peb_has_dirty_pages(pebi);
+		ssdfs_peb_current_log_unlock(pebi);
+		ssdfs_unlock_current_peb(pebc);
+	}
+
+	if (!is_regular_fs_operations(pebc))
+		need_commit_log = true;
+	else if (!is_current_seg)
+		need_commit_log = true;
+	else if (!have_flush_requests(pebc) && has_dirty_pages)
+		need_commit_log = true;
+
+	if (need_commit_log) {
+		req = ssdfs_request_alloc();
+		if (IS_ERR_OR_NULL(req)) {
+			err = (req == NULL ? -ENOMEM : PTR_ERR(req));
+			SSDFS_ERR("fail to allocate request: "
+				  "err %d\n", err);
+			*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			return err;
+		}
+
+		ssdfs_request_init(req);
+		ssdfs_get_request(req);
+
+		err = ssdfs_segment_commit_log_async2(si, SSDFS_REQ_ASYNC,
+							pebc->peb_index, req);
+		if (unlikely(err)) {
+			SSDFS_ERR("commit log request failed: "
+				  "err %d\n", err);
+			ssdfs_put_request(req);
+			ssdfs_request_free(req);
+			*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			return err;
+		}
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("request commit log now: reserved_pages %llu\n",
+			  reserved_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+	} else {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("get next create request: reserved_pages %llu\n",
+			  reserved_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+	*thread_state = SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST;
+	return 0;
+}
+
+static
+int wait_next_update_request(struct ssdfs_peb_container *pebc,
+			     int *thread_state)
+{
+	struct ssdfs_segment_info *si = pebc->parent_si;
+	struct ssdfs_peb_info *pebi;
+	struct ssdfs_fs_info *fsi = si->fsi;
+	u64 updated_pages = 0;
+	bool has_updated_pages = false;
+	bool has_dirty_pages = false;
+	bool need_commit_log = false;
+	wait_queue_head_t *wq = NULL;
+	struct ssdfs_segment_request *req;
+	int err;
+
+	if (!is_ssdfs_peb_containing_user_data(pebc)) {
+		*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+		return -ERANGE;
+	}
+
+	spin_lock(&fsi->volume_state_lock);
+	updated_pages = fsi->updated_user_data_pages;
+	has_updated_pages = updated_pages > 0;
+	spin_unlock(&fsi->volume_state_lock);
+
+	if (has_updated_pages) {
+		wq = &fsi->pending_wq;
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("wait next update request: updated_pages %llu\n",
+			  updated_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		err = wait_event_killable_timeout(*wq,
+				FLUSH_THREAD_UPDATE_WAKE_CONDITION(pebc),
+				SSDFS_DEFAULT_TIMEOUT);
+		if (err < 0)
+			WARN_ON(err < 0);
+		else
+			err = 0;
+	}
+
+	pebi = ssdfs_get_current_peb_locked(pebc);
+	if (!IS_ERR_OR_NULL(pebi)) {
+		ssdfs_peb_current_log_lock(pebi);
+		has_dirty_pages = ssdfs_peb_has_dirty_pages(pebi);
+		ssdfs_peb_current_log_unlock(pebi);
+		ssdfs_unlock_current_peb(pebc);
+	}
+
+	if (!is_regular_fs_operations(pebc))
+		need_commit_log = true;
+	else if (no_more_updated_pages(pebc))
+		need_commit_log = true;
+	else if (!have_flush_requests(pebc) && has_dirty_pages)
+		need_commit_log = true;
+
+	if (need_commit_log) {
+		req = ssdfs_request_alloc();
+		if (IS_ERR_OR_NULL(req)) {
+			err = (req == NULL ? -ENOMEM : PTR_ERR(req));
+			SSDFS_ERR("fail to allocate request: "
+				  "err %d\n", err);
+			*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			return err;
+		}
+
+		ssdfs_request_init(req);
+		ssdfs_get_request(req);
+
+		err = ssdfs_segment_commit_log_async2(si, SSDFS_REQ_ASYNC,
+							pebc->peb_index, req);
+		if (unlikely(err)) {
+			SSDFS_ERR("commit log request failed: "
+				  "err %d\n", err);
+			ssdfs_put_request(req);
+			ssdfs_request_free(req);
+			*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			return err;
+		}
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("request commit log now: updated_pages %llu\n",
+			  updated_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+	} else {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("get next create request: updated_pages %llu\n",
+			  updated_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+	*thread_state = SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST;
+	return 0;
+}
+
+static
+int wait_next_invalidate_request(struct ssdfs_peb_container *pebc,
+				 int *thread_state)
+{
+	struct ssdfs_segment_info *si = pebc->parent_si;
+	struct ssdfs_fs_info *fsi = si->fsi;
+	int state;
+	wait_queue_head_t *wq = NULL;
+	struct ssdfs_segment_request *req;
+	int err;
+
+	if (!is_ssdfs_peb_containing_user_data(pebc)) {
+		*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+		return -ERANGE;
+	}
+
+	if (!is_user_data_pages_invalidated(si)) {
+		*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+		return -ERANGE;
+	}
+
+	state = atomic_read(&fsi->global_fs_state);
+	switch(state) {
+	case SSDFS_REGULAR_FS_OPERATIONS:
+		wq = &fsi->pending_wq;
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("wait next invalidate request\n");
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		err = wait_event_killable_timeout(*wq,
+				FLUSH_THREAD_INVALIDATE_WAKE_CONDITION(pebc),
+				SSDFS_DEFAULT_TIMEOUT);
+		if (err < 0)
+			WARN_ON(err < 0);
+		else
+			err = 0;
+
+		if (have_flush_requests(pebc))
+			SSDFS_DBG("get next create request\n");
+		else
+			goto request_commit_log_now;
+		break;
+
+	case SSDFS_METADATA_GOING_FLUSHING:
+request_commit_log_now:
+		req = ssdfs_request_alloc();
+		if (IS_ERR_OR_NULL(req)) {
+			err = (req == NULL ? -ENOMEM : PTR_ERR(req));
+			SSDFS_ERR("fail to allocate request: "
+				  "err %d\n", err);
+			*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			return err;
+		}
+
+		ssdfs_request_init(req);
+		ssdfs_get_request(req);
+
+		err = ssdfs_segment_commit_log_async2(si, SSDFS_REQ_ASYNC,
+							pebc->peb_index, req);
+		if (unlikely(err)) {
+			SSDFS_ERR("commit log request failed: "
+				  "err %d\n", err);
+			ssdfs_put_request(req);
+			ssdfs_request_free(req);
+			*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			return err;
+		}
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("request commit log now\n");
+#endif /* CONFIG_SSDFS_DEBUG */
+		break;
+
+	default:
+		SSDFS_ERR("unexpected global FS state %#x\n",
+			  state);
+		*thread_state = SSDFS_FLUSH_THREAD_ERROR;
+		return -ERANGE;
+	}
+
+	*thread_state = SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST;
+	return 0;
+}
+
+static inline
+int ssdfs_check_peb_init_state(u64 seg_id, u64 peb_id, int state,
+				struct completion *init_end)
+{
+	int res;
+
+	if (peb_id >= U64_MAX ||
+	    state == SSDFS_PEB_OBJECT_INITIALIZED ||
+	    !init_end) {
+		/* do nothing */
+		return 0;
+	}
+
+	res = wait_for_completion_timeout(init_end, SSDFS_DEFAULT_TIMEOUT);
+	if (res == 0) {
+		SSDFS_ERR("PEB init failed: "
+			  "seg %llu, peb %llu\n",
+			  seg_id, peb_id);
+		return -ERANGE;
+	}
+
+	return 0;
+}
+
+static inline
+int ssdfs_check_src_peb_init_state(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_peb_info *pebi = NULL;
+	struct completion *init_end = NULL;
+	u64 peb_id = U64_MAX;
+	int state = SSDFS_PEB_OBJECT_UNKNOWN_STATE;
+
+	down_read(&pebc->lock);
+	pebi = pebc->src_peb;
+	if (pebi) {
+		init_end = &pebi->init_end;
+		peb_id = pebi->peb_id;
+		state = atomic_read(&pebi->state);
+	}
+	up_read(&pebc->lock);
+
+	return ssdfs_check_peb_init_state(pebc->parent_si->seg_id,
+					  peb_id, state, init_end);
+}
+
+static inline
+int ssdfs_check_dst_peb_init_state(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_peb_info *pebi = NULL;
+	struct completion *init_end = NULL;
+	u64 peb_id = U64_MAX;
+	int state = SSDFS_PEB_OBJECT_UNKNOWN_STATE;
+
+	down_read(&pebc->lock);
+	pebi = pebc->dst_peb;
+	if (pebi) {
+		init_end = &pebi->init_end;
+		peb_id = pebi->peb_id;
+		state = atomic_read(&pebi->state);
+	}
+	up_read(&pebc->lock);
+
+	return ssdfs_check_peb_init_state(pebc->parent_si->seg_id,
+					  peb_id, state, init_end);
+}
+
+static inline
+int ssdfs_check_peb_container_init_state(struct ssdfs_peb_container *pebc)
+{
+	int err;
+
+	err = ssdfs_check_src_peb_init_state(pebc);
+	if (!err)
+		err = ssdfs_check_dst_peb_init_state(pebc);
+
+	return err;
+}
+
+/*
+ * ssdfs_peb_flush_thread_func() - main fuction of flush thread
+ * @data: pointer on data object
+ *
+ * This function is main fuction of flush thread.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EINVAL     - invalid input.
+ */
+int ssdfs_peb_flush_thread_func(void *data)
+{
+	struct ssdfs_peb_container *pebc = data;
+	struct ssdfs_segment_info *si = pebc->parent_si;
+	struct ssdfs_fs_info *fsi = si->fsi;
+	struct ssdfs_peb_mapping_table *maptbl = fsi->maptbl;
+	wait_queue_head_t *wait_queue;
+	struct ssdfs_segment_request *req;
+	struct ssdfs_segment_request *postponed_req = NULL;
+	struct ssdfs_peb_info *pebi = NULL;
+	u64 peb_id = U64_MAX;
+	int state;
+	int thread_state = SSDFS_FLUSH_THREAD_NEED_CREATE_LOG;
+	__le64 cur_segs[SSDFS_CUR_SEGS_COUNT];
+	size_t size = sizeof(__le64) * SSDFS_CUR_SEGS_COUNT;
+	bool is_peb_exhausted = false;
+	bool is_peb_ready_to_exhaust = false;
+	bool peb_has_dirty_pages = false;
+	bool has_partial_empty_log = false;
+	bool skip_finish_flush_request = false;
+	bool need_create_log = true;
+	bool has_migration_check_requested = false;
+	bool has_extent_been_invalidated = false;
+	bool is_user_data = false;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	if (!pebc) {
+		SSDFS_ERR("pointer on PEB container is NULL\n");
+		BUG();
+	}
+
+	SSDFS_DBG("flush thread: seg %llu, peb_index %u\n",
+		  pebc->parent_si->seg_id, pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	wait_queue = &pebc->parent_si->wait_queue[SSDFS_PEB_FLUSH_THREAD];
+
+repeat:
+	if (err)
+		thread_state = SSDFS_FLUSH_THREAD_ERROR;
+
+	if (thread_state != SSDFS_FLUSH_THREAD_ERROR &&
+	    thread_state != SSDFS_FLUSH_THREAD_FREE_SPACE_ABSENT) {
+		if (fsi->sb->s_flags & SB_RDONLY)
+			thread_state = SSDFS_FLUSH_THREAD_RO_STATE;
+	}
+
+next_partial_step:
+	switch (thread_state) {
+	case SSDFS_FLUSH_THREAD_ERROR:
+		BUG_ON(err == 0);
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("[FLUSH THREAD STATE] ERROR\n");
+		SSDFS_DBG("thread after-error state: "
+			  "seg %llu, peb_index %u, err %d\n",
+			  pebc->parent_si->seg_id, pebc->peb_index, err);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (have_flush_requests(pebc)) {
+			ssdfs_requests_queue_remove_all(&pebc->update_rq,
+							-EROFS);
+		}
+
+		if (is_peb_joined_into_create_requests_queue(pebc))
+			ssdfs_peb_find_next_log_creation_thread(pebc);
+
+		/*
+		 * Check that we've delegated log creation role.
+		 * Otherwise, simply forget about creation queue.
+		 */
+		if (is_peb_joined_into_create_requests_queue(pebc)) {
+			spin_lock(&pebc->crq_ptr_lock);
+			ssdfs_requests_queue_remove_all(pebc->create_rq,
+							-EROFS);
+			spin_unlock(&pebc->crq_ptr_lock);
+
+			ssdfs_peb_forget_create_requests_queue(pebc);
+		}
+
+check_necessity_to_stop_thread:
+		if (kthread_should_stop()) {
+			struct completion *ptr;
+
+stop_flush_thread:
+			ptr = &pebc->thread[SSDFS_PEB_FLUSH_THREAD].full_stop;
+			complete_all(ptr);
+			return err;
+		} else
+			goto sleep_failed_flush_thread;
+		break;
+
+	case SSDFS_FLUSH_THREAD_FREE_SPACE_ABSENT:
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("[FLUSH THREAD STATE] FREE SPACE ABSENT: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (is_peb_joined_into_create_requests_queue(pebc)) {
+			err = ssdfs_peb_find_next_log_creation_thread(pebc);
+			if (err == -ENOSPC)
+				err = 0;
+			else if (unlikely(err)) {
+				SSDFS_WARN("fail to delegate log creation role:"
+					   " seg %llu, peb_index %u, err %d\n",
+					   pebc->parent_si->seg_id,
+					   pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+		}
+
+		/*
+		 * Check that we've delegated log creation role.
+		 * Otherwise, simply forget about creation queue.
+		 */
+		if (is_peb_joined_into_create_requests_queue(pebc)) {
+			spin_lock(&pebc->crq_ptr_lock);
+			ssdfs_requests_queue_remove_all(pebc->create_rq,
+							-EROFS);
+			spin_unlock(&pebc->crq_ptr_lock);
+
+			ssdfs_peb_forget_create_requests_queue(pebc);
+		}
+
+		if (err == -ENOSPC && have_flush_requests(pebc)) {
+			err = 0;
+
+			if (is_peb_under_migration(pebc)) {
+				err = __ssdfs_peb_finish_migration(pebc);
+				if (unlikely(err))
+					goto finish_process_free_space_absence;
+			}
+
+			err = ssdfs_peb_start_migration(pebc);
+			if (unlikely(err))
+				goto finish_process_free_space_absence;
+
+			pebi = ssdfs_get_current_peb_locked(pebc);
+			if (IS_ERR_OR_NULL(pebi)) {
+				err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+				SSDFS_ERR("fail to get PEB object: "
+					  "seg %llu, peb_index %u, err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto finish_process_free_space_absence;
+			}
+
+			if (is_ssdfs_maptbl_going_to_be_destroyed(maptbl)) {
+				SSDFS_WARN("seg %llu, peb_index %u\n",
+					   pebc->parent_si->seg_id,
+					   pebc->peb_index);
+			}
+
+			err = ssdfs_peb_container_change_state(pebc);
+			ssdfs_unlock_current_peb(pebc);
+
+finish_process_free_space_absence:
+			if (unlikely(err)) {
+				SSDFS_WARN("fail to start PEB's migration: "
+					   "seg %llu, peb_index %u, err %d\n",
+					   pebc->parent_si->seg_id,
+					   pebc->peb_index, err);
+			       ssdfs_requests_queue_remove_all(&pebc->update_rq,
+								-ENOSPC);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			thread_state = SSDFS_FLUSH_THREAD_NEED_CREATE_LOG;
+			goto next_partial_step;
+		} else if (have_flush_requests(pebc)) {
+			ssdfs_requests_queue_remove_all(&pebc->update_rq,
+							-ENOSPC);
+		}
+
+		goto check_necessity_to_stop_thread;
+		break;
+
+	case SSDFS_FLUSH_THREAD_RO_STATE:
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("[FLUSH THREAD STATE] READ-ONLY STATE: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		err = ssdfs_prepare_current_segment_ids(fsi, cur_segs, size);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to prepare current segments IDs: "
+				  "err %d\n",
+				  err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto repeat;
+		}
+
+		pebi = ssdfs_get_current_peb_locked(pebc);
+		if (IS_ERR_OR_NULL(pebi)) {
+			err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+			SSDFS_ERR("fail to get PEB object: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto repeat;
+		}
+
+		if (!(fsi->sb->s_flags & SB_RDONLY)) {
+			/*
+			 * File system state was changed.
+			 * Now file system has RW state.
+			 */
+			if (fsi->fs_state == SSDFS_ERROR_FS) {
+				ssdfs_peb_current_log_lock(pebi);
+				if (ssdfs_peb_has_dirty_pages(pebi))
+					ssdfs_peb_clear_current_log_pages(pebi);
+				ssdfs_peb_current_log_unlock(pebi);
+				ssdfs_unlock_current_peb(pebc);
+				goto check_necessity_to_stop_thread;
+			} else {
+				state = ssdfs_peb_get_current_log_state(pebc);
+				if (state <= SSDFS_LOG_UNKNOWN ||
+				    state >= SSDFS_LOG_STATE_MAX) {
+					err = -ERANGE;
+					SSDFS_WARN("invalid log state: "
+						   "state %#x\n",
+						   state);
+					ssdfs_unlock_current_peb(pebc);
+					goto repeat;
+				}
+
+				if (state != SSDFS_LOG_CREATED) {
+					thread_state =
+					    SSDFS_FLUSH_THREAD_NEED_CREATE_LOG;
+					ssdfs_unlock_current_peb(pebc);
+					goto next_partial_step;
+				}
+
+				thread_state =
+					SSDFS_FLUSH_THREAD_CHECK_STOP_CONDITION;
+				ssdfs_unlock_current_peb(pebc);
+				goto repeat;
+			}
+		}
+
+		ssdfs_peb_current_log_lock(pebi);
+		if (ssdfs_peb_has_dirty_pages(pebi)) {
+			if (fsi->fs_state == SSDFS_ERROR_FS)
+				ssdfs_peb_clear_current_log_pages(pebi);
+			else {
+				mutex_lock(&pebc->migration_lock);
+				err = ssdfs_peb_commit_log(pebi,
+							   cur_segs, size);
+				mutex_unlock(&pebc->migration_lock);
+
+				if (unlikely(err)) {
+					SSDFS_CRIT("fail to commit log: "
+						   "seg %llu, peb_index %u, "
+						   "err %d\n",
+						   pebc->parent_si->seg_id,
+						   pebc->peb_index,
+						   err);
+					ssdfs_peb_clear_current_log_pages(pebi);
+					ssdfs_peb_clear_cache_dirty_pages(pebi);
+					thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				}
+			}
+		}
+		ssdfs_peb_current_log_unlock(pebi);
+
+		if (!err) {
+			if (is_ssdfs_maptbl_going_to_be_destroyed(maptbl)) {
+				SSDFS_WARN("seg %llu, peb_index %u\n",
+					   pebc->parent_si->seg_id,
+					   pebc->peb_index);
+			}
+
+			err = ssdfs_peb_container_change_state(pebc);
+			if (unlikely(err)) {
+				SSDFS_CRIT("fail to change peb state: "
+					  "err %d\n", err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			}
+		}
+
+		ssdfs_unlock_current_peb(pebc);
+
+		goto check_necessity_to_stop_thread;
+		break;
+
+	case SSDFS_FLUSH_THREAD_NEED_CREATE_LOG:
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("[FLUSH THREAD STATE] NEED CREATE LOG: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#else
+		SSDFS_DBG("[FLUSH THREAD STATE] NEED CREATE LOG: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		if (fsi->sb->s_flags & SB_RDONLY) {
+			thread_state = SSDFS_FLUSH_THREAD_RO_STATE;
+			goto repeat;
+		}
+
+		if (kthread_should_stop()) {
+			if (have_flush_requests(pebc)) {
+				SSDFS_WARN("discovered unprocessed requests: "
+					   "seg %llu, peb_index %u\n",
+					   pebc->parent_si->seg_id,
+					   pebc->peb_index);
+			} else {
+				thread_state =
+				    SSDFS_FLUSH_THREAD_CHECK_STOP_CONDITION;
+				goto repeat;
+			}
+		}
+
+		if (!has_ssdfs_segment_blk_bmap_initialized(&si->blk_bmap,
+							    pebc)) {
+			err = ssdfs_segment_blk_bmap_wait_init_end(&si->blk_bmap,
+								   pebc);
+			if (unlikely(err)) {
+				SSDFS_ERR("block bitmap init failed: "
+					  "seg %llu, peb_index %u, err %d\n",
+					  si->seg_id, pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+		}
+
+		err = ssdfs_check_peb_container_init_state(pebc);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to check init state: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  si->seg_id, pebc->peb_index, err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto repeat;
+		}
+
+		pebi = ssdfs_get_current_peb_locked(pebc);
+		if (IS_ERR_OR_NULL(pebi)) {
+			err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+			SSDFS_ERR("fail to get PEB object: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto repeat;
+		}
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("seg %llu, peb_index %u, migration_state %#x\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index,
+			  atomic_read(&pebc->migration_state));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		ssdfs_peb_current_log_lock(pebi);
+		peb_id = pebi->peb_id;
+		peb_has_dirty_pages = ssdfs_peb_has_dirty_pages(pebi);
+		need_create_log = peb_has_dirty_pages ||
+					have_flush_requests(pebc);
+		is_peb_exhausted = is_ssdfs_peb_exhausted(fsi, pebi);
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("peb_id %llu, ssdfs_peb_has_dirty_pages %#x, "
+			  "have_flush_requests %#x, need_create_log %#x, "
+			  "is_peb_exhausted %#x\n",
+			  peb_id, peb_has_dirty_pages,
+			  have_flush_requests(pebc),
+			  need_create_log, is_peb_exhausted);
+#endif /* CONFIG_SSDFS_DEBUG */
+		ssdfs_peb_current_log_unlock(pebi);
+
+		if (!need_create_log) {
+			ssdfs_unlock_current_peb(pebc);
+			thread_state = SSDFS_FLUSH_THREAD_NEED_CREATE_LOG;
+			goto sleep_flush_thread;
+		}
+
+		if (has_commit_log_now_requested(pebc) &&
+		    is_create_requests_queue_empty(pebc)) {
+			/*
+			 * If no other commands in the queue
+			 * then ignore the log creation now.
+			 */
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("Don't create log: "
+				  "COMMIT_LOG_NOW command: "
+				  "seg %llu, peb_index %u\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+			ssdfs_unlock_current_peb(pebc);
+			thread_state = SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+			goto repeat;
+		}
+
+		if (has_start_migration_now_requested(pebc)) {
+			/*
+			 * No necessity to create log
+			 * for START_MIGRATION_NOW command.
+			 */
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("Don't create log: "
+				  "START_MIGRATION_NOW command: "
+				  "seg %llu, peb_index %u\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+			ssdfs_unlock_current_peb(pebc);
+			thread_state = SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+			goto repeat;
+		}
+
+		if (is_peb_exhausted) {
+			ssdfs_unlock_current_peb(pebc);
+
+			if (is_ssdfs_maptbl_under_flush(fsi)) {
+				if (is_ssdfs_peb_containing_user_data(pebc)) {
+					/*
+					 * Continue logic for user data.
+					 */
+#ifdef CONFIG_SSDFS_DEBUG
+					SSDFS_DBG("ignore mapping table's "
+						  "flush for user data\n");
+#endif /* CONFIG_SSDFS_DEBUG */
+				} else if (have_flush_requests(pebc)) {
+					SSDFS_ERR("maptbl is flushing: "
+						  "unprocessed requests: "
+						  "seg %llu, peb %llu\n",
+						  pebc->parent_si->seg_id,
+						  peb_id);
+
+#ifdef CONFIG_SSDFS_DEBUG
+					ssdfs_peb_check_update_queue(pebc);
+#endif /* CONFIG_SSDFS_DEBUG */
+					BUG();
+				} else {
+					SSDFS_ERR("maptbl is flushing\n");
+					thread_state =
+					    SSDFS_FLUSH_THREAD_NEED_CREATE_LOG;
+					goto sleep_flush_thread;
+				}
+			}
+
+			if (is_peb_under_migration(pebc)) {
+				err = __ssdfs_peb_finish_migration(pebc);
+				if (unlikely(err)) {
+					SSDFS_ERR("fail to finish migration: "
+						  "seg %llu, peb_index %u, "
+						  "err %d\n",
+						  pebc->parent_si->seg_id,
+						  pebc->peb_index, err);
+					thread_state = SSDFS_FLUSH_THREAD_ERROR;
+					goto repeat;
+				}
+			}
+
+			if (!has_peb_migration_done(pebc)) {
+				SSDFS_ERR("migration is not finished: "
+					  "seg %llu, peb_index %u, "
+					  "err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			err = ssdfs_peb_start_migration(pebc);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to start migration: "
+					  "seg %llu, peb_index %u, "
+					  "err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			pebi = ssdfs_get_current_peb_locked(pebc);
+			if (IS_ERR_OR_NULL(pebi)) {
+				err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+				SSDFS_ERR("fail to get PEB object: "
+					  "seg %llu, peb_index %u, err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			if (is_ssdfs_maptbl_going_to_be_destroyed(maptbl)) {
+				SSDFS_WARN("seg %llu, peb_index %u\n",
+					   pebc->parent_si->seg_id,
+					   pebc->peb_index);
+			}
+
+			err = ssdfs_peb_container_change_state(pebc);
+			if (unlikely(err)) {
+				ssdfs_unlock_current_peb(pebc);
+				SSDFS_ERR("fail to change peb state: "
+					  "err %d\n", err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+		}
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("is_peb_under_migration %#x, "
+			  "has_peb_migration_done %#x\n",
+			  is_peb_under_migration(pebc),
+			  has_peb_migration_done(pebc));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (is_peb_under_migration(pebc) &&
+		    has_peb_migration_done(pebc)) {
+			ssdfs_unlock_current_peb(pebc);
+
+			err = __ssdfs_peb_finish_migration(pebc);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to finish migration: "
+					  "seg %llu, peb_index %u, "
+					  "err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			pebi = ssdfs_get_current_peb_locked(pebc);
+			if (IS_ERR_OR_NULL(pebi)) {
+				err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+				SSDFS_ERR("fail to get PEB object: "
+					  "seg %llu, peb_index %u, err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			if (is_ssdfs_maptbl_going_to_be_destroyed(maptbl)) {
+				SSDFS_WARN("seg %llu, peb_index %u\n",
+					   pebc->parent_si->seg_id,
+					   pebc->peb_index);
+			}
+
+			err = ssdfs_peb_container_change_state(pebc);
+			if (unlikely(err)) {
+				ssdfs_unlock_current_peb(pebc);
+				SSDFS_ERR("fail to change peb state: "
+					  "err %d\n", err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+		}
+
+		mutex_lock(&pebc->migration_lock);
+		err = ssdfs_peb_create_log(pebi);
+		mutex_unlock(&pebc->migration_lock);
+		ssdfs_unlock_current_peb(pebc);
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("finished: err %d\n", err);
+#else
+		SSDFS_DBG("finished: err %d\n", err);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		if (err == -EAGAIN) {
+			if (kthread_should_stop())
+				goto fail_create_log;
+			else {
+				err = 0;
+				goto sleep_flush_thread;
+			}
+		} else if (err == -ENOSPC) {
+			err = 0;
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("PEB hasn't free space: "
+				  "seg %llu, peb_index %u\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+			thread_state = SSDFS_FLUSH_THREAD_FREE_SPACE_ABSENT;
+		} else if (unlikely(err)) {
+fail_create_log:
+			SSDFS_CRIT("fail to create log: "
+				   "seg %llu, peb_index %u, err %d\n",
+				   pebc->parent_si->seg_id,
+				   pebc->peb_index, err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+		} else
+			thread_state = SSDFS_FLUSH_THREAD_CHECK_STOP_CONDITION;
+		goto repeat;
+		break;
+
+	case SSDFS_FLUSH_THREAD_CHECK_STOP_CONDITION:
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("[FLUSH THREAD STATE] CHECK NECESSITY TO STOP: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#else
+		SSDFS_DBG("[FLUSH THREAD STATE] CHECK NECESSITY TO STOP: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		if (kthread_should_stop()) {
+			if (have_flush_requests(pebc)) {
+				state = ssdfs_peb_get_current_log_state(pebc);
+				if (state <= SSDFS_LOG_UNKNOWN ||
+				    state >= SSDFS_LOG_STATE_MAX) {
+					err = -ERANGE;
+					SSDFS_WARN("invalid log state: "
+						   "state %#x\n",
+						   state);
+					goto repeat;
+				}
+
+				if (state != SSDFS_LOG_CREATED) {
+					thread_state =
+					    SSDFS_FLUSH_THREAD_NEED_CREATE_LOG;
+					goto next_partial_step;
+				} else
+					goto process_flush_requests;
+			}
+
+			err = ssdfs_prepare_current_segment_ids(fsi,
+								cur_segs,
+								size);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to prepare current seg IDs: "
+					  "err %d\n",
+					  err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			pebi = ssdfs_get_current_peb_locked(pebc);
+			if (IS_ERR_OR_NULL(pebi)) {
+				err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+				SSDFS_ERR("fail to get PEB object: "
+					  "seg %llu, peb_index %u, err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			ssdfs_peb_current_log_lock(pebi);
+			mutex_lock(&pebc->migration_lock);
+			err = ssdfs_peb_commit_log_on_thread_stop(pebi,
+								  cur_segs,
+								  size);
+			mutex_unlock(&pebc->migration_lock);
+			ssdfs_peb_current_log_unlock(pebi);
+
+			if (unlikely(err)) {
+				SSDFS_CRIT("fail to commit log: "
+					   "seg %llu, peb_index %u, err %d\n",
+					   pebc->parent_si->seg_id,
+					   pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			}
+
+			ssdfs_unlock_current_peb(pebc);
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+			SSDFS_ERR("finished: err %d\n", err);
+#else
+			SSDFS_DBG("finished: err %d\n", err);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+			goto stop_flush_thread;
+		} else {
+process_flush_requests:
+			state = ssdfs_peb_get_current_log_state(pebc);
+			if (state <= SSDFS_LOG_UNKNOWN ||
+			    state >= SSDFS_LOG_STATE_MAX) {
+				err = -ERANGE;
+				SSDFS_WARN("invalid log state: "
+					   "state %#x\n",
+					   state);
+				goto repeat;
+			}
+
+			if (state != SSDFS_LOG_CREATED) {
+				thread_state =
+					SSDFS_FLUSH_THREAD_NEED_CREATE_LOG;
+			} else {
+				thread_state =
+					SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST;
+			}
+			goto repeat;
+		}
+		break;
+
+	case SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST:
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("[FLUSH THREAD STATE] GET CREATE REQUEST: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#else
+		SSDFS_DBG("[FLUSH THREAD STATE] GET CREATE REQUEST: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		if (!have_flush_requests(pebc)) {
+			thread_state = SSDFS_FLUSH_THREAD_CHECK_STOP_CONDITION;
+			if (kthread_should_stop())
+				goto repeat;
+			else
+				goto sleep_flush_thread;
+		}
+
+		if (!is_peb_joined_into_create_requests_queue(pebc) ||
+		    is_create_requests_queue_empty(pebc)) {
+			thread_state = SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+			goto repeat;
+		}
+
+		spin_lock(&pebc->crq_ptr_lock);
+		err = ssdfs_requests_queue_remove_first(pebc->create_rq, &req);
+		spin_unlock(&pebc->crq_ptr_lock);
+
+		if (err == -ENODATA) {
+			SSDFS_DBG("empty create queue\n");
+			err = 0;
+			thread_state = SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+			goto repeat;
+		} else if (err == -ENOENT) {
+			SSDFS_WARN("request queue contains NULL request\n");
+			err = 0;
+			thread_state = SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+			goto repeat;
+		} else if (unlikely(err < 0)) {
+			SSDFS_CRIT("fail to get request from create queue: "
+				   "err %d\n",
+				   err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto repeat;
+		}
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("req->private.class %#x, req->private.cmd %#x\n",
+			  req->private.class, req->private.cmd);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		thread_state = SSDFS_FLUSH_THREAD_PROCESS_CREATE_REQUEST;
+		goto next_partial_step;
+		break;
+
+	case SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST:
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("[FLUSH THREAD STATE] GET UPDATE REQUEST: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#else
+		SSDFS_DBG("[FLUSH THREAD STATE] GET UPDATE REQUEST: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		if (is_ssdfs_requests_queue_empty(&pebc->update_rq)) {
+			if (have_flush_requests(pebc)) {
+				thread_state =
+					SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST;
+				goto next_partial_step;
+			} else {
+				thread_state =
+					SSDFS_FLUSH_THREAD_CHECK_STOP_CONDITION;
+				goto sleep_flush_thread;
+			}
+		}
+
+		err = ssdfs_requests_queue_remove_first(&pebc->update_rq, &req);
+		if (err == -ENODATA) {
+			SSDFS_DBG("empty update queue\n");
+			err = 0;
+			thread_state = SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+			goto repeat;
+		} else if (err == -ENOENT) {
+			SSDFS_WARN("request queue contains NULL request\n");
+			err = 0;
+			thread_state = SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+			goto repeat;
+		} else if (unlikely(err < 0)) {
+			SSDFS_CRIT("fail to get request from update queue: "
+				   "err %d\n",
+				   err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto repeat;
+		}
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("req->private.class %#x, req->private.cmd %#x\n",
+			  req->private.class, req->private.cmd);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		thread_state = SSDFS_FLUSH_THREAD_PROCESS_UPDATE_REQUEST;
+		goto next_partial_step;
+		break;
+
+	case SSDFS_FLUSH_THREAD_PROCESS_CREATE_REQUEST:
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("[FLUSH THREAD STATE] PROCESS CREATE REQUEST: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+		SSDFS_ERR("req->private.class %#x, req->private.cmd %#x\n",
+			  req->private.class, req->private.cmd);
+#else
+		SSDFS_DBG("[FLUSH THREAD STATE] PROCESS CREATE REQUEST: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+		SSDFS_DBG("req->private.class %#x, req->private.cmd %#x\n",
+			  req->private.class, req->private.cmd);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		is_user_data = is_ssdfs_peb_containing_user_data(pebc);
+
+		if (!has_ssdfs_segment_blk_bmap_initialized(&si->blk_bmap,
+							    pebc)) {
+			err = ssdfs_segment_blk_bmap_wait_init_end(&si->blk_bmap,
+								   pebc);
+			if (unlikely(err)) {
+				SSDFS_ERR("block bitmap init failed: "
+					  "seg %llu, peb_index %u, err %d\n",
+					  si->seg_id, pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+		}
+
+		pebi = ssdfs_get_current_peb_locked(pebc);
+		if (IS_ERR_OR_NULL(pebi)) {
+			err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+			SSDFS_ERR("fail to get PEB object: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto repeat;
+		}
+
+		ssdfs_peb_current_log_lock(pebi);
+
+		mutex_lock(&pebc->migration_lock);
+		err = ssdfs_process_create_request(pebi, req);
+		mutex_unlock(&pebc->migration_lock);
+
+		if (err == -ENOSPC) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to process create request: "
+				  "seg %llu, peb_index %u\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+			ssdfs_finish_flush_request(pebc, req, wait_queue, err);
+			thread_state = SSDFS_FLUSH_THREAD_FREE_SPACE_ABSENT;
+			goto finish_create_request_processing;
+		} else if (err == -EAGAIN) {
+			err = 0;
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to process create request : "
+				  "seg %llu, peb_index %u\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+			spin_lock(&pebc->crq_ptr_lock);
+			ssdfs_requests_queue_add_head(pebc->create_rq, req);
+			spin_unlock(&pebc->crq_ptr_lock);
+			req = NULL;
+			skip_finish_flush_request = true;
+			thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+			goto finish_create_request_processing;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to process create request: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err);
+			ssdfs_finish_flush_request(pebc, req, wait_queue, err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto finish_create_request_processing;
+		}
+
+		if (req->private.type == SSDFS_REQ_SYNC) {
+			thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+			goto finish_create_request_processing;
+		}
+
+		/* SSDFS_REQ_ASYNC */
+		if (is_full_log_ready(pebi)) {
+			skip_finish_flush_request = false;
+			thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+			goto finish_create_request_processing;
+		} else if (should_partial_log_being_commited(pebi)) {
+			skip_finish_flush_request = false;
+			thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+			goto finish_create_request_processing;
+		} else if (!have_flush_requests(pebc)) {
+			if (need_wait_next_create_data_request(pebi)) {
+				ssdfs_account_user_data_flush_request(si);
+				ssdfs_finish_flush_request(pebc, req,
+							   wait_queue, err);
+				ssdfs_peb_current_log_unlock(pebi);
+				ssdfs_unlock_current_peb(pebc);
+
+				err = wait_next_create_request(pebc,
+							     &thread_state);
+				ssdfs_forget_user_data_flush_request(si);
+				skip_finish_flush_request = false;
+				goto finish_wait_next_create_request;
+			} else if (is_user_data) {
+				skip_finish_flush_request = false;
+				thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+				goto finish_create_request_processing;
+			} else {
+				goto get_next_update_request;
+			}
+		} else {
+get_next_update_request:
+			ssdfs_finish_flush_request(pebc, req,
+						   wait_queue, err);
+			thread_state =
+				SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+		}
+
+finish_create_request_processing:
+		ssdfs_peb_current_log_unlock(pebi);
+		ssdfs_unlock_current_peb(pebc);
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("finished\n");
+#else
+		SSDFS_DBG("finished\n");
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+finish_wait_next_create_request:
+		if (thread_state == SSDFS_FLUSH_THREAD_COMMIT_LOG)
+			goto next_partial_step;
+		else
+			goto repeat;
+		break;
+
+	case SSDFS_FLUSH_THREAD_PROCESS_UPDATE_REQUEST:
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("[FLUSH THREAD STATE] PROCESS UPDATE REQUEST: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+		SSDFS_ERR("req->private.class %#x, req->private.cmd %#x\n",
+			  req->private.class, req->private.cmd);
+#else
+		SSDFS_DBG("[FLUSH THREAD STATE] PROCESS UPDATE REQUEST: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+		SSDFS_DBG("req->private.class %#x, req->private.cmd %#x\n",
+			  req->private.class, req->private.cmd);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		is_user_data = is_ssdfs_peb_containing_user_data(pebc);
+
+		if (!has_ssdfs_segment_blk_bmap_initialized(&si->blk_bmap,
+							    pebc)) {
+			err = ssdfs_segment_blk_bmap_wait_init_end(&si->blk_bmap,
+								   pebc);
+			if (unlikely(err)) {
+				SSDFS_ERR("block bitmap init failed: "
+					  "seg %llu, peb_index %u, err %d\n",
+					  si->seg_id, pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+		}
+
+		pebi = ssdfs_get_current_peb_locked(pebc);
+		if (IS_ERR_OR_NULL(pebi)) {
+			err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+			SSDFS_ERR("fail to get PEB object: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto repeat;
+		}
+
+		ssdfs_peb_current_log_lock(pebi);
+
+		mutex_lock(&pebc->migration_lock);
+		err = ssdfs_process_update_request(pebi, req);
+		mutex_unlock(&pebc->migration_lock);
+
+		if (err == -EAGAIN) {
+			err = 0;
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to process update request : "
+				  "seg %llu, peb_index %u\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+			ssdfs_requests_queue_add_head(&pebc->update_rq, req);
+			req = NULL;
+			skip_finish_flush_request = true;
+			thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+			goto finish_update_request_processing;
+		} else if (err == -ENOENT &&
+			   req->private.cmd == SSDFS_BTREE_NODE_DIFF) {
+			err = 0;
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to process update request : "
+				  "seg %llu, peb_index %u\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+			req = NULL;
+			thread_state = SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+			goto finish_update_request_processing;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to process update request: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err);
+			ssdfs_finish_flush_request(pebc, req, wait_queue, err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto finish_update_request_processing;
+		}
+
+		switch (req->private.cmd) {
+		case SSDFS_EXTENT_WAS_INVALIDATED:
+			/* log has to be committed */
+			has_extent_been_invalidated = true;
+			thread_state =
+				SSDFS_FLUSH_THREAD_PROCESS_INVALIDATED_EXTENT;
+			goto finish_update_request_processing;
+
+		case SSDFS_START_MIGRATION_NOW:
+			thread_state = SSDFS_FLUSH_THREAD_START_MIGRATION_NOW;
+			goto finish_update_request_processing;
+
+		case SSDFS_COMMIT_LOG_NOW:
+			if (has_commit_log_now_requested(pebc)) {
+#ifdef CONFIG_SSDFS_DEBUG
+				SSDFS_DBG("Ignore current COMMIT_LOG_NOW: "
+					  "seg %llu, peb_index %u\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+				ssdfs_finish_flush_request(pebc, req,
+							   wait_queue, err);
+				thread_state =
+					SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST;
+			} else if (have_flush_requests(pebc)) {
+				ssdfs_requests_queue_add_tail(&pebc->update_rq,
+								req);
+				req = NULL;
+
+				state = atomic_read(&pebi->current_log.state);
+				if (state == SSDFS_LOG_COMMITTED) {
+					thread_state =
+					  SSDFS_FLUSH_THREAD_NEED_CREATE_LOG;
+				} else {
+					thread_state =
+					  SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST;
+				}
+			} else if (has_extent_been_invalidated) {
+				if (is_user_data_pages_invalidated(si) &&
+				    is_regular_fs_operations(pebc)) {
+					ssdfs_account_user_data_flush_request(si);
+					ssdfs_finish_flush_request(pebc, req,
+							   wait_queue, err);
+					ssdfs_peb_current_log_unlock(pebi);
+					ssdfs_unlock_current_peb(pebc);
+
+					err = wait_next_invalidate_request(pebc,
+								 &thread_state);
+					ssdfs_forget_user_data_flush_request(si);
+					skip_finish_flush_request = false;
+					goto finish_wait_next_data_request;
+				} else {
+					thread_state =
+						SSDFS_FLUSH_THREAD_COMMIT_LOG;
+				}
+			} else if (ssdfs_peb_has_dirty_pages(pebi)) {
+				if (need_wait_next_create_data_request(pebi)) {
+					ssdfs_account_user_data_flush_request(si);
+					ssdfs_finish_flush_request(pebc, req,
+							   wait_queue, err);
+					ssdfs_peb_current_log_unlock(pebi);
+					ssdfs_unlock_current_peb(pebc);
+
+					err = wait_next_create_request(pebc,
+								&thread_state);
+					ssdfs_forget_user_data_flush_request(si);
+					skip_finish_flush_request = false;
+					goto finish_wait_next_data_request;
+				} else if (need_wait_next_update_request(pebi)) {
+					ssdfs_account_user_data_flush_request(si);
+					ssdfs_finish_flush_request(pebc, req,
+							   wait_queue, err);
+					ssdfs_peb_current_log_unlock(pebi);
+					ssdfs_unlock_current_peb(pebc);
+
+					err = wait_next_update_request(pebc,
+								&thread_state);
+					ssdfs_forget_user_data_flush_request(si);
+					skip_finish_flush_request = false;
+					goto finish_wait_next_data_request;
+
+				} else {
+					thread_state =
+						SSDFS_FLUSH_THREAD_COMMIT_LOG;
+				}
+			} else {
+				ssdfs_finish_flush_request(pebc, req,
+							   wait_queue, err);
+
+				state = atomic_read(&pebi->current_log.state);
+				if (state == SSDFS_LOG_COMMITTED) {
+					thread_state =
+					  SSDFS_FLUSH_THREAD_NEED_CREATE_LOG;
+				} else {
+					thread_state =
+					  SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST;
+				}
+			}
+			goto finish_update_request_processing;
+
+		default:
+			/* do nothing */
+			break;
+		}
+
+		if (req->private.type == SSDFS_REQ_SYNC) {
+			thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+			goto finish_update_request_processing;
+		} else if (has_migration_check_requested) {
+			ssdfs_finish_flush_request(pebc, req,
+						   wait_queue, err);
+			thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+			goto finish_update_request_processing;
+		} else if (is_full_log_ready(pebi)) {
+			skip_finish_flush_request = false;
+			thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+			goto finish_update_request_processing;
+		} else if (should_partial_log_being_commited(pebi)) {
+			skip_finish_flush_request = false;
+			thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+			goto finish_update_request_processing;
+		} else if (!have_flush_requests(pebc)) {
+			if (need_wait_next_update_request(pebi)) {
+				ssdfs_account_user_data_flush_request(si);
+				ssdfs_finish_flush_request(pebc, req,
+							   wait_queue, err);
+				ssdfs_peb_current_log_unlock(pebi);
+				ssdfs_unlock_current_peb(pebc);
+
+				err = wait_next_update_request(pebc,
+							     &thread_state);
+				ssdfs_forget_user_data_flush_request(si);
+				skip_finish_flush_request = false;
+				goto finish_wait_next_data_request;
+			} else if (is_user_data &&
+				   ssdfs_peb_has_dirty_pages(pebi)) {
+				skip_finish_flush_request = false;
+				thread_state = SSDFS_FLUSH_THREAD_COMMIT_LOG;
+				goto finish_update_request_processing;
+			} else
+				goto get_next_create_request;
+		} else {
+get_next_create_request:
+			ssdfs_finish_flush_request(pebc, req, wait_queue, err);
+			thread_state = SSDFS_FLUSH_THREAD_GET_CREATE_REQUEST;
+			goto finish_update_request_processing;
+		}
+
+finish_update_request_processing:
+		ssdfs_peb_current_log_unlock(pebi);
+		ssdfs_unlock_current_peb(pebc);
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("finished\n");
+#else
+		SSDFS_DBG("finished\n");
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+finish_wait_next_data_request:
+		if (thread_state == SSDFS_FLUSH_THREAD_COMMIT_LOG ||
+		    thread_state == SSDFS_FLUSH_THREAD_START_MIGRATION_NOW) {
+			goto next_partial_step;
+		} else if (thread_state == SSDFS_FLUSH_THREAD_NEED_CREATE_LOG)
+			goto sleep_flush_thread;
+		else
+			goto repeat;
+		break;
+
+	case SSDFS_FLUSH_THREAD_PROCESS_INVALIDATED_EXTENT:
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("[FLUSH THREAD STATE] PROCESS INVALIDATED EXTENT: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#else
+		SSDFS_DBG("[FLUSH THREAD STATE] PROCESS INVALIDATED EXTENT: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		pebi = ssdfs_get_current_peb_locked(pebc);
+		if (IS_ERR_OR_NULL(pebi)) {
+			err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+			SSDFS_ERR("fail to get PEB object: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto repeat;
+		}
+
+		if (is_peb_under_migration(pebc) &&
+		    has_peb_migration_done(pebc)) {
+			ssdfs_unlock_current_peb(pebc);
+
+			err = __ssdfs_peb_finish_migration(pebc);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to finish migration: "
+					  "seg %llu, peb_index %u, "
+					  "err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			pebi = ssdfs_get_current_peb_locked(pebc);
+			if (IS_ERR_OR_NULL(pebi)) {
+				err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+				SSDFS_ERR("fail to get PEB object: "
+					  "seg %llu, peb_index %u, err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			if (is_ssdfs_maptbl_going_to_be_destroyed(maptbl)) {
+				SSDFS_WARN("seg %llu, peb_index %u\n",
+					   pebc->parent_si->seg_id,
+					   pebc->peb_index);
+			}
+
+			err = ssdfs_peb_container_change_state(pebc);
+			if (unlikely(err)) {
+				ssdfs_unlock_current_peb(pebc);
+				SSDFS_ERR("fail to change peb state: "
+					  "err %d\n", err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+		}
+
+		ssdfs_peb_current_log_lock(pebi);
+		ssdfs_finish_flush_request(pebc, req, wait_queue, err);
+		ssdfs_peb_current_log_unlock(pebi);
+
+		ssdfs_unlock_current_peb(pebc);
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("finished\n");
+#else
+		SSDFS_DBG("finished\n");
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		thread_state = SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+		goto next_partial_step;
+		break;
+
+	case SSDFS_FLUSH_THREAD_START_MIGRATION_NOW:
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("[FLUSH THREAD STATE] START MIGRATION REQUEST: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#else
+		SSDFS_DBG("[FLUSH THREAD STATE] START MIGRATION REQUEST: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		pebi = ssdfs_get_current_peb_locked(pebc);
+		if (IS_ERR_OR_NULL(pebi)) {
+			err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+			SSDFS_ERR("fail to get PEB object: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  pebc->parent_si->seg_id,
+				  pebc->peb_index, err);
+			thread_state = SSDFS_FLUSH_THREAD_ERROR;
+			goto repeat;
+		}
+
+		ssdfs_peb_current_log_lock(pebi);
+		is_peb_exhausted = is_ssdfs_peb_exhausted(fsi, pebi);
+		is_peb_ready_to_exhaust =
+			is_ssdfs_peb_ready_to_exhaust(fsi, pebi);
+		has_partial_empty_log =
+			ssdfs_peb_has_partial_empty_log(fsi, pebi);
+		ssdfs_peb_current_log_unlock(pebi);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("is_peb_exhausted %#x, "
+			  "is_peb_ready_to_exhaust %#x\n",
+			  is_peb_exhausted, is_peb_ready_to_exhaust);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (is_peb_exhausted || is_peb_ready_to_exhaust) {
+			ssdfs_unlock_current_peb(pebc);
+
+			if (is_peb_under_migration(pebc)) {
+				/*
+				 * START_MIGRATION_NOW is requested during
+				 * the flush operation of PEB mapping table,
+				 * segment bitmap or any btree. It is the first
+				 * step to initiate the migration.
+				 * Then, fragments or nodes will be flushed.
+				 * And final step is the COMMIT_LOG_NOW
+				 * request. So, it doesn't need to request
+				 * the COMMIT_LOG_NOW here.
+				 */
+				err = ssdfs_peb_finish_migration(pebc);
+				if (unlikely(err)) {
+					SSDFS_ERR("fail to finish migration: "
+						  "seg %llu, peb_index %u, "
+						  "err %d\n",
+						  pebc->parent_si->seg_id,
+						  pebc->peb_index, err);
+					thread_state = SSDFS_FLUSH_THREAD_ERROR;
+					goto process_migration_failure;
+				}
+			}
+
+			if (!has_peb_migration_done(pebc)) {
+				SSDFS_ERR("migration is not finished: "
+					  "seg %llu, peb_index %u, "
+					  "err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto process_migration_failure;
+			}
+
+			err = ssdfs_peb_start_migration(pebc);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to start migration: "
+					  "seg %llu, peb_index %u, "
+					  "err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto process_migration_failure;
+			}
+
+process_migration_failure:
+			pebi = ssdfs_get_current_peb_locked(pebc);
+			if (err) {
+				if (IS_ERR_OR_NULL(pebi)) {
+					thread_state = SSDFS_FLUSH_THREAD_ERROR;
+					goto repeat;
+				}
+
+				ssdfs_peb_current_log_lock(pebi);
+				ssdfs_finish_flush_request(pebc, req,
+							   wait_queue,
+							   err);
+				ssdfs_peb_current_log_unlock(pebi);
+				ssdfs_unlock_current_peb(pebc);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			} else if (IS_ERR_OR_NULL(pebi)) {
+				err = pebi == NULL ? -ERANGE : PTR_ERR(pebi);
+				SSDFS_ERR("fail to get PEB object: "
+					  "seg %llu, peb_index %u, err %d\n",
+					  pebc->parent_si->seg_id,
+					  pebc->peb_index, err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+
+			if (is_ssdfs_maptbl_going_to_be_destroyed(maptbl)) {
+				SSDFS_WARN("seg %llu, peb_index %u\n",
+					   pebc->parent_si->seg_id,
+					   pebc->peb_index);
+			}
+
+			err = ssdfs_peb_container_change_state(pebc);
+			if (unlikely(err)) {
+				ssdfs_unlock_current_peb(pebc);
+				SSDFS_ERR("fail to change peb state: "
+					  "err %d\n", err);
+				thread_state = SSDFS_FLUSH_THREAD_ERROR;
+				goto repeat;
+			}
+		} else if (has_partial_empty_log) {
+			/*
+			 * TODO: it will need to implement logic here
+			 */
+			SSDFS_WARN("log is partially empty\n");
+		}
+
+		ssdfs_peb_current_log_lock(pebi);
+		ssdfs_finish_flush_request(pebc, req, wait_queue, err);
+		ssdfs_peb_current_log_unlock(pebi);
+		ssdfs_unlock_current_peb(pebc);
+
+		state = ssdfs_peb_get_current_log_state(pebc);
+		if (state <= SSDFS_LOG_UNKNOWN ||
+		    state >= SSDFS_LOG_STATE_MAX) {
+			err = -ERANGE;
+			SSDFS_WARN("invalid log state: "
+				   "state %#x\n",
+				   state);
+			goto repeat;
+		}
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("finished\n");
+#else
+		SSDFS_DBG("finished\n");
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		if (state != SSDFS_LOG_CREATED) {
+			thread_state =
+				SSDFS_FLUSH_THREAD_NEED_CREATE_LOG;
+			goto sleep_flush_thread;
+		} else {
+			thread_state =
+				SSDFS_FLUSH_THREAD_GET_UPDATE_REQUEST;
+		}
+		goto next_partial_step;
+		break;
+
+	case SSDFS_FLUSH_THREAD_COMMIT_LOG:
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+		SSDFS_ERR("[FLUSH THREAD STATE] COMMIT LOG: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#else
+		SSDFS_DBG("[FLUSH THREAD STATE] COMMIT LOG: "
+			  "seg_id %llu, peb_index %u\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+		if (postponed_req) {
+			req = postponed_req;
+			postponed_req = NULL;
+			has_migration_check_requested = false;
+		} else if (req != NULL) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("req->private.class %#x, "
+				  "req->private.cmd %#x\n",
+				  req->private.class,
+				  req->private.cmd);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			switch (req->private.class) {
+			case SSDFS_PEB_COLLECT_GARBAGE_REQ:
+				/* ignore this case */
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.34.1

Part of the diff is not shown here. View the raw message.

#30 lore raw

RFC PATCH 29/76 ssdfs: commit log logic

In reply to Viacheslav Dubeyko

SSDFS stores any data or metadata in the form of logs in append-only manner. The mkfs tool defines the size of full log (for example, 32 logical block or NAND flash pages) for user data and various metadata. If flush thread has enough requests to prepare full log, then log starts from segment header, continues by payload, and finishes by footer. Otherwise, flush thread logic needs to commit a partial log. Partial log starts from partial log's header, continues by payaload, and has no footer. Potentially, area of full log can contain a sequence of partial logs. It is expected that the latest log in the sequence of partial logs finishes by footer. Header contains metadata that desribes location of block bitmap and offset translation table in the payload of log. Block bitmap tracks the state (free, pre-allocated, allocated, invalidated) of logical blocks in the erase block. Offset translation table converts logical block ID into the offset of piece of data in the log's payload.

CC Viacheslav Dubeyko <[email protected]>
CC Luka Perkov <[email protected]>
CC Bruno Banelli <[email protected]>
---
 fs/ssdfs/peb_flush_thread.c | 1745 +++++++++++++++++++++++++++++++++++
 1 file changed, 1745 insertions(+)
fs/ssdfs/peb_flush_thread.c +1745 -0
index 6a9032762ea6..d9352804f6b9 100644
@@ -113,6 +113,1751 @@ void ssdfs_flush_check_memory_leaks(void)
  *                         FLUSH THREAD FUNCTIONALITY                         *
  ******************************************************************************/
 
+/*
+ * ssdfs_peb_define_next_log_start() - define start of the next log
+ * @pebi: pointer on PEB object
+ * @log_strategy: strategy in log creation
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ */
+static
+void ssdfs_peb_define_next_log_start(struct ssdfs_peb_info *pebi,
+				     int log_strategy,
+				     pgoff_t *cur_page, u32 *write_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	u16 pages_diff;
+	u16 rest_phys_free_pages = 0;
+	u32 pages_per_peb;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!cur_page || !write_offset);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, log_strategy %#x, "
+		  "current_log.start_page %u, "
+		  "cur_page %lu, write_offset %u, "
+		  "current_log.free_data_pages %u, "
+		  "sequence_id %d\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  log_strategy,
+		  pebi->current_log.start_page,
+		  *cur_page, *write_offset,
+		  pebi->current_log.free_data_pages,
+		  atomic_read(&pebi->current_log.sequence_id));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	pages_per_peb = min_t(u32, fsi->leb_pages_capacity,
+				   fsi->peb_pages_capacity);
+
+	switch (log_strategy) {
+	case SSDFS_START_PARTIAL_LOG:
+	case SSDFS_CONTINUE_PARTIAL_LOG:
+		pebi->current_log.start_page = *cur_page;
+		rest_phys_free_pages = pebi->log_pages -
+					(*cur_page % pebi->log_pages);
+		pebi->current_log.free_data_pages = rest_phys_free_pages;
+		atomic_inc(&pebi->current_log.sequence_id);
+		WARN_ON(pebi->current_log.free_data_pages == 0);
+		break;
+
+	case SSDFS_FINISH_PARTIAL_LOG:
+	case SSDFS_FINISH_FULL_LOG:
+		if (*cur_page % pebi->log_pages) {
+			*cur_page += pebi->log_pages - 1;
+			*cur_page =
+			    (*cur_page / pebi->log_pages) * pebi->log_pages;
+		}
+
+		pebi->current_log.start_page = *cur_page;
+
+		if (pebi->current_log.start_page >= pages_per_peb) {
+			pebi->current_log.free_data_pages = 0;
+		} else {
+			pages_diff = pages_per_peb;
+			pages_diff -= pebi->current_log.start_page;
+
+			pebi->current_log.free_data_pages =
+				min_t(u16, pebi->log_pages, pages_diff);
+		}
+
+		atomic_set(&pebi->current_log.sequence_id, 0);
+		break;
+
+	default:
+		BUG();
+		break;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("pebi->current_log.start_page %u, "
+		  "current_log.free_data_pages %u, "
+		  "sequence_id %d\n",
+		  pebi->current_log.start_page,
+		  pebi->current_log.free_data_pages,
+		  atomic_read(&pebi->current_log.sequence_id));
+#endif /* CONFIG_SSDFS_DEBUG */
+}
+
+/*
+ * ssdfs_peb_store_pl_header_like_footer() - store partial log's header
+ * @pebi: pointer on PEB object
+ * @flags: partial log header's flags
+ * @hdr_desc: partial log header's metadata descriptor in segment header
+ * @plh_desc: partial log header's metadata descriptors array
+ * @array_size: count of items in array
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to store the partial log's header
+ * in the end of the log (instead of footer).
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code.
+ *
+ * %-EINVAL     - invalid input.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_pl_header_like_footer(struct ssdfs_peb_info *pebi,
+				    u32 flags,
+				    struct ssdfs_metadata_descriptor *hdr_desc,
+				    struct ssdfs_metadata_descriptor *plh_desc,
+				    size_t array_size,
+				    pgoff_t *cur_page,
+				    u32 *write_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_partial_log_header *pl_hdr;
+	u32 log_pages;
+	struct page *page;
+	size_t desc_size = sizeof(struct ssdfs_metadata_descriptor);
+	size_t array_bytes = desc_size * array_size;
+	u32 area_offset, area_size;
+	u16 seg_type;
+	int sequence_id;
+	u64 last_log_time;
+	u64 last_log_cno;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!hdr_desc || !plh_desc);
+	BUG_ON(!cur_page || !write_offset);
+	BUG_ON(array_size != SSDFS_SEG_HDR_DESC_MAX);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	seg_type = pebi->pebc->parent_si->seg_type;
+
+	sequence_id = atomic_read(&pebi->current_log.sequence_id);
+	if (sequence_id < 0 || sequence_id >= INT_MAX) {
+		SSDFS_ERR("invalid sequence_id %d\n", sequence_id);
+		return -ERANGE;
+	}
+
+	area_offset = *write_offset;
+	area_size = sizeof(struct ssdfs_partial_log_header);
+
+	*write_offset += max_t(u32, PAGE_SIZE, area_size);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(((*write_offset + PAGE_SIZE - 1) >> fsi->log_pagesize) >
+		pebi->log_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	log_pages = (*write_offset + fsi->pagesize - 1) / fsi->pagesize;
+
+	page = ssdfs_page_array_grab_page(&pebi->cache, *cur_page);
+	if (IS_ERR_OR_NULL(page)) {
+		SSDFS_ERR("fail to get cache page: index %lu\n",
+			  *cur_page);
+		return -ENOMEM;
+	}
+
+	pl_hdr = kmap_local_page(page);
+	memset(pl_hdr, 0xFF, PAGE_SIZE);
+	ssdfs_memcpy(pl_hdr->desc_array, 0, array_bytes,
+		     plh_desc, 0, array_bytes,
+		     array_bytes);
+
+	pl_hdr->peb_create_time = cpu_to_le64(pebi->peb_create_time);
+
+	last_log_time = pebi->current_log.last_log_time;
+	last_log_cno = pebi->current_log.last_log_cno;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb %llu, "
+		  "peb_create_time %llx, last_log_time %llx\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id,
+		  pebi->peb_create_time,
+		  last_log_time);
+
+	BUG_ON(pebi->peb_create_time > last_log_time);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_prepare_partial_log_header_for_commit(fsi,
+							  sequence_id,
+							  log_pages,
+							  seg_type, flags,
+							  last_log_time,
+							  last_log_cno,
+							  pl_hdr);
+
+	if (!err) {
+		hdr_desc->offset = cpu_to_le32(area_offset +
+				(pebi->current_log.start_page * fsi->pagesize));
+		hdr_desc->size = cpu_to_le32(area_size);
+
+		ssdfs_memcpy(&hdr_desc->check,
+			     0, sizeof(struct ssdfs_metadata_check),
+			     &pl_hdr->check,
+			     0, sizeof(struct ssdfs_metadata_check),
+			     sizeof(struct ssdfs_metadata_check));
+	}
+
+	flush_dcache_page(page);
+	kunmap_local(pl_hdr);
+
+	ssdfs_set_page_private(page, 0);
+	SetPageUptodate(page);
+
+	err = ssdfs_page_array_set_page_dirty(&pebi->cache, *cur_page);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to set page dirty: "
+			  "page_index %lu, err %d\n",
+			  *cur_page, err);
+	}
+
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to store partial log header: "
+			   "seg %llu, peb %llu, current_log.start_page %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   pebi->current_log.start_page, err);
+		return err;
+	}
+
+	pebi->current_log.seg_flags |=
+		SSDFS_LOG_IS_PARTIAL |
+		SSDFS_LOG_HAS_PARTIAL_HEADER |
+		SSDFS_PARTIAL_HEADER_INSTEAD_FOOTER;
+
+	(*cur_page)++;
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_store_pl_header_like_header() - store partial log's header
+ * @pebi: pointer on PEB object
+ * @flags: partial log header's flags
+ * @plh_desc: partial log header's metadata descriptors array
+ * @array_size: count of items in array
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to store the partial log's header
+ * in the beginning of the log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code.
+ *
+ * %-EINVAL     - invalid input.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_pl_header_like_header(struct ssdfs_peb_info *pebi,
+				    u32 flags,
+				    struct ssdfs_metadata_descriptor *plh_desc,
+				    size_t array_size,
+				    pgoff_t *cur_page,
+				    u32 *write_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_partial_log_header *pl_hdr;
+	struct page *page;
+	size_t desc_size = sizeof(struct ssdfs_metadata_descriptor);
+	size_t array_bytes = desc_size * array_size;
+	u32 seg_flags;
+	u32 log_pages;
+	u16 seg_type;
+	int sequence_id;
+	u64 last_log_time;
+	u64 last_log_cno;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!plh_desc);
+	BUG_ON(array_size != SSDFS_SEG_HDR_DESC_MAX);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(pebi->pebc->parent_si->seg_type > SSDFS_LAST_KNOWN_SEG_TYPE);
+	BUG_ON(*write_offset % fsi->pagesize);
+	BUG_ON((*write_offset >> fsi->log_pagesize) > pebi->log_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	sequence_id = atomic_read(&pebi->current_log.sequence_id);
+	if (sequence_id < 0 || sequence_id >= INT_MAX) {
+		SSDFS_ERR("invalid sequence_id %d\n", sequence_id);
+		return -ERANGE;
+	}
+
+	seg_type = pebi->pebc->parent_si->seg_type;
+	seg_flags = pebi->current_log.seg_flags;
+
+	log_pages = (*write_offset + fsi->pagesize - 1) / fsi->pagesize;
+
+	page = ssdfs_page_array_get_page_locked(&pebi->cache,
+						pebi->current_log.start_page);
+	if (IS_ERR_OR_NULL(page)) {
+		SSDFS_ERR("fail to get cache page: index %u\n",
+			  pebi->current_log.start_page);
+		return -ERANGE;
+	}
+
+	pl_hdr = kmap_local_page(page);
+
+	ssdfs_memcpy(pl_hdr->desc_array, 0, array_bytes,
+		     plh_desc, 0, array_bytes,
+		     array_bytes);
+
+	pl_hdr->peb_create_time = cpu_to_le64(pebi->peb_create_time);
+
+	last_log_time = pebi->current_log.last_log_time;
+	last_log_cno = pebi->current_log.last_log_cno;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb %llu, "
+		  "peb_create_time %llx, last_log_time %llx\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id,
+		  pebi->peb_create_time,
+		  last_log_time);
+
+	BUG_ON(pebi->peb_create_time > last_log_time);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_prepare_partial_log_header_for_commit(fsi,
+							  sequence_id,
+							  log_pages,
+							  seg_type,
+							  flags | seg_flags,
+							  last_log_time,
+							  last_log_cno,
+							  pl_hdr);
+	if (unlikely(err))
+		goto finish_pl_header_preparation;
+
+finish_pl_header_preparation:
+	flush_dcache_page(page);
+	kunmap_local(pl_hdr);
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to store partial log header: "
+			   "seg %llu, peb %llu, current_log.start_page %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   pebi->current_log.start_page, err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_store_partial_log_header() - store partial log's header
+ * @pebi: pointer on PEB object
+ * @flags: partial log header's flags
+ * @hdr_desc: partial log header's metadata descriptor in segment header
+ * @plh_desc: partial log header's metadata descriptors array
+ * @array_size: count of items in array
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to store the partial log's header.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code.
+ *
+ * %-EINVAL     - invalid input.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_partial_log_header(struct ssdfs_peb_info *pebi, u32 flags,
+				    struct ssdfs_metadata_descriptor *hdr_desc,
+				    struct ssdfs_metadata_descriptor *plh_desc,
+				    size_t array_size,
+				    pgoff_t *cur_page,
+				    u32 *write_offset)
+{
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!plh_desc);
+	BUG_ON(!cur_page || !write_offset);
+	BUG_ON(array_size != SSDFS_SEG_HDR_DESC_MAX);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (hdr_desc) {
+		return ssdfs_peb_store_pl_header_like_footer(pebi, flags,
+							     hdr_desc,
+							     plh_desc,
+							     array_size,
+							     cur_page,
+							     write_offset);
+	} else {
+		return ssdfs_peb_store_pl_header_like_header(pebi, flags,
+							     plh_desc,
+							     array_size,
+							     cur_page,
+							     write_offset);
+	}
+}
+
+/*
+ * ssdfs_peb_commit_first_partial_log() - commit first partial log
+ * @pebi: pointer on PEB object
+ *
+ * This function tries to commit the first partial log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code.
+ *
+ * %-EINVAL     - invalid input.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_commit_first_partial_log(struct ssdfs_peb_info *pebi)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_metadata_descriptor hdr_desc[SSDFS_SEG_HDR_DESC_MAX];
+	struct ssdfs_metadata_descriptor plh_desc[SSDFS_SEG_HDR_DESC_MAX];
+	struct ssdfs_metadata_descriptor *cur_hdr_desc;
+	u32 flags;
+	size_t desc_size = sizeof(struct ssdfs_metadata_descriptor);
+	pgoff_t cur_page = pebi->current_log.start_page;
+	u32 write_offset = 0;
+	bool log_has_data = false;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	memset(hdr_desc, 0, desc_size * SSDFS_SEG_HDR_DESC_MAX);
+	memset(plh_desc, 0, desc_size * SSDFS_SEG_HDR_DESC_MAX);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0001: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_reserve_segment_header(pebi, &cur_page, &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to reserve segment header: "
+			   "seg %llu, peb %llu, err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0002: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_commit_log_payload(pebi, hdr_desc, &log_has_data,
+					   &cur_page, &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to commit payload: "
+			   "seg %llu, peb %llu, cur_page %lu, write_offset %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   cur_page, write_offset, err);
+		goto finish_commit_log;
+	}
+
+	if (!log_has_data) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("current log hasn't data: start_page %u\n",
+			  pebi->current_log.start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0003: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	cur_hdr_desc = &hdr_desc[SSDFS_LOG_FOOTER_INDEX];
+	flags = SSDFS_LOG_IS_PARTIAL |
+		SSDFS_LOG_HAS_PARTIAL_HEADER |
+		SSDFS_PARTIAL_HEADER_INSTEAD_FOOTER;
+	err = ssdfs_peb_store_partial_log_header(pebi, flags, cur_hdr_desc,
+						 plh_desc,
+						 SSDFS_SEG_HDR_DESC_MAX,
+						 &cur_page,
+						 &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to store log's partial header: "
+			   "seg %llu, peb %llu, cur_page %lu, write_offset %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   cur_page, write_offset, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0004: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_store_log_header(pebi, hdr_desc,
+					 SSDFS_SEG_HDR_DESC_MAX,
+					 write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to store log's header: "
+			   "seg %llu, peb %llu, write_offset %u,"
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   write_offset, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0005: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_flush_current_log_dirty_pages(pebi, write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to flush current log: "
+			   "seg %llu, peb %llu, current_log.start_page %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   pebi->current_log.start_page, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0006: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	ssdfs_peb_define_next_log_start(pebi, SSDFS_START_PARTIAL_LOG,
+					&cur_page, &write_offset);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0007: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	pebi->current_log.reserved_pages = 0;
+	pebi->current_log.seg_flags = 0;
+
+	ssdfs_peb_set_current_log_state(pebi, SSDFS_LOG_COMMITTED);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("log commited: seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+finish_commit_log:
+	return err;
+}
+
+/*
+ * ssdfs_peb_commit_next_partial_log() - commit next partial log
+ * @pebi: pointer on PEB object
+ *
+ * This function tries to commit the next partial log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code.
+ *
+ * %-EINVAL     - invalid input.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_commit_next_partial_log(struct ssdfs_peb_info *pebi)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_metadata_descriptor plh_desc[SSDFS_SEG_HDR_DESC_MAX];
+	u32 flags;
+	size_t desc_size = sizeof(struct ssdfs_metadata_descriptor);
+	pgoff_t cur_page = pebi->current_log.start_page;
+	u32 write_offset = 0;
+	bool log_has_data = false;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	memset(plh_desc, 0, desc_size * SSDFS_SEG_HDR_DESC_MAX);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0001: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_reserve_partial_log_header(pebi, &cur_page, &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to reserve partial log's header: "
+			   "seg %llu, peb %llu, err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0002: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_commit_log_payload(pebi, plh_desc, &log_has_data,
+					   &cur_page, &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to commit payload: "
+			   "seg %llu, peb %llu, cur_page %lu, write_offset %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   cur_page, write_offset, err);
+		goto finish_commit_log;
+	}
+
+	if (!log_has_data) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("current log hasn't data: start_page %u\n",
+			  pebi->current_log.start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+	flags = SSDFS_LOG_IS_PARTIAL |
+		SSDFS_LOG_HAS_PARTIAL_HEADER;
+	err = ssdfs_peb_store_partial_log_header(pebi, flags, NULL,
+						 plh_desc,
+						 SSDFS_SEG_HDR_DESC_MAX,
+						 &cur_page,
+						 &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to store log's partial header: "
+			   "seg %llu, peb %llu, cur_page %lu, write_offset %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   cur_page, write_offset, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0003: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_flush_current_log_dirty_pages(pebi, write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to flush current log: "
+			   "seg %llu, peb %llu, current_log.start_page %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   pebi->current_log.start_page, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0004: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	ssdfs_peb_define_next_log_start(pebi, SSDFS_CONTINUE_PARTIAL_LOG,
+					&cur_page, &write_offset);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0005: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	pebi->current_log.reserved_pages = 0;
+	pebi->current_log.seg_flags = 0;
+
+	ssdfs_peb_set_current_log_state(pebi, SSDFS_LOG_COMMITTED);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("log commited: seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+finish_commit_log:
+	return err;
+}
+
+/*
+ * ssdfs_peb_commit_last_partial_log() - commit last partial log
+ * @pebi: pointer on PEB object
+ * @cur_segs: current segment IDs array
+ * @cur_segs_size: size of segment IDs array size in bytes
+ *
+ * This function tries to commit the last partial log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code.
+ *
+ * %-EINVAL     - invalid input.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_commit_last_partial_log(struct ssdfs_peb_info *pebi,
+					__le64 *cur_segs,
+					size_t cur_segs_size)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_metadata_descriptor plh_desc[SSDFS_SEG_HDR_DESC_MAX];
+	struct ssdfs_metadata_descriptor lf_desc[SSDFS_LOG_FOOTER_DESC_MAX];
+	struct ssdfs_metadata_descriptor *cur_hdr_desc;
+	u32 flags;
+	size_t desc_size = sizeof(struct ssdfs_metadata_descriptor);
+	pgoff_t cur_page = pebi->current_log.start_page;
+	pgoff_t cur_page_offset;
+	u32 write_offset = 0;
+	bool log_has_data = false;
+	int log_strategy = SSDFS_FINISH_PARTIAL_LOG;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	memset(plh_desc, 0, desc_size * SSDFS_SEG_HDR_DESC_MAX);
+	memset(lf_desc, 0, desc_size * SSDFS_LOG_FOOTER_DESC_MAX);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0001: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_reserve_partial_log_header(pebi, &cur_page, &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to reserve partial log's header: "
+			   "seg %llu, peb %llu, err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0002: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_commit_log_payload(pebi, plh_desc, &log_has_data,
+					   &cur_page, &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to commit payload: "
+			   "seg %llu, peb %llu, cur_page %lu, write_offset %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   cur_page, write_offset, err);
+		goto finish_commit_log;
+	}
+
+	if (!log_has_data) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("current log hasn't data: start_page %u\n",
+			  pebi->current_log.start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0003: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	cur_page_offset = cur_page % pebi->log_pages;
+
+	if (cur_page_offset == 0) {
+		/*
+		 * There is no space for log footer.
+		 * So, full log will be without footer.
+		 */
+		SSDFS_DBG("There is no space for log footer.\n");
+
+		flags = SSDFS_LOG_IS_PARTIAL |
+			SSDFS_LOG_HAS_PARTIAL_HEADER;
+		log_strategy = SSDFS_FINISH_PARTIAL_LOG;
+	} else if ((pebi->log_pages - cur_page_offset) == 1) {
+		cur_hdr_desc = &plh_desc[SSDFS_LOG_FOOTER_INDEX];
+		flags = SSDFS_PARTIAL_LOG_FOOTER | SSDFS_ENDING_LOG_FOOTER;
+		err = ssdfs_peb_store_log_footer(pebi, flags, cur_hdr_desc,
+						 lf_desc,
+						 SSDFS_LOG_FOOTER_DESC_MAX,
+						 cur_segs, cur_segs_size,
+						 &cur_page,
+						 &write_offset);
+		if (unlikely(err)) {
+			SSDFS_CRIT("fail to store log's footer: "
+				   "seg %llu, peb %llu, cur_page %lu, "
+				   "write_offset %u, err %d\n",
+				   pebi->pebc->parent_si->seg_id,
+				   pebi->peb_id,
+				   cur_page, write_offset, err);
+			goto finish_commit_log;
+		}
+
+		flags = SSDFS_LOG_IS_PARTIAL |
+			SSDFS_LOG_HAS_PARTIAL_HEADER |
+			SSDFS_LOG_HAS_FOOTER;
+		log_strategy = SSDFS_FINISH_PARTIAL_LOG;
+	} else {
+		/*
+		 * It is possible to add another log.
+		 */
+		flags = SSDFS_LOG_IS_PARTIAL |
+			SSDFS_LOG_HAS_PARTIAL_HEADER;
+		log_strategy = SSDFS_CONTINUE_PARTIAL_LOG;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0004: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_store_partial_log_header(pebi, flags, NULL,
+						 plh_desc,
+						 SSDFS_SEG_HDR_DESC_MAX,
+						 &cur_page,
+						 &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to store log's partial header: "
+			   "seg %llu, peb %llu, cur_page %lu, write_offset %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   cur_page, write_offset, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0005: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_flush_current_log_dirty_pages(pebi, write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to flush current log: "
+			   "seg %llu, peb %llu, current_log.start_page %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   pebi->current_log.start_page, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0006: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	ssdfs_peb_define_next_log_start(pebi, log_strategy,
+					&cur_page, &write_offset);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0007: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	pebi->current_log.reserved_pages = 0;
+	pebi->current_log.seg_flags = 0;
+
+	ssdfs_peb_set_current_log_state(pebi, SSDFS_LOG_COMMITTED);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("log commited: seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+finish_commit_log:
+	return err;
+}
+
+/*
+ * ssdfs_peb_commit_full_log() - commit full current log
+ * @pebi: pointer on PEB object
+ * @cur_segs: current segment IDs array
+ * @cur_segs_size: size of segment IDs array size in bytes
+ *
+ * This function tries to commit the current log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code.
+ *
+ * %-EINVAL     - invalid input.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_commit_full_log(struct ssdfs_peb_info *pebi,
+				__le64 *cur_segs,
+				size_t cur_segs_size)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_metadata_descriptor hdr_desc[SSDFS_SEG_HDR_DESC_MAX];
+	struct ssdfs_metadata_descriptor plh_desc[SSDFS_SEG_HDR_DESC_MAX];
+	struct ssdfs_metadata_descriptor lf_desc[SSDFS_LOG_FOOTER_DESC_MAX];
+	struct ssdfs_metadata_descriptor *cur_hdr_desc;
+	int log_strategy = SSDFS_FINISH_FULL_LOG;
+	u32 flags;
+	size_t desc_size = sizeof(struct ssdfs_metadata_descriptor);
+	pgoff_t cur_page = pebi->current_log.start_page;
+	pgoff_t cur_page_offset;
+	u32 write_offset = 0;
+	bool log_has_data = false;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	memset(hdr_desc, 0, desc_size * SSDFS_SEG_HDR_DESC_MAX);
+	memset(lf_desc, 0, desc_size * SSDFS_LOG_FOOTER_DESC_MAX);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0001: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_reserve_segment_header(pebi, &cur_page, &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to reserve segment header: "
+			   "seg %llu, peb %llu, err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0002: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_commit_log_payload(pebi, hdr_desc, &log_has_data,
+					   &cur_page, &write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to commit payload: "
+			   "seg %llu, peb %llu, cur_page %lu, write_offset %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   cur_page, write_offset, err);
+		goto finish_commit_log;
+	}
+
+	if (!log_has_data) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("current log hasn't data: start_page %u\n",
+			  pebi->current_log.start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+		goto define_next_log_start;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0003: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	cur_page_offset = cur_page % pebi->log_pages;
+	if (cur_page_offset == 0) {
+		SSDFS_WARN("There is no space for log footer.\n");
+	}
+
+	if ((pebi->log_pages - cur_page_offset) > 1) {
+		log_strategy = SSDFS_START_PARTIAL_LOG;
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("start partial log: "
+			  "cur_page_offset %lu, pebi->log_pages %u\n",
+			  cur_page_offset, pebi->log_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		cur_hdr_desc = &hdr_desc[SSDFS_LOG_FOOTER_INDEX];
+		flags = SSDFS_LOG_IS_PARTIAL |
+			SSDFS_LOG_HAS_PARTIAL_HEADER |
+			SSDFS_PARTIAL_HEADER_INSTEAD_FOOTER;
+		err = ssdfs_peb_store_partial_log_header(pebi, flags,
+							 cur_hdr_desc,
+							 plh_desc,
+							 SSDFS_SEG_HDR_DESC_MAX,
+							 &cur_page,
+							 &write_offset);
+		if (unlikely(err)) {
+			SSDFS_CRIT("fail to store log's partial header: "
+				   "seg %llu, peb %llu, cur_page %lu, "
+				   "write_offset %u, err %d\n",
+				   pebi->pebc->parent_si->seg_id,
+				   pebi->peb_id, cur_page,
+				   write_offset, err);
+			goto finish_commit_log;
+		}
+	} else {
+		log_strategy = SSDFS_FINISH_FULL_LOG;
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("finish full log: "
+			  "cur_page_offset %lu, pebi->log_pages %u\n",
+			  cur_page_offset, pebi->log_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		cur_hdr_desc = &hdr_desc[SSDFS_LOG_FOOTER_INDEX];
+		flags = 0;
+		err = ssdfs_peb_store_log_footer(pebi, flags, cur_hdr_desc,
+						 lf_desc,
+						 SSDFS_LOG_FOOTER_DESC_MAX,
+						 cur_segs, cur_segs_size,
+						 &cur_page,
+						 &write_offset);
+		if (unlikely(err)) {
+			SSDFS_CRIT("fail to store log's footer: "
+				   "seg %llu, peb %llu, cur_page %lu, "
+				   "write_offset %u, err %d\n",
+				   pebi->pebc->parent_si->seg_id,
+				   pebi->peb_id, cur_page,
+				   write_offset, err);
+			goto finish_commit_log;
+		}
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0004: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_store_log_header(pebi, hdr_desc,
+					 SSDFS_SEG_HDR_DESC_MAX,
+					 write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to store log's header: "
+			   "seg %llu, peb %llu, write_offset %u,"
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   write_offset, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0005: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_peb_flush_current_log_dirty_pages(pebi, write_offset);
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to flush current log: "
+			   "seg %llu, peb %llu, current_log.start_page %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   pebi->current_log.start_page, err);
+		goto finish_commit_log;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0006: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+define_next_log_start:
+	ssdfs_peb_define_next_log_start(pebi, log_strategy,
+					&cur_page, &write_offset);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("0007: cur_page %lu, write_offset %u\n",
+		  cur_page, write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	pebi->current_log.reserved_pages = 0;
+	pebi->current_log.seg_flags = 0;
+
+	ssdfs_peb_set_current_log_state(pebi, SSDFS_LOG_COMMITTED);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("log commited: seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+finish_commit_log:
+	return err;
+}
+
+/*
+ * ssdfs_peb_calculate_reserved_metapages() - calculate reserved metapages
+ * @page_size: size of page in bytes
+ * @data_pages: number of allocated data pages
+ * @pebs_per_seg: number of PEBs in one segment
+ * @log_strategy: stategy of log commiting
+ */
+u16 ssdfs_peb_calculate_reserved_metapages(u32 page_size,
+					   u32 data_pages,
+					   u32 pebs_per_seg,
+					   int log_strategy)
+{
+	size_t seg_hdr_size = sizeof(struct ssdfs_segment_header);
+	size_t lf_hdr_size = sizeof(struct ssdfs_log_footer);
+	u32 blk_bmap_bytes = 0;
+	u32 blk2off_tbl_bytes = 0;
+	u32 blk_desc_tbl_bytes = 0;
+	u32 reserved_bytes = 0;
+	u32 reserved_pages = 0;
+
+	/* segment header */
+	reserved_bytes += seg_hdr_size;
+
+	/* block bitmap */
+	blk_bmap_bytes = __ssdfs_peb_estimate_blk_bmap_bytes(data_pages, true);
+	reserved_bytes += blk_bmap_bytes;
+
+	/* blk2off table */
+	blk2off_tbl_bytes = __ssdfs_peb_estimate_blk2off_bytes(data_pages,
+								pebs_per_seg);
+	reserved_bytes += blk2off_tbl_bytes;
+
+	/* block descriptor table */
+	blk_desc_tbl_bytes =
+		__ssdfs_peb_estimate_blk_desc_tbl_bytes(data_pages);
+	reserved_bytes += blk_desc_tbl_bytes;
+
+	reserved_bytes += page_size - 1;
+	reserved_bytes /= page_size;
+	reserved_bytes *= page_size;
+
+	switch (log_strategy) {
+	case SSDFS_START_FULL_LOG:
+	case SSDFS_FINISH_PARTIAL_LOG:
+	case SSDFS_FINISH_FULL_LOG:
+		/* log footer header */
+		reserved_bytes += lf_hdr_size;
+
+		/* block bitmap */
+		reserved_bytes += blk_bmap_bytes;
+
+		/* blk2off table */
+		reserved_bytes += blk2off_tbl_bytes;
+
+		reserved_bytes += page_size - 1;
+		reserved_bytes /= page_size;
+		reserved_bytes *= page_size;
+
+		reserved_pages = reserved_bytes / page_size;
+		break;
+
+	case SSDFS_START_PARTIAL_LOG:
+	case SSDFS_CONTINUE_PARTIAL_LOG:
+		/* do nothing */
+		break;
+
+	default:
+		SSDFS_CRIT("unexpected log strategy %#x\n",
+			   log_strategy);
+		return U16_MAX;
+	}
+
+	reserved_pages = reserved_bytes / page_size;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("data_pages %u, log_strategy %#x, "
+		  "blk_bmap_bytes %u, blk2off_tbl_bytes %u, "
+		  "blk_desc_tbl_bytes %u, reserved_bytes %u, "
+		  "reserved_pages %u\n",
+		  data_pages, log_strategy,
+		  blk_bmap_bytes, blk2off_tbl_bytes,
+		  blk_desc_tbl_bytes, reserved_bytes,
+		  reserved_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	BUG_ON(reserved_pages >= U16_MAX);
+
+	return (u16)reserved_pages;
+}
+
+/*
+ * ssdfs_peb_commit_log() - commit current log
+ * @pebi: pointer on PEB object
+ * @cur_segs: current segment IDs array
+ * @cur_segs_size: size of segment IDs array size in bytes
+ *
+ * This function tries to commit the current log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code.
+ *
+ * %-EINVAL     - invalid input.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_commit_log(struct ssdfs_peb_info *pebi,
+			 __le64 *cur_segs, size_t cur_segs_size)
+{
+	struct ssdfs_segment_info *si;
+	struct ssdfs_blk2off_table *table;
+	int log_state;
+	int log_strategy;
+	u32 page_size;
+	u32 pebs_per_seg;
+	u32 pages_per_peb;
+	int used_pages;
+	int invalid_pages;
+	u32 data_pages;
+	u16 reserved_pages;
+	u16 diff;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	log_state = atomic_read(&pebi->current_log.state);
+
+	switch (log_state) {
+	case SSDFS_LOG_UNKNOWN:
+	case SSDFS_LOG_PREPARED:
+	case SSDFS_LOG_INITIALIZED:
+		SSDFS_WARN("peb %llu current log can't be commited\n",
+			   pebi->peb_id);
+		return -EINVAL;
+
+	case SSDFS_LOG_CREATED:
+		/* do function's work */
+		break;
+
+	case SSDFS_LOG_COMMITTED:
+		SSDFS_WARN("peb %llu current log has been commited\n",
+			   pebi->peb_id);
+		return 0;
+
+	default:
+		BUG();
+	};
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+	SSDFS_ERR("seg %llu, peb %llu, current_log.start_page %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page);
+#else
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page);
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+	si = pebi->pebc->parent_si;
+	log_strategy = is_log_partial(pebi);
+	page_size = pebi->pebc->parent_si->fsi->pagesize;
+	pebs_per_seg = pebi->pebc->parent_si->fsi->pebs_per_seg;
+	pages_per_peb = pebi->pebc->parent_si->fsi->pages_per_peb;
+
+	used_pages = ssdfs_peb_get_used_data_pages(pebi->pebc);
+	if (used_pages < 0) {
+		err = used_pages;
+		SSDFS_ERR("fail to get used data pages count: "
+			  "seg %llu, peb %llu, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id, err);
+		return err;
+	}
+
+	invalid_pages = ssdfs_peb_get_invalid_pages(pebi->pebc);
+	if (invalid_pages < 0) {
+		err = invalid_pages;
+		SSDFS_ERR("fail to get invalid pages count: "
+			  "seg %llu, peb %llu, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id, err);
+		return err;
+	}
+
+	data_pages = used_pages + invalid_pages;
+
+	if (data_pages == 0) {
+		SSDFS_ERR("invalid data pages count: "
+			  "used_pages %d, invalid_pages %d, "
+			  "data_pages %u\n",
+			  used_pages, invalid_pages, data_pages);
+		return -ERANGE;
+	}
+
+	reserved_pages = ssdfs_peb_calculate_reserved_metapages(page_size,
+								data_pages,
+								pebs_per_seg,
+								log_strategy);
+	if (reserved_pages > pages_per_peb) {
+		SSDFS_ERR("reserved_pages %u > pages_per_peb %u\n",
+			  reserved_pages, pages_per_peb);
+		return -ERANGE;
+	}
+
+	if (reserved_pages > pebi->current_log.reserved_pages) {
+		diff = reserved_pages - pebi->current_log.reserved_pages;
+
+		err = ssdfs_segment_blk_bmap_reserve_metapages(&si->blk_bmap,
+								pebi->pebc,
+								diff);
+		if (err == -ENOSPC) {
+			/* ignore error */
+			err = 0;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to reserve metadata pages: "
+				  "count %u, err %d\n",
+				  diff, err);
+			return err;
+		}
+
+		pebi->current_log.reserved_pages += diff;
+		if (diff > pebi->current_log.free_data_pages)
+			pebi->current_log.free_data_pages = 0;
+		else
+			pebi->current_log.free_data_pages -= diff;
+	} else if (reserved_pages < pebi->current_log.reserved_pages) {
+		diff = pebi->current_log.reserved_pages - reserved_pages;
+
+		err = ssdfs_segment_blk_bmap_free_metapages(&si->blk_bmap,
+							    pebi->pebc,
+							    diff);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to free metadata pages: "
+				  "count %u, err %d\n",
+				  diff, err);
+			return err;
+		}
+
+		pebi->current_log.reserved_pages -= diff;
+		pebi->current_log.free_data_pages += diff;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("data_pages %u, "
+		  "current_log (reserved_pages %u, free_data_pages %u)\n",
+		  data_pages,
+		  pebi->current_log.reserved_pages,
+		  pebi->current_log.free_data_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	pebi->current_log.last_log_time = ssdfs_current_timestamp();
+	pebi->current_log.last_log_cno = ssdfs_current_cno(si->fsi->sb);
+
+	log_strategy = is_log_partial(pebi);
+
+	switch (log_strategy) {
+	case SSDFS_START_FULL_LOG:
+		SSDFS_CRIT("log contains nothing: "
+			   "seg %llu, peb %llu, "
+			   "free_data_pages %u\n",
+			   pebi->pebc->parent_si->seg_id,
+			   pebi->peb_id,
+			   pebi->current_log.free_data_pages);
+		return -ERANGE;
+
+	case SSDFS_START_PARTIAL_LOG:
+		err = ssdfs_peb_commit_first_partial_log(pebi);
+		if (unlikely(err)) {
+			SSDFS_CRIT("fail to commit first partial log: "
+				   "err %d\n", err);
+			return err;
+		}
+		break;
+
+	case SSDFS_CONTINUE_PARTIAL_LOG:
+		err = ssdfs_peb_commit_next_partial_log(pebi);
+		if (unlikely(err)) {
+			SSDFS_CRIT("fail to commit next partial log: "
+				   "err %d\n", err);
+			return err;
+		}
+		break;
+
+	case SSDFS_FINISH_PARTIAL_LOG:
+		err = ssdfs_peb_commit_last_partial_log(pebi, cur_segs,
+							cur_segs_size);
+		if (unlikely(err)) {
+			SSDFS_CRIT("fail to commit last partial log: "
+				   "err %d\n", err);
+			return err;
+		}
+		break;
+
+	case SSDFS_FINISH_FULL_LOG:
+		err = ssdfs_peb_commit_full_log(pebi, cur_segs,
+						cur_segs_size);
+		if (unlikely(err)) {
+			SSDFS_CRIT("fail to commit full log: "
+				   "err %d\n", err);
+			return err;
+		}
+		break;
+
+	default:
+		SSDFS_CRIT("unexpected log strategy %#x\n",
+			   log_strategy);
+		return -ERANGE;
+	}
+
+	table = pebi->pebc->parent_si->blk2off_table;
+
+	err = ssdfs_blk2off_table_revert_migration_state(table,
+							 pebi->peb_index);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to revert migration state: "
+			  "seg %llu, peb %llu, peb_index %u, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  pebi->peb_index,
+			  err);
+	}
+
+#ifdef CONFIG_SSDFS_TRACK_API_CALL
+	SSDFS_ERR("finished\n");
+#else
+	SSDFS_DBG("finished\n");
+#endif /* CONFIG_SSDFS_TRACK_API_CALL */
+
+	return err;
+}
+
+/*
+ * ssdfs_peb_remain_log_creation_thread() - remain as log creation thread
+ * @pebc: pointer on PEB container
+ *
+ * This function check that PEB's flush thread can work
+ * as thread that creates logs.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ENOSPC     - PEB hasn't free space.
+ */
+static
+int ssdfs_peb_remain_log_creation_thread(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_segment_info *si;
+	int peb_free_pages;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si);
+
+	SSDFS_DBG("seg %llu, peb_index %d\n",
+		  pebc->parent_si->seg_id, pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	si = pebc->parent_si;
+
+	peb_free_pages = ssdfs_peb_get_free_pages(pebc);
+	if (unlikely(peb_free_pages < 0)) {
+		err = peb_free_pages;
+		SSDFS_ERR("fail to calculate PEB's free pages: "
+			  "seg %llu, peb index %d, err %d\n",
+			  pebc->parent_si->seg_id,
+			  pebc->peb_index, err);
+		return err;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("peb_free_pages %d\n", peb_free_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (peb_free_pages == 0) {
+		SSDFS_DBG("PEB hasn't free space\n");
+		return -ENOSPC;
+	}
+
+	if (!is_peb_joined_into_create_requests_queue(pebc)) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_WARN("peb_index %u hasn't creation role\n",
+			    pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		err = ssdfs_peb_join_create_requests_queue(pebc,
+							   &si->create_rq);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to join create requests queue: "
+				  "seg %llu, peb_index %d, err %d\n",
+				  si->seg_id, pebc->peb_index, err);
+			return err;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_delegate_log_creation_role() - try to delegate log creation role
+ * @pebc: pointer on PEB container
+ * @found_peb_index: index of PEB candidate
+ *
+ * This function tries to delegate the role of logs creation to
+ * PEB with @found_peb_index.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EAGAIN     - it needs to search another candidate.
+ */
+static
+int ssdfs_peb_delegate_log_creation_role(struct ssdfs_peb_container *pebc,
+					 int found_peb_index)
+{
+	struct ssdfs_segment_info *si;
+	struct ssdfs_peb_container *found_pebc;
+	int peb_free_pages;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si);
+	BUG_ON(found_peb_index >= pebc->parent_si->pebs_count);
+
+	SSDFS_DBG("seg %llu, peb_index %d, found_peb_index %d\n",
+		  pebc->parent_si->seg_id, pebc->peb_index,
+		  found_peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	si = pebc->parent_si;
+
+	if (found_peb_index == pebc->peb_index) {
+		err = ssdfs_peb_remain_log_creation_thread(pebc);
+		if (err == -ENOSPC) {
+			SSDFS_WARN("PEB hasn't free space\n");
+			return err;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to remain log creation thread: "
+				  "seg %llu, peb_index %d, "
+				  "err %d\n",
+				  si->seg_id, pebc->peb_index, err);
+			return err;
+		}
+
+		return 0;
+	}
+
+	found_pebc = &si->peb_array[found_peb_index];
+
+	peb_free_pages = ssdfs_peb_get_free_pages(found_pebc);
+	if (unlikely(peb_free_pages < 0)) {
+		err = peb_free_pages;
+		SSDFS_ERR("fail to calculate PEB's free pages: "
+			  "seg %llu, peb index %d, err %d\n",
+			  found_pebc->parent_si->seg_id,
+			  found_pebc->peb_index, err);
+		return err;
+	}
+
+	if (peb_free_pages == 0)
+		return -EAGAIN;
+
+	if (is_peb_joined_into_create_requests_queue(found_pebc)) {
+		SSDFS_WARN("PEB is creating log: "
+			   "seg %llu, peb_index %d\n",
+			   found_pebc->parent_si->seg_id,
+			   found_pebc->peb_index);
+		return -EAGAIN;
+	}
+
+	ssdfs_peb_forget_create_requests_queue(pebc);
+
+	err = ssdfs_peb_join_create_requests_queue(found_pebc,
+						   &si->create_rq);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to join create requests queue: "
+			  "seg %llu, peb_index %d, err %d\n",
+			  si->seg_id, found_pebc->peb_index, err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_find_next_log_creation_thread() - search PEB for logs creation
+ * @pebc: pointer on PEB container
+ *
+ * This function tries to find and to delegate the role of logs creation
+ * to another PEB's flush thread.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EINVAL     - invalid input.
+ * %-ERANGE     - fail to find another PEB.
+ */
+static
+int ssdfs_peb_find_next_log_creation_thread(struct ssdfs_peb_container *pebc)
+{
+	struct ssdfs_segment_info *si;
+	int start_pos;
+	int i;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebc || !pebc->parent_si);
+
+	SSDFS_DBG("seg %llu, peb_index %d\n",
+		  pebc->parent_si->seg_id, pebc->peb_index);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	si = pebc->parent_si;
+
+	if (!is_peb_joined_into_create_requests_queue(pebc)) {
+		SSDFS_WARN("peb_index %u hasn't creation role\n",
+			    pebc->peb_index);
+		return -EINVAL;
+	}
+
+	start_pos = pebc->peb_index + si->create_threads;
+
+	if (start_pos >= si->pebs_count)
+		start_pos = pebc->peb_index % si->create_threads;
+
+	if (start_pos == pebc->peb_index) {
+		err = ssdfs_peb_remain_log_creation_thread(pebc);
+		if (err == -ENOSPC) {
+			SSDFS_DBG("PEB hasn't free space\n");
+			return 0;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to remain log creation thread: "
+				  "seg %llu, peb_index %d, "
+				  "err %d\n",
+				  si->seg_id, pebc->peb_index, err);
+			return err;
+		} else
+			return 0;
+	}
+
+	if (start_pos < pebc->peb_index)
+		goto search_from_begin;
+
+	for (i = start_pos; i < si->pebs_count; i += si->create_threads) {
+		err = ssdfs_peb_delegate_log_creation_role(pebc, i);
+		if (err == -EAGAIN)
+			continue;
+		else if (err == -ENOSPC) {
+			SSDFS_WARN("PEB hasn't free space\n");
+			return err;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to delegate log creation role: "
+				  "seg %llu, peb_index %d, "
+				  "found_peb_index %d, err %d\n",
+				  si->seg_id, pebc->peb_index,
+				  i, err);
+			return err;
+		} else
+			return 0;
+	}
+
+	start_pos = pebc->peb_index % si->create_threads;
+
+search_from_begin:
+	for (i = start_pos; i <= pebc->peb_index; i += si->create_threads) {
+		err = ssdfs_peb_delegate_log_creation_role(pebc, i);
+		if (err == -EAGAIN)
+			continue;
+		if (err == -ENOSPC) {
+			SSDFS_WARN("PEB hasn't free space\n");
+			return err;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to delegate log creation role: "
+				  "seg %llu, peb_index %d, "
+				  "found_peb_index %d, err %d\n",
+				  si->seg_id, pebc->peb_index,
+				  i, err);
+			return err;
+		} else
+			return 0;
+	}
+
+	SSDFS_ERR("fail to delegate log creation role: "
+		  "seg %llu, peb_index %d\n",
+		  si->seg_id, pebc->peb_index);
+	return -ERANGE;
+}
+
 /*
  * __ssdfs_finish_request() - common logic of request's finishing
  * @pebc: pointer on PEB container
Signature · 1 line
2.34.1
#31 lore raw

RFC PATCH 30/76 ssdfs: commit log payload

In reply to Viacheslav Dubeyko

Log can be imagined like a container that keeps user data or metadata of SSDFS metadata structures. Every log starts from header (and can be finished by footer). Header and footer are log's metadata structures that describes the structure of log's payload. Initially, flush thread processes create and update requests and payload of these requests is compressed and then is compacted (ot aggregated) into contigous sequence of memory pages. When full log is ready or commit request has been received, flush thread executes commit log logic. This logic includes the steps: (1) reserve space for header, (2) define commit strategy (full or partial log), (3) store block bitmap,
(4) store offset translation table, (5) copy content of main, journal, or diff-on-write areas into log's payload, (6) create and store log's footer (optional logic), (7) create and store header into reserved space.

CC Viacheslav Dubeyko <[email protected]>
CC Luka Perkov <[email protected]>
CC Bruno Banelli <[email protected]>
---
 fs/ssdfs/peb_flush_thread.c | 3341 +++++++++++++++++++++++++++++++++++
 1 file changed, 3341 insertions(+)
fs/ssdfs/peb_flush_thread.c +3341 -0
index d9352804f6b9..2de4bb806678 100644
@@ -113,6 +113,3347 @@ void ssdfs_flush_check_memory_leaks(void)
  *                         FLUSH THREAD FUNCTIONALITY                         *
  ******************************************************************************/
 
+/*
+ * ssdfs_peb_has_dirty_pages() - check that PEB has dirty pages
+ * @pebi: pointer on PEB object
+ */
+bool ssdfs_peb_has_dirty_pages(struct ssdfs_peb_info *pebi)
+{
+	struct ssdfs_page_array *area_pages;
+	bool is_peb_dirty = false;
+	int i;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	for (i = 0; i < SSDFS_LOG_AREA_MAX; i++) {
+		area_pages = &pebi->current_log.area[i].array;
+
+		if (atomic_read(&area_pages->state) == SSDFS_PAGE_ARRAY_DIRTY) {
+			is_peb_dirty = true;
+			break;
+		}
+	}
+
+	return is_peb_dirty;
+}
+
+/*
+ * is_full_log_ready() - check that full log is ready
+ * @pebi: pointer on PEB object
+ */
+static inline
+bool is_full_log_ready(struct ssdfs_peb_info *pebi)
+{
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, free_data_pages %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id,
+		  pebi->current_log.free_data_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	return pebi->current_log.free_data_pages == 0;
+}
+
+/*
+ * should_partial_log_being_commited() - check that it's time to commit
+ * @pebi: pointer on PEB object
+ */
+static inline
+bool should_partial_log_being_commited(struct ssdfs_peb_info *pebi)
+{
+	u16 free_data_pages;
+	u16 min_partial_log_pages;
+	int log_strategy;
+	bool time_to_commit = false;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	free_data_pages = pebi->current_log.free_data_pages;
+	min_partial_log_pages = ssdfs_peb_estimate_min_partial_log_pages(pebi);
+
+	log_strategy = is_log_partial(pebi);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb %llu, log_strategy %#x, "
+		  "free_data_pages %u, min_partial_log_pages %u\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id, log_strategy,
+		  free_data_pages, min_partial_log_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	switch (log_strategy) {
+	case SSDFS_START_FULL_LOG:
+	case SSDFS_START_PARTIAL_LOG:
+		if (free_data_pages <= min_partial_log_pages) {
+			time_to_commit = true;
+		} else {
+			time_to_commit = false;
+		}
+		break;
+
+	case SSDFS_CONTINUE_PARTIAL_LOG:
+	case SSDFS_FINISH_PARTIAL_LOG:
+	case SSDFS_FINISH_FULL_LOG:
+		/* do nothing */
+		time_to_commit = false;
+		break;
+
+	default:
+		SSDFS_CRIT("unexpected log strategy %#x\n",
+			   log_strategy);
+		time_to_commit = false;
+		break;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("time_to_commit %#x\n", time_to_commit);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	return time_to_commit;
+}
+
+/*
+ * ssdfs_reserve_segment_header() - reserve space for segment header
+ * @pebi: pointer on PEB object
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function reserves space for segment header in PEB's cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EINVAL     - invalid input.
+ * %-ENOMEM     - fail to allocate page.
+ */
+static
+int ssdfs_reserve_segment_header(struct ssdfs_peb_info *pebi,
+				 pgoff_t *cur_page, u32 *write_offset)
+{
+	struct page *page;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!cur_page || !write_offset);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  *cur_page, *write_offset);
+
+	if (*cur_page != pebi->current_log.start_page) {
+		SSDFS_ERR("cur_page %lu != start_page %u\n",
+			  *cur_page, pebi->current_log.start_page);
+		return -EINVAL;
+	}
+
+	if (*write_offset != 0) {
+		SSDFS_ERR("write_offset %u != 0\n",
+			  *write_offset);
+		return -EINVAL;
+	}
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	page = ssdfs_page_array_grab_page(&pebi->cache, *cur_page);
+	if (IS_ERR_OR_NULL(page)) {
+		SSDFS_ERR("fail to grab cache page: index %lu\n",
+			  *cur_page);
+		return -ENOMEM;
+	}
+
+	/* prepare header space */
+	ssdfs_memset_page(page, 0, PAGE_SIZE, 0xFF, PAGE_SIZE);
+
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*write_offset = offsetof(struct ssdfs_segment_header, payload);
+
+	return 0;
+}
+
+/*
+ * ssdfs_reserve_partial_log_header() - reserve space for partial log's header
+ * @pebi: pointer on PEB object
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function reserves space for partial log's header in PEB's cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EINVAL     - invalid input.
+ * %-ENOMEM     - fail to allocate page.
+ */
+static
+int ssdfs_reserve_partial_log_header(struct ssdfs_peb_info *pebi,
+				     pgoff_t *cur_page, u32 *write_offset)
+{
+	struct page *page;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!cur_page || !write_offset);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  *cur_page, *write_offset);
+
+	if (*cur_page != pebi->current_log.start_page) {
+		SSDFS_ERR("cur_page %lu != start_page %u\n",
+			  *cur_page, pebi->current_log.start_page);
+		return -EINVAL;
+	}
+
+	if (*write_offset != 0) {
+		SSDFS_ERR("write_offset %u != 0\n",
+			  *write_offset);
+		return -EINVAL;
+	}
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	page = ssdfs_page_array_grab_page(&pebi->cache, *cur_page);
+	if (IS_ERR_OR_NULL(page)) {
+		SSDFS_ERR("fail to grab cache page: index %lu\n",
+			  *cur_page);
+		return -ENOMEM;
+	}
+
+	/* prepare header space */
+	ssdfs_memset_page(page, 0, PAGE_SIZE, 0xFF, PAGE_SIZE);
+
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*write_offset = offsetof(struct ssdfs_partial_log_header, payload);
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_store_pagevec() - store pagevec into page cache
+ * @desc: descriptor of pagevec environment
+ *
+ * This function tries to store pagevec into log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_pagevec(struct ssdfs_pagevec_descriptor *desc)
+{
+	struct ssdfs_fs_info *fsi;
+	struct page *src_page, *dst_page;
+	unsigned char *kaddr;
+	int i;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!desc);
+	BUG_ON(!desc->pebi || !desc->pebi->pebc->parent_si);
+	BUG_ON(!desc->pebi->pebc->parent_si->fsi);
+	BUG_ON(!desc->page_vec || !desc->desc_array);
+	BUG_ON(!desc->cur_page || !desc->write_offset);
+
+	switch (desc->compression_type) {
+	case SSDFS_FRAGMENT_UNCOMPR_BLOB:
+	case SSDFS_FRAGMENT_ZLIB_BLOB:
+	case SSDFS_FRAGMENT_LZO_BLOB:
+		/* valid type */
+		break;
+
+	default:
+		SSDFS_WARN("invalid compression %#x\n",
+			   desc->compression_type);
+		return -EINVAL;
+	}
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u\n",
+		  desc->pebi->pebc->parent_si->seg_id,
+		  desc->pebi->peb_id,
+		  desc->pebi->current_log.start_page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = desc->pebi->pebc->parent_si->fsi;
+	desc->compr_size = 0;
+	desc->uncompr_size = 0;
+	desc->fragments_count = 0;
+
+	for (i = 0; i < ssdfs_page_vector_count(desc->page_vec); i++) {
+		size_t iter_bytes;
+		size_t dst_page_off;
+		size_t dst_free_space;
+		struct ssdfs_fragment_source from;
+		struct ssdfs_fragment_destination to;
+
+		BUG_ON(i >= desc->array_capacity);
+
+		if (desc->uncompr_size > desc->bytes_count) {
+			SSDFS_WARN("uncompr_size %u > bytes_count %zu\n",
+				   desc->uncompr_size,
+				   desc->bytes_count);
+			break;
+		} else if (desc->uncompr_size == desc->bytes_count)
+			break;
+
+		iter_bytes = min_t(size_t, PAGE_SIZE,
+				   desc->bytes_count - desc->uncompr_size);
+
+		src_page = desc->page_vec->pages[i];
+
+try_get_next_page:
+		dst_page = ssdfs_page_array_grab_page(&desc->pebi->cache,
+						      *desc->cur_page);
+		if (IS_ERR_OR_NULL(dst_page)) {
+			SSDFS_ERR("fail to grab cache page: index %lu\n",
+				  *desc->cur_page);
+			return -ENOMEM;
+		}
+
+		dst_page_off = *(desc->write_offset) % PAGE_SIZE;
+		dst_free_space = PAGE_SIZE - dst_page_off;
+
+		kaddr = kmap_local_page(dst_page);
+
+		from.page = src_page;
+		from.start_offset = 0;
+		from.data_bytes = iter_bytes;
+		from.sequence_id = desc->start_sequence_id + i;
+		from.fragment_type = desc->compression_type;
+		from.fragment_flags = SSDFS_FRAGMENT_HAS_CSUM;
+
+		to.area_offset = desc->area_offset;
+		to.write_offset = *desc->write_offset;
+		to.store = kaddr + dst_page_off;
+		to.free_space = dst_free_space;
+		to.compr_size = 0;
+		to.desc = &desc->desc_array[i];
+
+		err = ssdfs_peb_store_fragment(&from, &to);
+
+		flush_dcache_page(dst_page);
+		kunmap_local(kaddr);
+
+		if (!err) {
+			ssdfs_set_page_private(dst_page, 0);
+			SetPageUptodate(dst_page);
+
+			err =
+			    ssdfs_page_array_set_page_dirty(&desc->pebi->cache,
+							    *desc->cur_page);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to set page %lu dirty: "
+					  "err %d\n",
+					  *desc->cur_page, err);
+			}
+		}
+
+		ssdfs_unlock_page(dst_page);
+		ssdfs_put_page(dst_page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("page %p, count %d\n",
+			  dst_page, page_ref_count(dst_page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("try to get next page: "
+				  "write_offset %u, dst_free_space %zu\n",
+				  *desc->write_offset,
+				  dst_free_space);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			*desc->write_offset += dst_free_space;
+			(*desc->cur_page)++;
+			goto try_get_next_page;
+		}
+
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to store fragment: "
+				  "sequence_id %u, write_offset %u, err %d\n",
+				  desc->start_sequence_id + i,
+				  *desc->write_offset, err);
+			return err;
+		}
+
+		desc->uncompr_size += iter_bytes;
+		*desc->write_offset += to.compr_size;
+		desc->compr_size += to.compr_size;
+		desc->fragments_count++;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_store_blk_bmap_fragment() - store fragment of block bitmap
+ * @desc: descriptor of block bitmap fragment environment
+ * @bmap_hdr_offset: offset of header from log's beginning
+ *
+ * This function tries to store block bitmap fragment
+ * into PEB's log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_blk_bmap_fragment(struct ssdfs_bmap_descriptor *desc,
+				      u32 bmap_hdr_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_block_bitmap_fragment *frag_hdr = NULL;
+	struct ssdfs_fragment_desc *frag_desc_array = NULL;
+	size_t frag_hdr_size = sizeof(struct ssdfs_block_bitmap_fragment);
+	size_t frag_desc_size = sizeof(struct ssdfs_fragment_desc);
+	size_t allocation_size = 0;
+	u32 frag_hdr_off;
+	struct ssdfs_pagevec_descriptor pvec_desc;
+	u32 pages_per_peb;
+	struct page *page;
+	pgoff_t index;
+	u32 page_off;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!desc);
+	BUG_ON(!desc->pebi || !desc->cur_page || !desc->write_offset);
+	BUG_ON(ssdfs_page_vector_count(desc->snapshot) == 0);
+
+	switch (desc->compression_type) {
+	case SSDFS_BLK_BMAP_NOCOMPR_TYPE:
+	case SSDFS_BLK_BMAP_ZLIB_COMPR_TYPE:
+	case SSDFS_BLK_BMAP_LZO_COMPR_TYPE:
+		/* valid type */
+		break;
+
+	default:
+		SSDFS_WARN("invalid compression %#x\n",
+			   desc->compression_type);
+		return -EINVAL;
+	}
+
+	SSDFS_DBG("peb_id %llu, peb_index %u, "
+		  "cur_page %lu, write_offset %u, "
+		  "desc->compression_type %#x\n",
+		  desc->pebi->peb_id,
+		  desc->pebi->peb_index,
+		  *(desc->cur_page), *(desc->write_offset),
+		  desc->compression_type);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = desc->pebi->pebc->parent_si->fsi;
+
+	allocation_size = frag_hdr_size;
+	allocation_size +=
+		ssdfs_page_vector_count(desc->snapshot) * frag_desc_size;
+
+	frag_hdr = ssdfs_flush_kzalloc(allocation_size, GFP_KERNEL);
+	if (!frag_hdr) {
+		SSDFS_ERR("unable to allocate block bmap header\n");
+		return -ENOMEM;
+	}
+
+	frag_hdr_off = *(desc->write_offset);
+	*(desc->write_offset) += allocation_size;
+
+	frag_desc_array = (struct ssdfs_fragment_desc *)((u8 *)frag_hdr +
+							  frag_hdr_size);
+
+	switch (desc->compression_type) {
+	case SSDFS_BLK_BMAP_NOCOMPR_TYPE:
+		pvec_desc.compression_type = SSDFS_FRAGMENT_UNCOMPR_BLOB;
+		break;
+
+	case SSDFS_BLK_BMAP_ZLIB_COMPR_TYPE:
+		pvec_desc.compression_type = SSDFS_FRAGMENT_ZLIB_BLOB;
+		break;
+
+	case SSDFS_BLK_BMAP_LZO_COMPR_TYPE:
+		pvec_desc.compression_type = SSDFS_FRAGMENT_LZO_BLOB;
+		break;
+
+	default:
+		SSDFS_WARN("invalid compression %#x\n",
+			   desc->compression_type);
+		return -EINVAL;
+	}
+
+	pvec_desc.pebi = desc->pebi;
+	pvec_desc.start_sequence_id = 0;
+	pvec_desc.area_offset = bmap_hdr_offset;
+	pvec_desc.page_vec = desc->snapshot;
+	pvec_desc.bytes_count = desc->bytes_count;
+	pvec_desc.desc_array = frag_desc_array;
+	pvec_desc.array_capacity = SSDFS_BLK_BMAP_FRAGMENTS_CHAIN_MAX;
+	pvec_desc.cur_page = desc->cur_page;
+	pvec_desc.write_offset = desc->write_offset;
+
+	err = ssdfs_peb_store_pagevec(&pvec_desc);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to store block bitmap in the log: "
+			  "seg %llu, peb %llu, write_offset %u, "
+			  "err %d\n",
+			  desc->pebi->pebc->parent_si->seg_id,
+			  desc->pebi->peb_id,
+			  *(desc->write_offset), err);
+		goto fail_store_bmap_fragment;
+	}
+
+	frag_hdr->peb_index = cpu_to_le16(desc->peb_index);
+	frag_hdr->sequence_id = *(desc->frag_id);
+	*(desc->frag_id) += 1;
+	frag_hdr->flags = desc->flags;
+	frag_hdr->type = desc->type;
+
+	pages_per_peb = fsi->pages_per_peb;
+
+	if (desc->last_free_blk >= pages_per_peb) {
+		SSDFS_ERR("last_free_page %u >= pages_per_peb %u\n",
+			  desc->last_free_blk, pages_per_peb);
+		err = -ERANGE;
+		goto fail_store_bmap_fragment;
+	}
+
+	if ((desc->invalid_blks + desc->metadata_blks) > pages_per_peb) {
+		SSDFS_ERR("invalid descriptor state: "
+			  "invalid_blks %u, metadata_blks %u, "
+			  "pages_per_peb %u\n",
+			  desc->invalid_blks,
+			  desc->metadata_blks,
+			  pages_per_peb);
+		err = -ERANGE;
+		goto fail_store_bmap_fragment;
+	}
+
+	frag_hdr->last_free_blk = cpu_to_le32(desc->last_free_blk);
+	frag_hdr->metadata_blks = cpu_to_le32(desc->metadata_blks);
+	frag_hdr->invalid_blks = cpu_to_le32(desc->invalid_blks);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	WARN_ON(pvec_desc.compr_size > pvec_desc.uncompr_size);
+	WARN_ON(pvec_desc.compr_size >
+			desc->pebi->pebc->parent_si->fsi->segsize);
+#endif /* CONFIG_SSDFS_DEBUG */
+	frag_hdr->chain_hdr.compr_bytes = cpu_to_le32(pvec_desc.compr_size);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	WARN_ON(pvec_desc.uncompr_size >
+			desc->pebi->pebc->parent_si->fsi->segsize);
+#endif /* CONFIG_SSDFS_DEBUG */
+	frag_hdr->chain_hdr.uncompr_bytes = cpu_to_le32(pvec_desc.uncompr_size);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	WARN_ON(pvec_desc.fragments_count > SSDFS_BLK_BMAP_FRAGMENTS_CHAIN_MAX);
+#endif /* CONFIG_SSDFS_DEBUG */
+	frag_hdr->chain_hdr.fragments_count =
+		cpu_to_le16(pvec_desc.fragments_count);
+
+	frag_hdr->chain_hdr.desc_size = cpu_to_le16(frag_desc_size);
+	frag_hdr->chain_hdr.magic = SSDFS_CHAIN_HDR_MAGIC;
+	frag_hdr->chain_hdr.type = SSDFS_BLK_BMAP_CHAIN_HDR;
+	frag_hdr->chain_hdr.flags = 0;
+
+	index = ssdfs_write_offset_to_mem_page_index(fsi,
+				desc->pebi->current_log.start_page,
+				frag_hdr_off);
+
+	page = ssdfs_page_array_get_page_locked(&desc->pebi->cache, index);
+	if (IS_ERR_OR_NULL(page)) {
+		SSDFS_ERR("fail to get cache page: index %lu\n", index);
+		err = -ENOMEM;
+		goto fail_store_bmap_fragment;
+	}
+
+	page_off = frag_hdr_off % PAGE_SIZE;
+
+	err = ssdfs_memcpy_to_page(page, page_off, PAGE_SIZE,
+				   frag_hdr, 0, allocation_size,
+				   allocation_size);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to copy: "
+			  "page_off %u, allocation_size %zu, err %d\n",
+			  page_off, allocation_size, err);
+		goto finish_copy;
+	}
+
+	ssdfs_set_page_private(page, 0);
+	SetPageUptodate(page);
+
+	err = ssdfs_page_array_set_page_dirty(&desc->pebi->cache, index);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to set page %lu dirty: "
+			  "err %d\n",
+			  index, err);
+	}
+
+finish_copy:
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+fail_store_bmap_fragment:
+	ssdfs_block_bmap_forget_snapshot(desc->snapshot);
+	ssdfs_flush_kfree(frag_hdr);
+	return err;
+}
+
+/*
+ * ssdfs_peb_store_dst_blk_bmap() - store destination block bitmap
+ * @pebi: pointer on PEB object
+ * @items_state: PEB container's items state
+ * @compression: compression type
+ * @bmap_hdr_off: offset from log's beginning to bitmap header
+ * @frag_id: pointer on fragments counter [in|out]
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to store destination block bitmap
+ * into destination PEB's log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_dst_blk_bmap(struct ssdfs_peb_info *pebi,
+				 int items_state,
+				 u8 compression,
+				 u32 bmap_hdr_off,
+				 u8 *frag_id,
+				 pgoff_t *cur_page,
+				 u32 *write_offset)
+{
+	struct ssdfs_segment_blk_bmap *seg_blkbmap;
+	struct ssdfs_peb_blk_bmap *peb_blkbmap;
+	struct ssdfs_block_bmap *bmap;
+	struct ssdfs_bmap_descriptor desc;
+	int buffers_state;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!frag_id || !cur_page || !write_offset);
+	BUG_ON(!rwsem_is_locked(&pebi->pebc->lock));
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	switch (items_state) {
+	case SSDFS_PEB1_SRC_PEB2_DST_CONTAINER:
+	case SSDFS_PEB2_SRC_PEB1_DST_CONTAINER:
+		/* valid state */
+		break;
+
+	default:
+		SSDFS_WARN("invalid items_state %#x\n",
+			   items_state);
+		return -EINVAL;
+	}
+
+	switch (compression) {
+	case SSDFS_BLK_BMAP_NOCOMPR_TYPE:
+	case SSDFS_BLK_BMAP_ZLIB_COMPR_TYPE:
+	case SSDFS_BLK_BMAP_LZO_COMPR_TYPE:
+		/* valid type */
+		break;
+
+	default:
+		SSDFS_WARN("invalid compression %#x\n",
+			   compression);
+		return -EINVAL;
+	}
+
+	SSDFS_DBG("seg %llu, peb_index %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_index,
+		  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	desc.compression_type = compression;
+	desc.flags = SSDFS_PEB_HAS_RELATION | SSDFS_MIGRATING_BLK_BMAP;
+	desc.type = SSDFS_DST_BLK_BMAP;
+	desc.frag_id = frag_id;
+	desc.cur_page = cur_page;
+	desc.write_offset = write_offset;
+
+	desc.snapshot = &pebi->current_log.bmap_snapshot;
+
+	if (!pebi->pebc->src_peb || !pebi->pebc->dst_peb) {
+		SSDFS_WARN("empty src or dst PEB pointer\n");
+		return -ERANGE;
+	}
+
+	if (pebi == pebi->pebc->src_peb)
+		desc.pebi = pebi->pebc->src_peb;
+	else
+		desc.pebi = pebi->pebc->dst_peb;
+
+	if (!desc.pebi) {
+		SSDFS_WARN("destination PEB doesn't exist\n");
+		return -ERANGE;
+	}
+
+	desc.peb_index = desc.pebi->peb_index;
+
+	seg_blkbmap = &pebi->pebc->parent_si->blk_bmap;
+	peb_blkbmap = &seg_blkbmap->peb[pebi->pebc->peb_index];
+
+	err = ssdfs_page_vector_init(desc.snapshot);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to init page vector: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	if (!ssdfs_peb_blk_bmap_initialized(peb_blkbmap)) {
+		SSDFS_ERR("PEB's block bitmap isn't initialized\n");
+		return -ERANGE;
+	}
+
+	down_read(&peb_blkbmap->lock);
+
+	buffers_state = atomic_read(&peb_blkbmap->buffers_state);
+	switch (buffers_state) {
+	case SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST:
+	case SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST:
+		/* valid state */
+		break;
+
+	default:
+		err = -ERANGE;
+		SSDFS_WARN("invalid buffers_state %#x\n",
+			   buffers_state);
+		goto finish_store_dst_blk_bmap;
+	}
+
+	bmap = peb_blkbmap->dst;
+	if (!bmap) {
+		err = -ERANGE;
+		SSDFS_WARN("destination bitmap doesn't exist\n");
+		goto finish_store_dst_blk_bmap;
+	}
+
+	err = ssdfs_block_bmap_lock(bmap);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to lock block bitmap: err %d\n", err);
+		goto finish_store_dst_blk_bmap;
+	}
+
+	err = ssdfs_block_bmap_snapshot(bmap, desc.snapshot,
+					&desc.last_free_blk,
+					&desc.metadata_blks,
+					&desc.invalid_blks,
+					&desc.bytes_count);
+
+	ssdfs_block_bmap_unlock(bmap);
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to snapshot block bitmap: "
+			  "seg %llu, peb_index %u, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->pebc->peb_index, err);
+		goto finish_store_dst_blk_bmap;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("peb_id %llu, DST: last_free_blk %u, "
+		  "metadata_blks %u, invalid_blks %u\n",
+		  pebi->peb_id, desc.last_free_blk,
+		  desc.metadata_blks, desc.invalid_blks);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (ssdfs_page_vector_count(desc.snapshot) == 0) {
+		err = -ERANGE;
+		SSDFS_ERR("empty block bitmap\n");
+		goto finish_store_dst_blk_bmap;
+	}
+
+finish_store_dst_blk_bmap:
+	up_read(&peb_blkbmap->lock);
+
+	if (unlikely(err))
+		return err;
+
+	return ssdfs_peb_store_blk_bmap_fragment(&desc, bmap_hdr_off);
+}
+
+/*
+ * ssdfs_peb_store_source_blk_bmap() - store source block bitmap
+ * @pebi: pointer on PEB object
+ * @items_state: PEB container's items state
+ * @compression: compression type
+ * @bmap_hdr_off: offset from log's beginning to bitmap header
+ * @frag_id: pointer on fragments counter [in|out]
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to store source block bitmap
+ * into destination PEB's log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_source_blk_bmap(struct ssdfs_peb_info *pebi,
+				    int items_state,
+				    u8 compression,
+				    u32 bmap_hdr_off,
+				    u8 *frag_id,
+				    pgoff_t *cur_page,
+				    u32 *write_offset)
+{
+	struct ssdfs_segment_blk_bmap *seg_blkbmap;
+	struct ssdfs_peb_blk_bmap *peb_blkbmap;
+	struct ssdfs_block_bmap *bmap;
+	struct ssdfs_bmap_descriptor desc;
+	int buffers_state;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!frag_id || !cur_page || !write_offset);
+	BUG_ON(!pebi);
+	BUG_ON(!rwsem_is_locked(&pebi->pebc->lock));
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	switch (items_state) {
+	case SSDFS_PEB1_SRC_CONTAINER:
+	case SSDFS_PEB2_SRC_CONTAINER:
+	case SSDFS_PEB1_SRC_PEB2_DST_CONTAINER:
+	case SSDFS_PEB2_SRC_PEB1_DST_CONTAINER:
+	case SSDFS_PEB1_DST_CONTAINER:
+	case SSDFS_PEB2_DST_CONTAINER:
+		/* valid state */
+		break;
+
+	default:
+		SSDFS_WARN("invalid items_state %#x\n",
+			   items_state);
+		return -EINVAL;
+	}
+
+	switch (compression) {
+	case SSDFS_BLK_BMAP_NOCOMPR_TYPE:
+	case SSDFS_BLK_BMAP_ZLIB_COMPR_TYPE:
+	case SSDFS_BLK_BMAP_LZO_COMPR_TYPE:
+		/* valid type */
+		break;
+
+	default:
+		SSDFS_WARN("invalid compression %#x\n",
+			   compression);
+		return -EINVAL;
+	}
+
+	SSDFS_DBG("seg %llu, peb_index %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_index,
+		  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	desc.compression_type = compression;
+	desc.frag_id = frag_id;
+	desc.cur_page = cur_page;
+	desc.write_offset = write_offset;
+
+	desc.snapshot = &pebi->current_log.bmap_snapshot;
+
+	switch (items_state) {
+	case SSDFS_PEB1_SRC_CONTAINER:
+	case SSDFS_PEB2_SRC_CONTAINER:
+		desc.flags = 0;
+		desc.type = SSDFS_SRC_BLK_BMAP;
+		desc.pebi = pebi->pebc->src_peb;
+		break;
+
+	case SSDFS_PEB1_SRC_PEB2_DST_CONTAINER:
+	case SSDFS_PEB2_SRC_PEB1_DST_CONTAINER:
+		if (!pebi->pebc->src_peb || !pebi->pebc->dst_peb) {
+			SSDFS_WARN("empty src or dst PEB pointer\n");
+			return -ERANGE;
+		}
+
+		desc.flags = SSDFS_PEB_HAS_RELATION |
+				SSDFS_MIGRATING_BLK_BMAP;
+		desc.type = SSDFS_SRC_BLK_BMAP;
+
+		if (pebi == pebi->pebc->src_peb)
+			desc.pebi = pebi->pebc->src_peb;
+		else
+			desc.pebi = pebi->pebc->dst_peb;
+		break;
+
+	case SSDFS_PEB1_DST_CONTAINER:
+	case SSDFS_PEB2_DST_CONTAINER:
+		desc.flags = SSDFS_MIGRATING_BLK_BMAP;
+		desc.type = SSDFS_DST_BLK_BMAP;
+		/* log could be created in destintaion PEB only */
+		desc.pebi = pebi->pebc->dst_peb;
+		break;
+
+	default:
+		BUG();
+	}
+
+	if (!desc.pebi) {
+		SSDFS_WARN("destination PEB doesn't exist\n");
+		return -ERANGE;
+	}
+
+	desc.peb_index = desc.pebi->peb_index;
+
+	seg_blkbmap = &pebi->pebc->parent_si->blk_bmap;
+	peb_blkbmap = &seg_blkbmap->peb[pebi->peb_index];
+
+	err = ssdfs_page_vector_init(desc.snapshot);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to init page vector: "
+			  "err %d\n", err);
+		return err;
+	}
+
+	if (!ssdfs_peb_blk_bmap_initialized(peb_blkbmap)) {
+		SSDFS_ERR("PEB's block bitmap isn't initialized\n");
+		return -ERANGE;
+	}
+
+	down_read(&peb_blkbmap->lock);
+
+	buffers_state = atomic_read(&peb_blkbmap->buffers_state);
+	switch (buffers_state) {
+	case SSDFS_PEB_BMAP1_SRC:
+	case SSDFS_PEB_BMAP2_SRC:
+	case SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST:
+	case SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST:
+		/* valid state */
+		break;
+
+	default:
+		err = -ERANGE;
+		SSDFS_WARN("invalid buffers_state %#x\n",
+			   buffers_state);
+		goto finish_store_src_blk_bmap;
+	}
+
+	bmap = peb_blkbmap->src;
+	if (!bmap) {
+		err = -ERANGE;
+		SSDFS_WARN("source bitmap doesn't exist\n");
+		goto finish_store_src_blk_bmap;
+	}
+
+	err = ssdfs_block_bmap_lock(bmap);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to lock block bitmap: err %d\n", err);
+		goto finish_store_src_blk_bmap;
+	}
+
+	err = ssdfs_block_bmap_snapshot(bmap, desc.snapshot,
+					&desc.last_free_blk,
+					&desc.metadata_blks,
+					&desc.invalid_blks,
+					&desc.bytes_count);
+
+	ssdfs_block_bmap_unlock(bmap);
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to snapshot block bitmap: "
+			  "seg %llu, peb_index %u, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->pebc->peb_index, err);
+		goto finish_store_src_blk_bmap;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("peb_id %llu, SRC: last_free_blk %u, "
+		  "metadata_blks %u, invalid_blks %u\n",
+		  pebi->peb_id, desc.last_free_blk,
+		  desc.metadata_blks, desc.invalid_blks);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (desc.metadata_blks == 0) {
+		SSDFS_WARN("peb_id %llu, SRC: last_free_blk %u, "
+			   "metadata_blks %u, invalid_blks %u\n",
+			   pebi->peb_id, desc.last_free_blk,
+			   desc.metadata_blks, desc.invalid_blks);
+		BUG();
+	}
+
+	if (ssdfs_page_vector_count(desc.snapshot) == 0) {
+		err = -ERANGE;
+		SSDFS_ERR("empty block bitmap\n");
+		goto finish_store_src_blk_bmap;
+	}
+
+finish_store_src_blk_bmap:
+	up_read(&peb_blkbmap->lock);
+
+	if (unlikely(err))
+		return err;
+
+	return ssdfs_peb_store_blk_bmap_fragment(&desc, bmap_hdr_off);
+}
+
+/*
+ * ssdfs_peb_store_dependent_blk_bmap() - store dependent source bitmaps
+ * @pebi: pointer on PEB object
+ * @items_state: PEB container's items state
+ * @compression: compression type
+ * @bmap_hdr_off: offset from log's beginning to bitmap header
+ * @frag_id: pointer on fragments counter [in|out]
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to store dependent source block bitmaps
+ * of migrating PEBs into destination PEB's log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_dependent_blk_bmap(struct ssdfs_peb_info *pebi,
+					int items_state,
+					u8 compression,
+					u32 bmap_hdr_off,
+					u8 *frag_id,
+					pgoff_t *cur_page,
+					u32 *write_offset)
+{
+	struct ssdfs_segment_blk_bmap *seg_blkbmap;
+	struct ssdfs_peb_blk_bmap *peb_blkbmap;
+	struct ssdfs_block_bmap *bmap;
+	struct ssdfs_bmap_descriptor desc;
+	int i;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!frag_id || !cur_page || !write_offset);
+	BUG_ON(!rwsem_is_locked(&pebi->pebc->lock));
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	switch (items_state) {
+	case SSDFS_PEB1_SRC_PEB2_DST_CONTAINER:
+	case SSDFS_PEB2_SRC_PEB1_DST_CONTAINER:
+	case SSDFS_PEB1_DST_CONTAINER:
+	case SSDFS_PEB2_DST_CONTAINER:
+		/* valid state */
+		break;
+
+	default:
+		SSDFS_WARN("invalid items_state %#x\n",
+			   items_state);
+		return -EINVAL;
+	}
+
+	switch (compression) {
+	case SSDFS_BLK_BMAP_NOCOMPR_TYPE:
+	case SSDFS_BLK_BMAP_ZLIB_COMPR_TYPE:
+	case SSDFS_BLK_BMAP_LZO_COMPR_TYPE:
+		/* valid type */
+		break;
+
+	default:
+		SSDFS_WARN("invalid compression %#x\n",
+			   compression);
+		return -EINVAL;
+	}
+
+	SSDFS_DBG("seg %llu, peb_index %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_index,
+		  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	desc.compression_type = compression;
+	desc.frag_id = frag_id;
+	desc.cur_page = cur_page;
+	desc.write_offset = write_offset;
+
+	desc.snapshot = &pebi->current_log.bmap_snapshot;
+
+	switch (items_state) {
+	case SSDFS_PEB1_SRC_PEB2_DST_CONTAINER:
+	case SSDFS_PEB2_SRC_PEB1_DST_CONTAINER:
+	case SSDFS_PEB1_DST_CONTAINER:
+	case SSDFS_PEB2_DST_CONTAINER:
+		desc.flags = SSDFS_PEB_HAS_EXT_PTR | SSDFS_MIGRATING_BLK_BMAP;
+		desc.type = SSDFS_SRC_BLK_BMAP;
+		desc.pebi = pebi->pebc->dst_peb;
+		break;
+
+	default:
+		BUG();
+	}
+
+	if (!desc.pebi) {
+		SSDFS_WARN("destination PEB doesn't exist\n");
+		return -ERANGE;
+	}
+
+	seg_blkbmap = &pebi->pebc->parent_si->blk_bmap;
+
+	for (i = 0; i < pebi->pebc->parent_si->pebs_count; i++) {
+		struct ssdfs_peb_container *cur_pebc;
+		struct ssdfs_peb_info *dst_peb;
+		int buffers_state;
+
+		cur_pebc = &pebi->pebc->parent_si->peb_array[i];
+
+		switch (atomic_read(&cur_pebc->items_state)) {
+		case SSDFS_PEB1_SRC_EXT_PTR_DST_CONTAINER:
+		case SSDFS_PEB2_SRC_EXT_PTR_DST_CONTAINER:
+			/* do nothing here */
+			break;
+
+		default:
+			continue;
+		};
+
+		down_read(&cur_pebc->lock);
+		dst_peb = cur_pebc->dst_peb;
+		up_read(&cur_pebc->lock);
+
+		if (dst_peb == NULL) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("dst_peb is NULL: "
+				  "peb_index %u\n",
+				  i);
+#endif /* CONFIG_SSDFS_DEBUG */
+			continue;
+		} else if (dst_peb != pebi->pebc->dst_peb)
+			continue;
+
+		peb_blkbmap = &seg_blkbmap->peb[i];
+
+		err = ssdfs_page_vector_init(desc.snapshot);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to init page vector: "
+				  "err %d\n", err);
+			return err;
+		}
+
+		desc.peb_index = (u16)i;
+
+		if (!ssdfs_peb_blk_bmap_initialized(peb_blkbmap)) {
+			SSDFS_ERR("PEB's block bitmap isn't initialized\n");
+			return -ERANGE;
+		}
+
+		down_read(&peb_blkbmap->lock);
+
+		buffers_state = atomic_read(&peb_blkbmap->buffers_state);
+		switch (buffers_state) {
+		case SSDFS_PEB_BMAP1_SRC:
+		case SSDFS_PEB_BMAP2_SRC:
+			/* valid state */
+			break;
+
+		default:
+			err = -ERANGE;
+			SSDFS_WARN("invalid buffers_state %#x\n",
+				   buffers_state);
+			goto finish_store_dependent_blk_bmap;
+		}
+
+		bmap = peb_blkbmap->src;
+		if (!bmap) {
+			err = -ERANGE;
+			SSDFS_WARN("source bitmap doesn't exist\n");
+			goto finish_store_dependent_blk_bmap;
+		}
+
+		err = ssdfs_block_bmap_lock(bmap);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to lock block bitmap: err %d\n", err);
+			goto finish_store_dependent_blk_bmap;
+		}
+
+		err = ssdfs_block_bmap_snapshot(bmap, desc.snapshot,
+						&desc.last_free_blk,
+						&desc.metadata_blks,
+						&desc.invalid_blks,
+						&desc.bytes_count);
+
+		ssdfs_block_bmap_unlock(bmap);
+
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to snapshot block bitmap: "
+				  "seg %llu, peb_index %u, err %d\n",
+				  cur_pebc->parent_si->seg_id,
+				  cur_pebc->peb_index, err);
+			goto finish_store_dependent_blk_bmap;
+		}
+
+		if (ssdfs_page_vector_count(desc.snapshot) == 0) {
+			err = -ERANGE;
+			SSDFS_ERR("empty block bitmap\n");
+			goto finish_store_dependent_blk_bmap;
+		}
+
+finish_store_dependent_blk_bmap:
+		up_read(&peb_blkbmap->lock);
+
+		if (unlikely(err))
+			return err;
+
+		err = ssdfs_peb_store_blk_bmap_fragment(&desc, bmap_hdr_off);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to store block bitmap fragment: "
+				  "peb_index %u, err %d\n",
+				  i, err);
+			return err;
+		}
+
+		ssdfs_block_bmap_forget_snapshot(desc.snapshot);
+	}
+
+	return 0;
+}
+
+static inline
+void ssdfs_prepare_blk_bmap_options(struct ssdfs_fs_info *fsi,
+				    u16 *flags, u8 *compression)
+{
+	u8 type;
+
+	*flags = fsi->metadata_options.blk_bmap.flags;
+	type = fsi->metadata_options.blk_bmap.compression;
+
+	*compression = SSDFS_BLK_BMAP_UNCOMPRESSED_BLOB;
+
+	if (*flags & SSDFS_BLK_BMAP_MAKE_COMPRESSION) {
+		switch (type) {
+		case SSDFS_BLK_BMAP_NOCOMPR_TYPE:
+			*compression = SSDFS_BLK_BMAP_UNCOMPRESSED_BLOB;
+			break;
+
+		case SSDFS_BLK_BMAP_ZLIB_COMPR_TYPE:
+			*compression = SSDFS_BLK_BMAP_ZLIB_BLOB;
+			break;
+
+		case SSDFS_BLK_BMAP_LZO_COMPR_TYPE:
+			*compression = SSDFS_BLK_BMAP_LZO_BLOB;
+			break;
+		}
+	}
+}
+
+/*
+ * ssdfs_peb_store_block_bmap() - store block bitmap into page cache
+ * @pebi: pointer on PEB object
+ * @desc: block bitmap descriptor [out]
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to store block bitmap into log.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_block_bmap(struct ssdfs_peb_info *pebi,
+				struct ssdfs_metadata_descriptor *desc,
+				pgoff_t *cur_page,
+				u32 *write_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_segment_blk_bmap *seg_blkbmap;
+	struct ssdfs_block_bitmap_header *bmap_hdr = NULL;
+	size_t bmap_hdr_size = sizeof(struct ssdfs_block_bitmap_header);
+	int items_state;
+	u8 frag_id = 0;
+	u32 bmap_hdr_off;
+	u32 pages_per_peb;
+	u16 log_start_page = 0;
+	u16 flags = 0;
+	u8 compression = SSDFS_BLK_BMAP_UNCOMPRESSED_BLOB;
+	struct page *page;
+	pgoff_t index;
+	void *kaddr;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(flags & ~SSDFS_BLK_BMAP_FLAG_MASK);
+	BUG_ON(!desc || !cur_page || !write_offset);
+
+	SSDFS_DBG("seg %llu, peb_index %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_index,
+		  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	seg_blkbmap = &pebi->pebc->parent_si->blk_bmap;
+
+	pages_per_peb = min_t(u32, fsi->leb_pages_capacity,
+				   fsi->peb_pages_capacity);
+
+	ssdfs_prepare_blk_bmap_options(fsi, &flags, &compression);
+
+	bmap_hdr_off = *write_offset;
+	*write_offset += bmap_hdr_size;
+
+	items_state = atomic_read(&pebi->pebc->items_state);
+	switch (items_state) {
+	case SSDFS_PEB1_SRC_CONTAINER:
+	case SSDFS_PEB2_SRC_CONTAINER:
+		/* Prepare source bitmap only */
+		err = ssdfs_peb_store_source_blk_bmap(pebi, items_state,
+						      compression,
+						      bmap_hdr_off,
+						      &frag_id,
+						      cur_page,
+						      write_offset);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to store source bitmap: "
+				  "cur_page %lu, write_offset %u, "
+				  "err %d\n",
+				  *cur_page, *write_offset, err);
+			goto finish_store_block_bitmap;
+		}
+		break;
+
+	case SSDFS_PEB1_SRC_PEB2_DST_CONTAINER:
+	case SSDFS_PEB2_SRC_PEB1_DST_CONTAINER:
+		if (!pebi->pebc->src_peb || !pebi->pebc->dst_peb) {
+			err = -ERANGE;
+			SSDFS_WARN("invalid src or dst PEB pointer\n");
+			goto finish_store_block_bitmap;
+		}
+
+		/*
+		 * Prepare
+		 * (1) destination bitmap
+		 * (2) source bitmap
+		 * (3) all dependent bitmaps
+		 */
+		err = ssdfs_peb_store_dst_blk_bmap(pebi, items_state,
+						   compression,
+						   bmap_hdr_off,
+						   &frag_id,
+						   cur_page,
+						   write_offset);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to store destination bitmap: "
+				  "cur_page %lu, write_offset %u, "
+				  "err %d\n",
+				  *cur_page, *write_offset, err);
+			goto finish_store_block_bitmap;
+		}
+
+		err = ssdfs_peb_store_source_blk_bmap(pebi, items_state,
+						      compression,
+						      bmap_hdr_off,
+						      &frag_id,
+						      cur_page,
+						      write_offset);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to store source bitmap: "
+				  "cur_page %lu, write_offset %u, "
+				  "err %d\n",
+				  *cur_page, *write_offset, err);
+			goto finish_store_block_bitmap;
+		}
+
+		err = ssdfs_peb_store_dependent_blk_bmap(pebi, items_state,
+							 compression,
+							 bmap_hdr_off,
+							 &frag_id,
+							 cur_page,
+							 write_offset);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to store dependent bitmaps: "
+				  "cur_page %lu, write_offset %u, "
+				  "err %d\n",
+				  *cur_page, *write_offset, err);
+			goto finish_store_block_bitmap;
+		}
+		break;
+
+	case SSDFS_PEB1_DST_CONTAINER:
+	case SSDFS_PEB2_DST_CONTAINER:
+		/*
+		 * Prepare
+		 * (1) source bitmap
+		 * (2) all dependent bitmaps
+		 */
+		err = ssdfs_peb_store_source_blk_bmap(pebi, items_state,
+						      compression,
+						      bmap_hdr_off,
+						      &frag_id,
+						      cur_page,
+						      write_offset);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to store source bitmap: "
+				  "cur_page %lu, write_offset %u, "
+				  "err %d\n",
+				  *cur_page, *write_offset, err);
+			goto finish_store_block_bitmap;
+		}
+
+		err = ssdfs_peb_store_dependent_blk_bmap(pebi, items_state,
+							 compression,
+							 bmap_hdr_off,
+							 &frag_id,
+							 cur_page,
+							 write_offset);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to store dependent bitmaps: "
+				  "cur_page %lu, write_offset %u, "
+				  "err %d\n",
+				  *cur_page, *write_offset, err);
+			goto finish_store_block_bitmap;
+		}
+		break;
+
+	default:
+		err = -ERANGE;
+		SSDFS_WARN("invalid items_state %#x\n",
+			   items_state);
+		break;
+	}
+
+	if (pebi->current_log.start_page >= pages_per_peb) {
+		err = -ERANGE;
+		SSDFS_ERR("log_start_page %u >= pages_per_peb %u\n",
+			  log_start_page, pages_per_peb);
+		goto finish_store_block_bitmap;
+	}
+
+	desc->offset = cpu_to_le32(bmap_hdr_off +
+			    (pebi->current_log.start_page * fsi->pagesize));
+
+	index = ssdfs_write_offset_to_mem_page_index(fsi,
+					     pebi->current_log.start_page,
+					     bmap_hdr_off);
+
+	page = ssdfs_page_array_get_page_locked(&pebi->cache, index);
+	if (IS_ERR_OR_NULL(page)) {
+		SSDFS_ERR("fail to get cache page: index %lu\n", index);
+		err = -ENOMEM;
+		goto finish_store_block_bitmap;
+	}
+
+	kaddr = kmap_local_page(page);
+
+	bmap_hdr = SSDFS_BLKBMP_HDR((u8 *)kaddr +
+				    (bmap_hdr_off % PAGE_SIZE));
+
+	bmap_hdr->magic.common = cpu_to_le32(SSDFS_SUPER_MAGIC);
+	bmap_hdr->magic.key = cpu_to_le16(SSDFS_BLK_BMAP_MAGIC);
+	bmap_hdr->magic.version.major = SSDFS_MAJOR_REVISION;
+	bmap_hdr->magic.version.minor = SSDFS_MINOR_REVISION;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(frag_id == 0);
+#endif /* CONFIG_SSDFS_DEBUG */
+	bmap_hdr->fragments_count = cpu_to_le16(frag_id);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(*write_offset <= bmap_hdr_off);
+	BUG_ON(*write_offset <= (bmap_hdr_off + bmap_hdr_size));
+#endif /* CONFIG_SSDFS_DEBUG */
+	bmap_hdr->bytes_count = cpu_to_le32(*write_offset - bmap_hdr_off);
+	desc->size = bmap_hdr->bytes_count;
+
+	pebi->current_log.prev_log_bmap_bytes =
+			le32_to_cpu(bmap_hdr->bytes_count);
+
+	bmap_hdr->flags = flags;
+	bmap_hdr->type = compression;
+
+	desc->check.bytes = cpu_to_le16(bmap_hdr_size);
+
+	switch (compression) {
+	case SSDFS_BLK_BMAP_ZLIB_BLOB:
+		desc->check.flags = cpu_to_le16(SSDFS_CRC32 |
+						SSDFS_ZLIB_COMPRESSED);
+		break;
+
+	case SSDFS_BLK_BMAP_LZO_BLOB:
+		desc->check.flags = cpu_to_le16(SSDFS_CRC32 |
+						SSDFS_LZO_COMPRESSED);
+		break;
+
+	default:
+		desc->check.flags = cpu_to_le16(SSDFS_CRC32);
+		break;
+	}
+
+	err = ssdfs_calculate_csum(&desc->check, bmap_hdr, bmap_hdr_size);
+	if (unlikely(err)) {
+		SSDFS_ERR("unable to calculate checksum: err %d\n", err);
+		goto finish_bmap_hdr_preparation;
+	}
+
+	pebi->current_log.seg_flags |= SSDFS_SEG_HDR_HAS_BLK_BMAP;
+
+finish_bmap_hdr_preparation:
+	flush_dcache_page(page);
+	kunmap_local(kaddr);
+
+	ssdfs_set_page_private(page, 0);
+	SetPageUptodate(page);
+
+	err = ssdfs_page_array_set_page_dirty(&pebi->cache, index);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to set page %lu dirty: "
+			  "err %d\n",
+			  index, err);
+	}
+
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+finish_store_block_bitmap:
+	return err;
+}
+
+/*
+ * is_peb_area_empty() - check that PEB's area is empty
+ * @pebi: pointer on PEB object
+ * @area_type: type of area
+ */
+static inline
+bool is_peb_area_empty(struct ssdfs_peb_info *pebi, int area_type)
+{
+	struct ssdfs_peb_area *area;
+	size_t blk_table_size = sizeof(struct ssdfs_area_block_table);
+	bool is_empty = false;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	area = &pebi->current_log.area[area_type];
+
+	if (area->has_metadata)
+		is_empty = area->write_offset == blk_table_size;
+	else
+		is_empty = area->write_offset == 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("area_type %#x, write_offset %u, is_empty %d\n",
+		  area_type, area->write_offset, (int)is_empty);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	return is_empty;
+}
+
+/*
+ * ssdfs_peb_copy_area_pages_into_cache() - copy area pages into cache
+ * @pebi: pointer on PEB object
+ * @area_type: type of area
+ * @desc: descriptor of metadata area
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to copy area pages into log's page cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ENODATA    - area is empty.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_copy_area_pages_into_cache(struct ssdfs_peb_info *pebi,
+					 int area_type,
+					 struct ssdfs_metadata_descriptor *desc,
+					 pgoff_t *cur_page,
+					 u32 *write_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_peb_area *area;
+	size_t blk_table_size = sizeof(struct ssdfs_area_block_table);
+	struct pagevec pvec;
+	struct ssdfs_page_array *smap, *dmap;
+	pgoff_t page_index, end, pages_count, range_len;
+	struct page *page;
+	u32 area_offset, area_size = 0;
+	u16 log_start_page;
+	u32 read_bytes = 0;
+	u32 area_write_offset = 0;
+	u16 flags;
+	int i;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(area_type >= SSDFS_LOG_AREA_MAX);
+	BUG_ON(!desc || !cur_page || !write_offset);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	area = &pebi->current_log.area[area_type];
+	log_start_page = pebi->current_log.start_page;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "area_type %#x, area->write_offset %u, "
+		  "area->compressed_offset %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  area_type, area->write_offset,
+		  area->compressed_offset,
+		  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (is_peb_area_empty(pebi, area_type)) {
+		SSDFS_DBG("area %#x is empty\n", area_type);
+		return -ENODATA;
+	}
+
+	smap = &area->array;
+	dmap = &pebi->cache;
+
+	switch (area_type) {
+	case SSDFS_LOG_BLK_DESC_AREA:
+		flags = fsi->metadata_options.blk2off_tbl.flags;
+		if (flags & SSDFS_BLK2OFF_TBL_MAKE_COMPRESSION)
+			area_write_offset = area->compressed_offset;
+		else
+			area_write_offset = area->write_offset;
+		break;
+
+	default:
+		area_write_offset = area->write_offset;
+		break;
+	}
+
+	area_offset = *write_offset;
+	area_size = area_write_offset;
+
+	desc->offset = cpu_to_le32(area_offset +
+					(log_start_page * fsi->pagesize));
+	desc->size = cpu_to_le32(area_size);
+
+	if (area->has_metadata) {
+		void *kaddr;
+		u8 compression = fsi->metadata_options.blk2off_tbl.compression;
+		u16 metadata_flags = SSDFS_CRC32;
+
+		switch (area_type) {
+		case SSDFS_LOG_BLK_DESC_AREA:
+			flags = fsi->metadata_options.blk2off_tbl.flags;
+			if (flags & SSDFS_BLK2OFF_TBL_MAKE_COMPRESSION) {
+				switch (compression) {
+				case SSDFS_BLK2OFF_TBL_ZLIB_COMPR_TYPE:
+					metadata_flags |= SSDFS_ZLIB_COMPRESSED;
+					break;
+
+				case SSDFS_BLK2OFF_TBL_LZO_COMPR_TYPE:
+					metadata_flags |= SSDFS_LZO_COMPRESSED;
+					break;
+
+				default:
+					/* do nothing */
+					break;
+				}
+			}
+			break;
+
+		default:
+			/* do nothing */
+			break;
+		}
+
+		page = ssdfs_page_array_get_page_locked(smap, 0);
+		if (IS_ERR_OR_NULL(page)) {
+			SSDFS_ERR("fail to get page of area %#x\n",
+				  area_type);
+			return -ERANGE;
+		}
+
+		kaddr = kmap_local_page(page);
+		desc->check.bytes = cpu_to_le16(blk_table_size);
+		desc->check.flags = cpu_to_le16(metadata_flags);
+		err = ssdfs_calculate_csum(&desc->check, kaddr, blk_table_size);
+		kunmap_local(kaddr);
+		ssdfs_unlock_page(page);
+		ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("page %p, count %d\n",
+			  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (unlikely(err)) {
+			SSDFS_ERR("unable to calculate checksum: err %d\n",
+				  err);
+			return err;
+		}
+
+		err = ssdfs_page_array_set_page_dirty(smap, 0);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to set page dirty: err %d\n",
+				  err);
+			return err;
+		}
+	}
+
+	pagevec_init(&pvec);
+
+	page_index = 0;
+	pages_count = area_write_offset + PAGE_SIZE - 1;
+	pages_count >>= PAGE_SHIFT;
+
+	while (page_index < pages_count) {
+		int i;
+
+		range_len = min_t(pgoff_t,
+				  (pgoff_t)PAGEVEC_SIZE,
+				  (pgoff_t)(pages_count - page_index));
+		end = page_index + range_len - 1;
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("page_index %lu, pages_count %lu\n",
+			  page_index, pages_count);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		err = ssdfs_page_array_lookup_range(smap, &page_index, end,
+						    SSDFS_DIRTY_PAGE_TAG,
+						    PAGEVEC_SIZE,
+						    &pvec);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to find any dirty pages: err %d\n",
+				  err);
+			return err;
+		}
+
+		for (i = 0; i < pagevec_count(&pvec); i++) {
+			struct page *page1 = pvec.pages[i], *page2;
+			pgoff_t src_index = page1->index;
+			u32 src_len, dst_len, copy_len;
+			u32 src_off, dst_off;
+			u32 rest_len = PAGE_SIZE;
+
+			if (read_bytes == area_size)
+				goto finish_pagevec_copy;
+			else if (read_bytes > area_size) {
+				err = -E2BIG;
+				SSDFS_ERR("too many pages: "
+					  "pages_count %u, area_size %u\n",
+					  pagevec_count(&pvec),
+					  area_size);
+				goto finish_current_copy;
+			}
+
+			src_off = 0;
+
+try_copy_area_data:
+			ssdfs_lock_page(page1);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page1, page_ref_count(page1));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			if (*write_offset >= PAGE_SIZE)
+				dst_off = *write_offset % PAGE_SIZE;
+			else
+				dst_off = *write_offset;
+
+			src_len = min_t(u32, area_size - read_bytes, rest_len);
+			dst_len = min_t(u32, PAGE_SIZE, PAGE_SIZE - dst_off);
+			copy_len = min_t(u32, src_len, dst_len);
+
+			page2 = ssdfs_page_array_grab_page(dmap, *cur_page);
+			if (unlikely(IS_ERR_OR_NULL(page2))) {
+				err = -ENOMEM;
+				SSDFS_ERR("fail to grab page: index %lu\n",
+					  *cur_page);
+				goto unlock_page1;
+			}
+
+			err = ssdfs_memcpy_page(page2, dst_off, PAGE_SIZE,
+						page1, src_off, PAGE_SIZE,
+						copy_len);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to copy: "
+					  "src_off %u, dst_off %u, "
+					  "copy_len %u\n",
+					  src_off, dst_off, copy_len);
+				goto unlock_page2;
+			}
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("src_off %u, dst_off %u, src_len %u, "
+				  "dst_len %u, copy_len %u, "
+				  "write_offset %u, cur_page %lu, "
+				  "page_index %d\n",
+				  src_off, dst_off, src_len, dst_len, copy_len,
+				  *write_offset, *cur_page, i);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			if (PageDirty(page1)) {
+				err = ssdfs_page_array_set_page_dirty(dmap,
+								     *cur_page);
+				if (unlikely(err)) {
+					SSDFS_ERR("fail to set page dirty: "
+						  "page_index %lu, err %d\n",
+						  *cur_page, err);
+					goto unlock_page2;
+				}
+			} else {
+				err = -ERANGE;
+				SSDFS_ERR("page %d is not dirty\n", i);
+				goto unlock_page2;
+			}
+
+unlock_page2:
+			ssdfs_unlock_page(page2);
+			ssdfs_put_page(page2);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page2, page_ref_count(page2));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+unlock_page1:
+			ssdfs_unlock_page(page1);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page1, page_ref_count(page1));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+finish_current_copy:
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to copy page: "
+					  " from %lu to %lu, err %d\n",
+					  src_index, *cur_page, err);
+				goto fail_copy_area_pages;
+			}
+
+			read_bytes += copy_len;
+			*write_offset += copy_len;
+			rest_len -= copy_len;
+
+			if ((dst_off + copy_len) >= PAGE_SIZE)
+				++(*cur_page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("read_bytes %u, area_size %u, "
+				  "write_offset %u, copy_len %u, rest_len %u\n",
+				  read_bytes, area_size,
+				  *write_offset, copy_len, rest_len);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			if (read_bytes == area_size) {
+				err = ssdfs_page_array_clear_dirty_page(smap,
+								page_index + i);
+				if (unlikely(err)) {
+					SSDFS_ERR("fail to mark page clean: "
+						  "page_index %lu\n",
+						  page_index + i);
+					goto fail_copy_area_pages;
+				} else
+					goto finish_pagevec_copy;
+			} else if ((src_off + copy_len) < PAGE_SIZE) {
+				src_off += copy_len;
+				goto try_copy_area_data;
+			} else {
+				err = ssdfs_page_array_clear_dirty_page(smap,
+								page_index + i);
+				if (unlikely(err)) {
+					SSDFS_ERR("fail to mark page clean: "
+						  "page_index %lu\n",
+						  page_index + i);
+					goto fail_copy_area_pages;
+				}
+			}
+		}
+
+finish_pagevec_copy:
+		page_index += PAGEVEC_SIZE;
+
+		for (i = 0; i < pagevec_count(&pvec); i++) {
+			page = pvec.pages[i];
+			ssdfs_put_page(page);
+		}
+
+		pagevec_reinit(&pvec);
+		cond_resched();
+	};
+
+	err = ssdfs_page_array_release_all_pages(smap);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to release area's pages: "
+			  "err %d\n", err);
+		goto finish_copy_area_pages;
+	}
+
+	pebi->current_log.seg_flags |= SSDFS_AREA_TYPE2FLAG(area_type);
+
+	return 0;
+
+fail_copy_area_pages:
+	for (i = 0; i < pagevec_count(&pvec); i++) {
+		page = pvec.pages[i];
+		ssdfs_put_page(page);
+	}
+
+	pagevec_reinit(&pvec);
+
+finish_copy_area_pages:
+	return err;
+}
+
+/*
+ * ssdfs_peb_move_area_pages_into_cache() - move area pages into cache
+ * @pebi: pointer on PEB object
+ * @area_type: type of area
+ * @desc: descriptor of metadata area
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to move area pages into log's page cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ENODATA    - area is empty.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_move_area_pages_into_cache(struct ssdfs_peb_info *pebi,
+					 int area_type,
+					 struct ssdfs_metadata_descriptor *desc,
+					 pgoff_t *cur_page,
+					 u32 *write_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_peb_area *area;
+	size_t blk_table_size = sizeof(struct ssdfs_area_block_table);
+	struct pagevec pvec;
+	struct ssdfs_page_array *smap, *dmap;
+	pgoff_t page_index, end, pages_count, range_len;
+	struct page *page;
+	u32 area_offset, area_size;
+	void *kaddr;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(area_type >= SSDFS_LOG_AREA_MAX);
+	BUG_ON(!desc || !cur_page || !write_offset);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	area = &pebi->current_log.area[area_type];
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "area_type %#x, area->write_offset %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  area_type, area->write_offset,
+		  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (is_peb_area_empty(pebi, area_type)) {
+		SSDFS_DBG("area %#x is empty\n", area_type);
+		return -ENODATA;
+	}
+
+	smap = &area->array;
+	dmap = &pebi->cache;
+
+	area_offset = *write_offset;
+	area_size = area->write_offset;
+
+	desc->offset = cpu_to_le32(area_offset +
+				(pebi->current_log.start_page * fsi->pagesize));
+
+	desc->size = cpu_to_le32(area_size);
+
+	if (area->has_metadata) {
+		page = ssdfs_page_array_get_page_locked(smap, 0);
+		if (IS_ERR_OR_NULL(page)) {
+			SSDFS_ERR("fail to get page of area %#x\n",
+				  area_type);
+			return -ERANGE;
+		}
+
+		kaddr = kmap_local_page(page);
+		desc->check.bytes = cpu_to_le16(blk_table_size);
+		desc->check.flags = cpu_to_le16(SSDFS_CRC32);
+		err = ssdfs_calculate_csum(&desc->check, kaddr, blk_table_size);
+		kunmap_local(kaddr);
+		ssdfs_unlock_page(page);
+		ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("page %p, count %d\n",
+			  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (unlikely(err)) {
+			SSDFS_ERR("unable to calculate checksum: err %d\n",
+				  err);
+			return err;
+		}
+
+		err = ssdfs_page_array_set_page_dirty(smap, 0);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to set page dirty: err %d\n",
+				  err);
+			return err;
+		}
+	}
+
+	pagevec_init(&pvec);
+
+	page_index = 0;
+	pages_count = area->write_offset + PAGE_SIZE - 1;
+	pages_count >>= PAGE_SHIFT;
+
+	while (page_index < pages_count) {
+		int i;
+
+		range_len = min_t(pgoff_t,
+				  (pgoff_t)PAGEVEC_SIZE,
+				  (pgoff_t)(pages_count - page_index));
+		end = page_index + range_len - 1;
+
+		err = ssdfs_page_array_lookup_range(smap, &page_index, end,
+						    SSDFS_DIRTY_PAGE_TAG,
+						    PAGEVEC_SIZE,
+						    &pvec);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to find any dirty pages: err %d\n",
+				  err);
+			return err;
+		}
+
+		for (i = 0; i < pagevec_count(&pvec); i++) {
+			struct page *page = pvec.pages[i], *page2;
+			pgoff_t src_off = page->index;
+
+			ssdfs_lock_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			page2 = ssdfs_page_array_delete_page(smap, src_off);
+			if (IS_ERR_OR_NULL(page2)) {
+				err = !page2 ? -ERANGE : PTR_ERR(page2);
+				SSDFS_ERR("fail to delete page %lu: err %d\n",
+					  src_off, err);
+				goto finish_current_move;
+			}
+
+			WARN_ON(page2 != page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("cur_page %lu, write_offset %u, "
+				  "i %d, pvec_count %u\n",
+				  *cur_page, *write_offset,
+				  i, pagevec_count(&pvec));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			page->index = *cur_page;
+
+			err = ssdfs_page_array_add_page(dmap, page, *cur_page);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to add page %lu: err %d\n",
+					  *cur_page, err);
+				goto finish_current_move;
+			}
+
+			if (PageDirty(page)) {
+				err = ssdfs_page_array_set_page_dirty(dmap,
+								     *cur_page);
+				if (unlikely(err)) {
+					SSDFS_ERR("fail to set page dirty: "
+						  "page_index %lu, err %d\n",
+						  *cur_page, err);
+					goto finish_current_move;
+				}
+			} else {
+				err = -ERANGE;
+				SSDFS_ERR("page %d is not dirty\n", i);
+				goto finish_current_move;
+			}
+
+			pvec.pages[i] = NULL;
+
+finish_current_move:
+			ssdfs_unlock_page(page);
+			ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("page %p, count %d\n",
+				  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			if (unlikely(err)) {
+				for (i = 0; i < pagevec_count(&pvec); i++) {
+					page = pvec.pages[i];
+					if (!page)
+						continue;
+					ssdfs_put_page(page);
+				}
+
+				pagevec_reinit(&pvec);
+				SSDFS_ERR("fail to move page: "
+					  " from %lu to %lu, err %d\n",
+					  src_off, *cur_page, err);
+				return err;
+			}
+
+			(*cur_page)++;
+			*write_offset += PAGE_SIZE;
+		}
+
+		page_index += PAGEVEC_SIZE;
+
+		pagevec_reinit(&pvec);
+		cond_resched();
+	};
+
+	pebi->current_log.seg_flags |= SSDFS_AREA_TYPE2FLAG(area_type);
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_store_blk_desc_table() - try to store block descriptor table
+ * @pebi: pointer on PEB object
+ * @desc: descriptor of metadata area
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to store block descriptor into log's page cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ENODATA    - area is empty.
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_blk_desc_table(struct ssdfs_peb_info *pebi,
+				   struct ssdfs_metadata_descriptor *desc,
+				   pgoff_t *cur_page,
+				   u32 *write_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_peb_area *area;
+	struct ssdfs_fragment_desc *meta_desc;
+	struct ssdfs_fragments_chain_header *chain_hdr;
+	struct ssdfs_peb_temp_buffer *buf;
+	int area_type = SSDFS_LOG_BLK_DESC_AREA;
+	u16 flags;
+	size_t uncompr_size;
+	size_t compr_size = 0;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!desc || !cur_page || !write_offset);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	area = &pebi->current_log.area[area_type];
+	chain_hdr = &area->metadata.area.blk_desc.table.chain_hdr;
+	buf = &area->metadata.area.blk_desc.flush_buf;
+	flags = fsi->metadata_options.blk2off_tbl.flags;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "area->write_offset %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  area->write_offset,
+		  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (is_peb_area_empty(pebi, area_type)) {
+		SSDFS_DBG("area %#x is empty\n", area_type);
+		return -ENODATA;
+	}
+
+	meta_desc = ssdfs_peb_get_area_cur_frag_desc(pebi, area_type);
+	if (IS_ERR(meta_desc)) {
+		SSDFS_ERR("fail to get current fragment descriptor: "
+			  "err %d\n",
+			  (int)PTR_ERR(meta_desc));
+		return PTR_ERR(meta_desc);
+	} else if (!meta_desc) {
+		err = -ERANGE;
+		SSDFS_ERR("fail to get current fragment descriptor: "
+			  "err %d\n",
+			  err);
+		return err;
+	}
+
+	uncompr_size = le32_to_cpu(meta_desc->uncompr_size);
+
+	if (uncompr_size == 0) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("latest fragment of blk desc table is empty: "
+			  "seg %llu, peb %llu, current_log.start_page %u, "
+			  "area->write_offset %u, "
+			  "cur_page %lu, write_offset %u\n",
+			  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			  pebi->current_log.start_page,
+			  area->write_offset,
+			  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+		goto copy_area_pages_into_cache;
+	}
+
+	if (flags & SSDFS_BLK2OFF_TBL_MAKE_COMPRESSION) {
+#ifdef CONFIG_SSDFS_DEBUG
+		BUG_ON(!buf->ptr);
+
+		if (buf->write_offset >= buf->size) {
+			SSDFS_ERR("invalid request: "
+				  "buf->write_offset %u, buf->size %zu\n",
+				  buf->write_offset, buf->size);
+			return -ERANGE;
+		}
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		meta_desc->flags = SSDFS_FRAGMENT_HAS_CSUM;
+
+		if (uncompr_size > buf->size) {
+			SSDFS_ERR("invalid state: "
+				  "uncompr_size %zu > buf->size %zu\n",
+				  uncompr_size, buf->size);
+			return -ERANGE;
+		}
+
+		meta_desc->checksum = ssdfs_crc32_le(buf->ptr, uncompr_size);
+
+		if (le32_to_cpu(meta_desc->checksum) == 0) {
+			SSDFS_WARN("checksum is invalid: "
+				   "seg %llu, peb %llu, bytes_count %zu\n",
+				   pebi->pebc->parent_si->seg_id,
+				   pebi->peb_id,
+				   uncompr_size);
+			return -ERANGE;
+		}
+
+		err = ssdfs_peb_compress_blk_descs_fragment(pebi,
+							    uncompr_size,
+							    &compr_size);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to compress blk desc fragment: "
+				  "err %d\n", err);
+			return err;
+		}
+
+		meta_desc->offset = cpu_to_le32(area->compressed_offset);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		WARN_ON(compr_size > U16_MAX);
+#endif /* CONFIG_SSDFS_DEBUG */
+		meta_desc->compr_size = cpu_to_le16((u16)compr_size);
+
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("offset %u, compr_size %u, "
+			  "uncompr_size %u, checksum %#x\n",
+			  le32_to_cpu(meta_desc->offset),
+			  le16_to_cpu(meta_desc->compr_size),
+			  le16_to_cpu(meta_desc->uncompr_size),
+			  le32_to_cpu(meta_desc->checksum));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		area->compressed_offset += compr_size;
+		le32_add_cpu(&chain_hdr->compr_bytes, compr_size);
+	}
+
+	err = ssdfs_peb_store_area_block_table(pebi, area_type, 0);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to store area's block table: "
+			  "area %#x, err %d\n",
+			  area_type, err);
+		return err;
+	}
+
+copy_area_pages_into_cache:
+	err = ssdfs_peb_copy_area_pages_into_cache(pebi, area_type,
+						   desc, cur_page,
+						   write_offset);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to move pages in the cache: err %d\n",
+			  err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_store_log_footer() - store log footer
+ * @pebi: pointer on PEB object
+ * @flags: log footer's flags
+ * @hdr_desc: log footer's metadata descriptor in header
+ * @lf_desc: log footer's metadata descriptors array
+ * @array_size: count of items in array
+ * @cur_segs: current segment IDs array
+ * @cur_segs_size: size of segment IDs array size in bytes
+ * @cur_page: pointer on current page value [in|out]
+ * @write_offset: pointer on write offset value [in|out]
+ *
+ * This function tries to store log footer into PEB's page cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ENOMEM     - fail to allocate memory page.
+ */
+static
+int ssdfs_peb_store_log_footer(struct ssdfs_peb_info *pebi,
+				u32 flags,
+				struct ssdfs_metadata_descriptor *hdr_desc,
+				struct ssdfs_metadata_descriptor *lf_desc,
+				size_t array_size,
+				__le64 *cur_segs,
+				size_t cur_segs_size,
+				pgoff_t *cur_page,
+				u32 *write_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_log_footer *footer;
+	size_t desc_size = sizeof(struct ssdfs_metadata_descriptor);
+	size_t array_bytes = desc_size * array_size;
+	int padding;
+	u32 log_pages;
+	struct page *page;
+	u32 area_offset, area_size;
+	u64 last_log_time;
+	u64 last_log_cno;
+	int i;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!hdr_desc || !lf_desc || !cur_segs);
+	BUG_ON(!cur_page || !write_offset);
+	BUG_ON(array_size != SSDFS_LOG_FOOTER_DESC_MAX);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "cur_page %lu, write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  *cur_page, *write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	area_offset = *write_offset;
+	area_size = sizeof(struct ssdfs_log_footer);
+
+	*write_offset += max_t(u32, PAGE_SIZE, area_size);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(flags & ~SSDFS_LOG_FOOTER_FLAG_MASK);
+	BUG_ON(((*write_offset + fsi->pagesize - 1) >> fsi->log_pagesize) >
+		pebi->log_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	log_pages = (*write_offset + fsi->pagesize - 1) / fsi->pagesize;
+
+	padding = *cur_page % pebi->log_pages;
+	padding = pebi->log_pages - padding;
+	padding--;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("area_offset %u, write_offset %u, "
+		  "log_pages %u, padding %d, "
+		  "cur_page %lu\n",
+		  area_offset, *write_offset,
+		  log_pages, padding,
+		  *cur_page);
+
+	if (padding > 1) {
+		SSDFS_WARN("padding is big: "
+			   "seg %llu, peb %llu, current_log.start_page %u, "
+			   "cur_page %lu, write_offset %u, "
+			   "padding %d\n",
+			   pebi->pebc->parent_si->seg_id,
+			   pebi->peb_id,
+			   pebi->current_log.start_page,
+			   *cur_page, *write_offset,
+			   padding);
+	}
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (padding > 0) {
+		/*
+		 * Align the log_pages and log_bytes.
+		 */
+		log_pages += padding;
+		*write_offset = log_pages * fsi->pagesize;
+		area_offset = *write_offset - fsi->pagesize;
+
+		for (i = 0; i < padding; i++) {
+			page = ssdfs_page_array_grab_page(&pebi->cache,
+							  *cur_page);
+			if (IS_ERR_OR_NULL(page)) {
+				SSDFS_ERR("fail to get cache page: index %lu\n",
+					  *cur_page);
+				return -ENOMEM;
+			}
+
+			ssdfs_memset_page(page, 0, PAGE_SIZE, 0xFF, PAGE_SIZE);
+
+			ssdfs_set_page_private(page, 0);
+			SetPageUptodate(page);
+
+			err = ssdfs_page_array_set_page_dirty(&pebi->cache,
+							      *cur_page);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to set page dirty: "
+					  "page_index %lu, err %d\n",
+					  *cur_page, err);
+			}
+
+			ssdfs_unlock_page(page);
+			ssdfs_put_page(page);
+
+			if (unlikely(err))
+				return err;
+
+			(*cur_page)++;
+		}
+	}
+
+	page = ssdfs_page_array_grab_page(&pebi->cache, *cur_page);
+	if (IS_ERR_OR_NULL(page)) {
+		SSDFS_ERR("fail to get cache page: index %lu\n",
+			  *cur_page);
+		return -ENOMEM;
+	}
+
+	footer = kmap_local_page(page);
+	memset(footer, 0xFF, PAGE_SIZE);
+	ssdfs_memcpy(footer->desc_array, 0, array_bytes,
+		     lf_desc, 0, array_bytes,
+		     array_bytes);
+
+	last_log_time = pebi->current_log.last_log_time;
+	last_log_cno = pebi->current_log.last_log_cno;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(pebi->peb_create_time > last_log_time);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_prepare_volume_state_info_for_commit(fsi, SSDFS_MOUNTED_FS,
+							 cur_segs,
+							 cur_segs_size,
+							 last_log_time,
+							 last_log_cno,
+							 &footer->volume_state);
+
+	if (!err) {
+		err = ssdfs_prepare_log_footer_for_commit(fsi, log_pages,
+							  flags,
+							  last_log_time,
+							  last_log_cno,
+							  footer);
+
+		footer->peb_create_time = cpu_to_le64(pebi->peb_create_time);
+	}
+
+	if (!err) {
+		hdr_desc->offset = cpu_to_le32(area_offset +
+				(pebi->current_log.start_page * fsi->pagesize));
+		hdr_desc->size = cpu_to_le32(area_size);
+
+		ssdfs_memcpy(&hdr_desc->check,
+			     0, sizeof(struct ssdfs_metadata_check),
+			     &footer->volume_state.check,
+			     0, sizeof(struct ssdfs_metadata_check),
+			     sizeof(struct ssdfs_metadata_check));
+	}
+
+	flush_dcache_page(page);
+	kunmap_local(footer);
+
+	ssdfs_set_page_private(page, 0);
+	SetPageUptodate(page);
+
+	err = ssdfs_page_array_set_page_dirty(&pebi->cache, *cur_page);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to set page dirty: "
+			  "page_index %lu, err %d\n",
+			  *cur_page, err);
+	}
+
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to store log footer: "
+			   "seg %llu, peb %llu, current_log.start_page %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   pebi->current_log.start_page, err);
+		return err;
+	}
+
+	pebi->current_log.seg_flags |= SSDFS_LOG_HAS_FOOTER;
+
+	(*cur_page)++;
+
+	return 0;
+}
+
+/*
+ * ssdfs_extract_src_peb_migration_id() - prepare src PEB's migration_id
+ * @pebi: pointer on PEB object
+ * @prev_id: pointer on previous PEB's peb_migration_id [out]
+ * @cur_id: pointer on current PEB's peb_migration_id [out]
+ */
+static inline
+int ssdfs_extract_src_peb_migration_id(struct ssdfs_peb_info *pebi,
+					u8 *prev_id, u8 *cur_id)
+{
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !pebi->pebc->src_peb);
+	BUG_ON(!prev_id || !cur_id);
+	BUG_ON(!rwsem_is_locked(&pebi->pebc->lock));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*prev_id = SSDFS_PEB_UNKNOWN_MIGRATION_ID;
+	*cur_id = SSDFS_PEB_UNKNOWN_MIGRATION_ID;
+
+	if (pebi != pebi->pebc->src_peb) {
+		SSDFS_ERR("pebi %p != src_peb %p\n",
+			  pebi, pebi->pebc->src_peb);
+		return -ERANGE;
+	}
+
+	*cur_id = ssdfs_get_peb_migration_id_checked(pebi);
+	if (unlikely(*cur_id < 0)) {
+		err = *cur_id;
+		*cur_id = SSDFS_PEB_UNKNOWN_MIGRATION_ID;
+		SSDFS_ERR("fail to get migration_id: "
+			  "seg %llu, peb %llu, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  err);
+		return err;
+	}
+
+	*prev_id = ssdfs_define_prev_peb_migration_id(pebi);
+	if (!is_peb_migration_id_valid(*prev_id)) {
+		err = *prev_id;
+		*prev_id = SSDFS_PEB_UNKNOWN_MIGRATION_ID;
+		SSDFS_ERR("fail to define prev migration_id: "
+			  "seg %llu, peb %llu, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  err);
+		return -ERANGE;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_extract_dst_peb_migration_id() - prepare dst PEB's migration_id
+ * @pebi: pointer on PEB object
+ * @prev_id: pointer on previous PEB's peb_migration_id [out]
+ * @cur_id: pointer on current PEB's peb_migration_id [out]
+ */
+static inline
+int ssdfs_extract_dst_peb_migration_id(struct ssdfs_peb_info *pebi,
+					u8 *prev_id, u8 *cur_id)
+{
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->src_peb || !pebi->pebc->dst_peb);
+	BUG_ON(!prev_id || !cur_id);
+	BUG_ON(!rwsem_is_locked(&pebi->pebc->lock));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	*prev_id = SSDFS_PEB_UNKNOWN_MIGRATION_ID;
+	*cur_id = SSDFS_PEB_UNKNOWN_MIGRATION_ID;
+
+	*cur_id = ssdfs_get_peb_migration_id_checked(pebi->pebc->dst_peb);
+	if (unlikely(*cur_id < 0)) {
+		err = *cur_id;
+		*cur_id = SSDFS_PEB_UNKNOWN_MIGRATION_ID;
+		SSDFS_ERR("fail to get migration_id: "
+			  "seg %llu, peb %llu, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  err);
+		return err;
+	}
+
+	*prev_id = ssdfs_get_peb_migration_id_checked(pebi->pebc->src_peb);
+	if (unlikely(*prev_id < 0)) {
+		err = *prev_id;
+		*prev_id = SSDFS_PEB_UNKNOWN_MIGRATION_ID;
+		SSDFS_ERR("fail to get migration_id: "
+			  "seg %llu, peb %llu, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id,
+			  err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_store_peb_migration_id() - store peb_migration_id into header
+ * @pebi: pointer on PEB object
+ * @hdr: pointer on segment header [out]
+ */
+static
+int ssdfs_store_peb_migration_id(struct ssdfs_peb_info *pebi,
+				 struct ssdfs_segment_header *hdr)
+{
+	int items_state;
+	u8 prev_id = SSDFS_PEB_UNKNOWN_MIGRATION_ID;
+	u8 cur_id = SSDFS_PEB_UNKNOWN_MIGRATION_ID;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!rwsem_is_locked(&pebi->pebc->lock));
+
+	SSDFS_DBG("seg %llu, peb %llu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	items_state = atomic_read(&pebi->pebc->items_state);
+	switch (items_state) {
+	case SSDFS_PEB1_SRC_CONTAINER:
+	case SSDFS_PEB2_SRC_CONTAINER:
+#ifdef CONFIG_SSDFS_DEBUG
+		BUG_ON(!pebi->pebc->src_peb || pebi->pebc->dst_peb);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		err = ssdfs_extract_src_peb_migration_id(pebi,
+							 &prev_id,
+							 &cur_id);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to extract peb_migration_id: "
+				  "seg %llu, peb %llu, err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id, err);
+			return err;
+		}
+		break;
+
+	case SSDFS_PEB1_DST_CONTAINER:
+	case SSDFS_PEB2_DST_CONTAINER:
+#ifdef CONFIG_SSDFS_DEBUG
+		BUG_ON(pebi->pebc->src_peb || !pebi->pebc->dst_peb);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		if (pebi != pebi->pebc->dst_peb) {
+			SSDFS_ERR("pebi %p != dst_peb %p\n",
+				  pebi, pebi->pebc->dst_peb);
+			return -ERANGE;
+		}
+
+		cur_id = ssdfs_get_peb_migration_id_checked(pebi);
+		if (unlikely(cur_id < 0)) {
+			err = cur_id;
+			SSDFS_ERR("fail to get migration_id: "
+				  "seg %llu, peb %llu, err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id,
+				  err);
+			return err;
+		}
+		break;
+
+	case SSDFS_PEB1_SRC_PEB2_DST_CONTAINER:
+	case SSDFS_PEB2_SRC_PEB1_DST_CONTAINER:
+#ifdef CONFIG_SSDFS_DEBUG
+		BUG_ON(!pebi->pebc->src_peb || !pebi->pebc->dst_peb);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		err = -ERANGE;
+
+		if (pebi == pebi->pebc->src_peb) {
+			err = ssdfs_extract_src_peb_migration_id(pebi,
+								 &prev_id,
+								 &cur_id);
+		} else if (pebi == pebi->pebc->dst_peb) {
+			err = ssdfs_extract_dst_peb_migration_id(pebi,
+								 &prev_id,
+								 &cur_id);
+		}
+
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to extract peb_migration_id: "
+				  "seg %llu, peb %llu, err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id, err);
+			return err;
+		}
+		break;
+
+	case SSDFS_PEB1_SRC_EXT_PTR_DST_CONTAINER:
+	case SSDFS_PEB2_SRC_EXT_PTR_DST_CONTAINER:
+#ifdef CONFIG_SSDFS_DEBUG
+		BUG_ON(!pebi->pebc->src_peb || !pebi->pebc->dst_peb);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		err = ssdfs_extract_src_peb_migration_id(pebi,
+							 &prev_id,
+							 &cur_id);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to extract peb_migration_id: "
+				  "seg %llu, peb %llu, err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id, err);
+			return err;
+		}
+		break;
+
+	default:
+		SSDFS_WARN("invalid items_state %#x\n",
+			   items_state);
+#ifdef CONFIG_SSDFS_DEBUG
+		BUG();
+#endif /* CONFIG_SSDFS_DEBUG */
+		return -ERANGE;
+	}
+
+	hdr->peb_migration_id[SSDFS_PREV_MIGRATING_PEB] = prev_id;
+	hdr->peb_migration_id[SSDFS_CUR_MIGRATING_PEB] = cur_id;
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_store_log_header() - store log's header
+ * @pebi: pointer on PEB object
+ * @desc_array: pointer on descriptors array
+ * @array_size: count of items in array
+ * @write_offset: current write offset in log
+ *
+ * This function tries to store log's header in PEB's page cache.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_store_log_header(struct ssdfs_peb_info *pebi,
+				struct ssdfs_metadata_descriptor *desc_array,
+				size_t array_size,
+				u32 write_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_segment_header *hdr;
+	struct page *page;
+	size_t desc_size = sizeof(struct ssdfs_metadata_descriptor);
+	size_t array_bytes = desc_size * array_size;
+	u32 seg_flags;
+	u32 log_pages;
+	u16 seg_type;
+	u64 last_log_time;
+	u64 last_log_cno;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!desc_array);
+	BUG_ON(array_size != SSDFS_SEG_HDR_DESC_MAX);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(pebi->pebc->parent_si->seg_type > SSDFS_LAST_KNOWN_SEG_TYPE);
+	BUG_ON(pebi->current_log.seg_flags & ~SSDFS_SEG_HDR_FLAG_MASK);
+	BUG_ON(write_offset % fsi->pagesize);
+	BUG_ON((write_offset >> fsi->log_pagesize) > pebi->log_pages);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	seg_type = pebi->pebc->parent_si->seg_type;
+	log_pages = pebi->log_pages;
+	seg_flags = pebi->current_log.seg_flags;
+
+	page = ssdfs_page_array_get_page_locked(&pebi->cache,
+						pebi->current_log.start_page);
+	if (IS_ERR_OR_NULL(page)) {
+		SSDFS_ERR("fail to get cache page: index %u\n",
+			  pebi->current_log.start_page);
+		return -ERANGE;
+	}
+
+	hdr = kmap_local_page(page);
+
+	ssdfs_memcpy(hdr->desc_array, 0, array_bytes,
+		     desc_array, 0, array_bytes,
+		     array_bytes);
+
+	ssdfs_create_volume_header(fsi, &hdr->volume_hdr);
+
+	err = ssdfs_prepare_volume_header_for_commit(fsi, &hdr->volume_hdr);
+	if (unlikely(err))
+		goto finish_segment_header_preparation;
+
+	err = ssdfs_store_peb_migration_id(pebi, hdr);
+	if (unlikely(err))
+		goto finish_segment_header_preparation;
+
+	hdr->peb_create_time = cpu_to_le64(pebi->peb_create_time);
+
+	last_log_time = pebi->current_log.last_log_time;
+	last_log_cno = pebi->current_log.last_log_cno;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb %llu, "
+		  "peb_create_time %llx, last_log_time %llx\n",
+		  pebi->pebc->parent_si->seg_id,
+		  pebi->peb_id,
+		  pebi->peb_create_time,
+		  last_log_time);
+
+	BUG_ON(pebi->peb_create_time > last_log_time);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_prepare_segment_header_for_commit(fsi,
+						      log_pages,
+						      seg_type,
+						      seg_flags,
+						      last_log_time,
+						      last_log_cno,
+						      hdr);
+	if (unlikely(err))
+		goto finish_segment_header_preparation;
+
+finish_segment_header_preparation:
+	flush_dcache_page(page);
+	kunmap_local(hdr);
+	ssdfs_unlock_page(page);
+	ssdfs_put_page(page);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("page %p, count %d\n",
+		  page, page_ref_count(page));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (unlikely(err)) {
+		SSDFS_CRIT("fail to store segment header: "
+			   "seg %llu, peb %llu, current_log.start_page %u, "
+			   "err %d\n",
+			   pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			   pebi->current_log.start_page, err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_flush_current_log_dirty_pages() - flush log's dirty pages
+ * @pebi: pointer on PEB object
+ * @write_offset: current write offset in log
+ *
+ * This function tries to flush the current log's dirty pages.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_flush_current_log_dirty_pages(struct ssdfs_peb_info *pebi,
+					    u32 write_offset)
+{
+	struct ssdfs_fs_info *fsi;
+	loff_t peb_offset;
+	struct pagevec pvec;
+	u32 log_bytes, written_bytes;
+	u32 log_start_off;
+	unsigned flushed_pages;
+#ifdef CONFIG_SSDFS_CHECK_LOGICAL_BLOCK_EMPTYNESS
+	u32 pages_per_block;
+#endif /* CONFIG_SSDFS_CHECK_LOGICAL_BLOCK_EMPTYNESS */
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(write_offset == 0);
+	BUG_ON(write_offset % pebi->pebc->parent_si->fsi->pagesize);
+	BUG_ON(!pebi->pebc->parent_si->fsi->devops);
+	BUG_ON(!pebi->pebc->parent_si->fsi->devops->writepages);
+	BUG_ON(!is_ssdfs_peb_current_log_locked(pebi));
+
+	SSDFS_DBG("seg %llu, peb %llu, current_log.start_page %u, "
+		  "write_offset %u\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  pebi->current_log.start_page,
+		  write_offset);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	pagevec_init(&pvec);
+
+	peb_offset = (pebi->peb_id * fsi->pages_per_peb) << fsi->log_pagesize;
+
+	log_bytes = write_offset;
+	log_start_off = pebi->current_log.start_page << fsi->log_pagesize;
+	written_bytes = 0;
+	flushed_pages = 0;
+
+	while (written_bytes < log_bytes) {
+		pgoff_t index, end;
+		unsigned i;
+		u32 page_start_off, write_size;
+		loff_t iter_write_offset;
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.34.1

Part of the diff is not shown here. View the raw message.

#32 lore raw

RFC PATCH 31/76 ssdfs: process update request

In reply to Viacheslav Dubeyko

Flush thread can receive several types of update requests:
(1) update block or extent,
(2) prepare diff of b-tree node,
(3) prepare diff of user data logical block,
(4) commit log now,
(5) start migration now,
(6) process invalidated extent,
(7) migrate range, pre-allocated page, or fragment.

Update block or extent implies to store updated user data or metadata under Copy-On-Write (COW) policy in compressed or non-compressed state. Prepare diff means to use a delta-encoding technique to store updated user data or metadata under Copy-On-Write (COW) policy. Commit log now operation requests the execution of log commit right now. The start migration now operation is dedicated to mapping table case. This operation is requested before mapping table flush with the goal to check the necessity to finish/start migration. Because, start/finish migration requires the modification of mapping table. However, mapping table's flush operation needs to be finished without any modifications of mapping table itself. Process invalidates extent operation is executed after operation of file truncate or b-tree node deletion operation. This operation executes with the goal to finish migration operation and correct the state of PEB container. The migrate range operation can be received from global GC thread(s) as a recommendation for flush thread to execute migration of valid blocks from source (exhausted) erase block into destination (clean or "using") erase block if a pair of erase block is stuck in migration process.

CC Viacheslav Dubeyko <[email protected]>
CC Luka Perkov <[email protected]>
CC Bruno Banelli <[email protected]>
---
 fs/ssdfs/peb_flush_thread.c | 2426 +++++++++++++++++++++++++++++++++++
 1 file changed, 2426 insertions(+)
fs/ssdfs/peb_flush_thread.c +2426 -0
index 2de4bb806678..7e6a8a67e142 100644
@@ -109,10 +109,2436 @@ void ssdfs_flush_check_memory_leaks(void)
 #endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */
 }
 
+/*
+ * struct ssdfs_fragment_source - fragment source descriptor
+ * @page: memory page that contains uncompressed fragment
+ * @start_offset: offset into page to fragment's begin
+ * @data_bytes: size of fragment in bytes
+ * @sequence_id: fragment's sequence number
+ * @fragment_type: fragment type
+ * @fragment_flags: fragment's flags
+ */
+struct ssdfs_fragment_source {
+	struct page *page;
+	u32 start_offset;
+	size_t data_bytes;
+	u8 sequence_id;
+	u8 fragment_type;
+	u8 fragment_flags;
+};
+
+/*
+ * struct ssdfs_fragment_destination - fragment destination descriptor
+ * @area_offset: offset of area from log's beginning
+ * @write_offset: offset of @store pointer from area's begin
+ * @store: pointer for storing fragment
+ * @free_space: available space in bytes for fragment storing [in|out]
+ * @compr_size: size of fragment in bytes after compression [out]
+ * @desc: fragment descriptor [out]
+ */
+struct ssdfs_fragment_destination {
+	u32 area_offset;
+	u32 write_offset;
+	unsigned char *store;
+	size_t free_space;
+	size_t compr_size;
+	struct ssdfs_fragment_desc *desc;
+};
+
+/*
+ * struct ssdfs_byte_stream_descriptor - byte stream descriptor
+ * @pvec: pagevec that contains byte stream
+ * @start_offset: offset in bytes of byte stream in pagevec
+ * @data_bytes: size of uncompressed byte stream
+ * @write_offset: write offset of byte stream in area [out]
+ * @compr_bytes: size of byte stream after compression [out]
+ */
+struct ssdfs_byte_stream_descriptor {
+	struct pagevec *pvec;
+	u32 start_offset;
+	u32 data_bytes;
+	u32 write_offset;
+	u32 compr_bytes;
+};
+
+/*
+ * struct ssdfs_bmap_descriptor - block bitmap flush descriptor
+ * @pebi: pointer on PEB object
+ * @snapshot: block bitmap snapshot
+ * @peb_index: PEB index of bitmap owner
+ * @flags: fragment flags
+ * @type: fragment type
+ * @compression_type: type of compression
+ * @last_free_blk: last logical free block
+ * @metadata_blks: count of physical pages are used by metadata
+ * @invalid_blks: count of invalid blocks
+ * @frag_id: pointer on fragment counter
+ * @cur_page: pointer on current page value
+ * @write_offset: pointer on write offset value
+ */
+struct ssdfs_bmap_descriptor {
+	struct ssdfs_peb_info *pebi;
+	struct ssdfs_page_vector *snapshot;
+	u16 peb_index;
+	u8 flags;
+	u8 type;
+	u8 compression_type;
+	u32 last_free_blk;
+	u32 metadata_blks;
+	u32 invalid_blks;
+	size_t bytes_count;
+	u8 *frag_id;
+	pgoff_t *cur_page;
+	u32 *write_offset;
+};
+
+/*
+ * struct ssdfs_pagevec_descriptor - pagevec descriptor
+ * @pebi: pointer on PEB object
+ * @page_vec: pagevec with saving data
+ * @start_sequence_id: start sequence id
+ * @area_offset: offset of area
+ * @bytes_count: size in bytes of valid data in pagevec
+ * @desc_array: array of fragment descriptors
+ * @array_capacity: capacity of fragment descriptors' array
+ * @compression_type: type of compression
+ * @compr_size: whole size of all compressed fragments [out]
+ * @uncompr_size: whole size of all fragments in uncompressed state [out]
+ * @fragments_count: count of saved fragments
+ * @cur_page: pointer on current page value
+ * @write_offset: pointer on write offset value
+ */
+struct ssdfs_pagevec_descriptor {
+	struct ssdfs_peb_info *pebi;
+	struct ssdfs_page_vector *page_vec;
+	u16 start_sequence_id;
+	u32 area_offset;
+	size_t bytes_count;
+	struct ssdfs_fragment_desc *desc_array;
+	size_t array_capacity;
+	u8 compression_type;
+	u32 compr_size;
+	u32 uncompr_size;
+	u16 fragments_count;
+	pgoff_t *cur_page;
+	u32 *write_offset;
+};
+
 /******************************************************************************
  *                         FLUSH THREAD FUNCTIONALITY                         *
  ******************************************************************************/
 
+/*
+ * ssdfs_peb_read_from_offset() - read in buffer from offset
+ * @pebi: pointer on PEB object
+ * @off: offset in PEB
+ * @buf: pointer on buffer
+ * @buf_size: size of the buffer
+ *
+ * This function tries to read from volume into buffer.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+#ifdef CONFIG_SSDFS_UNDER_DEVELOPMENT_FUNC
+static
+int ssdfs_peb_read_from_offset(struct ssdfs_peb_info *pebi,
+			       struct ssdfs_phys_offset_descriptor *off,
+			       void *buf, size_t buf_size)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_metadata_descriptor desc_array[SSDFS_SEG_HDR_DESC_MAX];
+	u16 log_start_page;
+	u32 byte_offset;
+	u16 log_index;
+	int area_index;
+	u32 area_offset;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(!off || !buf);
+	BUG_ON(buf_size == 0);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	log_start_page = le16_to_cpu(off->blk_state.log_start_page);
+	byte_offset = le32_to_cpu(off->blk_state.byte_offset);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb %llu, "
+		  "log_start_page %u, log_area %#x, "
+		  "peb_migration_id %u, byte_offset %u, "
+		  "buf %p, buf_size %zu\n",
+		  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+		  log_start_page, off->blk_state.log_area,
+		  off->blk_state.peb_migration_id,
+		  byte_offset, buf, buf_size);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	log_index = log_start_page / pebi->log_pages;
+
+	if (log_index >= (fsi->pages_per_peb / pebi->log_pages)) {
+		SSDFS_ERR("invalid log index %u\n", log_index);
+		return -ERANGE;
+	}
+
+	area_index = SSDFS_AREA_TYPE2INDEX(off->blk_state.log_area);
+
+	if (area_index >= SSDFS_SEG_HDR_DESC_MAX) {
+		SSDFS_ERR("invalid area index %#x\n", area_index);
+		return -ERANGE;
+	}
+
+	err = ssdfs_peb_read_log_hdr_desc_array(pebi, log_index, desc_array,
+						SSDFS_SEG_HDR_DESC_MAX);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read log's header desc array: "
+			  "seg %llu, peb %llu, log_index %u, err %d\n",
+			  pebi->pebc->parent_si->seg_id, pebi->peb_id,
+			  log_index, err);
+		return err;
+	}
+
+	area_offset = le32_to_cpu(desc_array[area_index].offset);
+
+	err = ssdfs_unaligned_read_buffer(fsi, pebi->peb_id,
+					  area_offset + byte_offset,
+					  buf, buf_size);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to read buffer: "
+			  "peb %llu, area_offset %u, byte_offset %u, "
+			  "buf_size %zu, err %d\n",
+			  pebi->peb_id, area_offset, byte_offset,
+			  buf_size, err);
+		return err;
+	}
+
+	return 0;
+}
+#endif /* CONFIG_SSDFS_UNDER_DEVELOPMENT_FUNC */
+
+static inline
+bool does_user_data_block_contain_diff(struct ssdfs_peb_info *pebi,
+					struct ssdfs_segment_request *req)
+{
+	struct ssdfs_fs_info *fsi;
+	u32 mem_pages_per_block;
+	int page_index;
+	struct page *page;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !req);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (!is_ssdfs_peb_containing_user_data(pebi->pebc))
+		return false;
+
+	fsi = pebi->pebc->parent_si->fsi;
+	mem_pages_per_block = fsi->pagesize / PAGE_SIZE;
+	page_index = req->result.processed_blks * mem_pages_per_block;
+	page = req->result.pvec.pages[page_index];
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	return is_diff_page(page);
+}
+
+/*
+ * __ssdfs_peb_update_block() - update data block
+ * @pebi: pointer on PEB object
+ * @req: I/O request
+ *
+ * This function tries to update data block in PEB.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EAGAIN     - try again to update data block.
+ * %-ENOENT     - need migrate base state before storing diff.
+ */
+static
+int __ssdfs_peb_update_block(struct ssdfs_peb_info *pebi,
+			     struct ssdfs_segment_request *req)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_segment_info *si;
+	struct ssdfs_blk2off_table *table;
+	struct ssdfs_segment_blk_bmap *seg_blkbmap;
+	struct ssdfs_peb_blk_bmap *peb_blkbmap;
+	struct ssdfs_phys_offset_descriptor *blk_desc_off;
+	struct ssdfs_peb_phys_offset data_off = {0};
+	struct ssdfs_peb_phys_offset desc_off = {0};
+	u16 blk;
+	u64 logical_offset;
+	struct ssdfs_block_bmap_range range;
+	int range_state;
+	u32 written_bytes;
+	u16 peb_index;
+	int migration_state = SSDFS_LBLOCK_UNKNOWN_STATE;
+	struct ssdfs_offset_position pos = {0};
+	u8 migration_id1;
+	int migration_id2;
+#ifdef CONFIG_SSDFS_DEBUG
+	int i;
+#endif /* CONFIG_SSDFS_DEBUG */
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !req);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(req->place.start.seg_id != pebi->pebc->parent_si->seg_id);
+	BUG_ON(req->place.start.blk_index >=
+		pebi->pebc->parent_si->fsi->pages_per_seg);
+	switch (req->private.class) {
+	case SSDFS_PEB_UPDATE_REQ:
+	case SSDFS_PEB_PRE_ALLOC_UPDATE_REQ:
+	case SSDFS_PEB_DIFF_ON_WRITE_REQ:
+	case SSDFS_PEB_COLLECT_GARBAGE_REQ:
+		/* expected case */
+		break;
+	default:
+		BUG();
+		break;
+	}
+	BUG_ON(req->private.type >= SSDFS_REQ_TYPE_MAX);
+	BUG_ON(atomic_read(&req->private.refs_count) == 0);
+
+	SSDFS_DBG("ino %llu, seg %llu, peb %llu, logical_offset %llu, "
+		  "processed_blks %d, logical_block %u, data_bytes %u, "
+		  "cno %llu, parent_snapshot %llu, cmd %#x, type %#x\n",
+		  req->extent.ino, req->place.start.seg_id, pebi->peb_id,
+		  req->extent.logical_offset, req->result.processed_blks,
+		  req->place.start.blk_index,
+		  req->extent.data_bytes, req->extent.cno,
+		  req->extent.parent_snapshot,
+		  req->private.cmd, req->private.type);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	si = pebi->pebc->parent_si;
+	fsi = si->fsi;
+	table = pebi->pebc->parent_si->blk2off_table;
+	seg_blkbmap = &pebi->pebc->parent_si->blk_bmap;
+	peb_blkbmap = &seg_blkbmap->peb[pebi->pebc->peb_index];
+
+#ifdef CONFIG_SSDFS_DEBUG
+	if (req->extent.logical_offset >= U64_MAX) {
+		SSDFS_ERR("seg %llu, peb %llu, logical_block %u, "
+			  "logical_offset %llu, "
+			  "processed_blks %d\n",
+			  req->place.start.seg_id, pebi->peb_id,
+			  req->place.start.blk_index,
+			  req->extent.logical_offset,
+			  req->result.processed_blks);
+		BUG();
+	}
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	blk = req->place.start.blk_index + req->result.processed_blks;
+	logical_offset = req->extent.logical_offset +
+			    ((u64)req->result.processed_blks * fsi->pagesize);
+	logical_offset = div64_u64(logical_offset, fsi->pagesize);
+
+	if (req->private.class == SSDFS_PEB_DIFF_ON_WRITE_REQ) {
+		u32 pvec_size = pagevec_count(&req->result.pvec);
+		u32 cur_index = req->result.processed_blks;
+
+		if (cur_index >= pvec_size) {
+			SSDFS_ERR("processed_blks %u > pagevec_size %u\n",
+				  cur_index, pvec_size);
+			return -ERANGE;
+		}
+
+		if (req->result.pvec.pages[cur_index] == NULL) {
+			req->result.processed_blks++;
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("block %u hasn't diff\n",
+				  blk);
+#endif /* CONFIG_SSDFS_DEBUG */
+			goto finish_update_block;
+		}
+	}
+
+	blk_desc_off = ssdfs_blk2off_table_convert(table, blk,
+						   &peb_index,
+						   &migration_state,
+						   &pos);
+	if (IS_ERR(blk_desc_off) && PTR_ERR(blk_desc_off) == -EAGAIN) {
+		struct completion *end = &table->full_init_end;
+
+		err = SSDFS_WAIT_COMPLETION(end);
+		if (unlikely(err)) {
+			SSDFS_ERR("blk2off init failed: "
+				  "err %d\n", err);
+			return err;
+		}
+
+		blk_desc_off = ssdfs_blk2off_table_convert(table, blk,
+							   &peb_index,
+							   &migration_state,
+							   &pos);
+	}
+
+	if (IS_ERR_OR_NULL(blk_desc_off)) {
+		err = (blk_desc_off == NULL ? -ERANGE : PTR_ERR(blk_desc_off));
+		SSDFS_ERR("fail to convert: "
+			  "logical_blk %u, err %d\n",
+			  blk, err);
+		return err;
+	}
+
+	if (req->private.class == SSDFS_PEB_DIFF_ON_WRITE_REQ) {
+		migration_id1 =
+			SSDFS_GET_BLK_DESC_MIGRATION_ID(&pos.blk_desc.buf);
+		migration_id2 = ssdfs_get_peb_migration_id_checked(pebi);
+
+		if (migration_id1 < U8_MAX && migration_id1 != migration_id2) {
+			/*
+			 * Base state and diff in different PEBs
+			 */
+
+			range.start = blk;
+			range.len = 1;
+
+			ssdfs_requests_queue_add_head(&pebi->pebc->update_rq,
+							req);
+
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("range: (start %u, len %u)\n",
+				  range.start, range.len);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+			if (is_ssdfs_peb_containing_user_data(pebi->pebc)) {
+				ssdfs_account_updated_user_data_pages(fsi,
+								    range.len);
+			}
+
+			err = ssdfs_peb_migrate_valid_blocks_range(si,
+								   pebi->pebc,
+								   peb_blkbmap,
+								   &range);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to migrate valid blocks: "
+					  "range (start %u, len %u), err %d\n",
+					  range.start, range.len, err);
+				return err;
+			}
+
+			return -ENOENT;
+		}
+	}
+
+	down_write(&table->translation_lock);
+
+	migration_state = ssdfs_blk2off_table_get_block_migration(table, blk,
+								  peb_index);
+	switch (migration_state) {
+	case SSDFS_LBLOCK_UNKNOWN_STATE:
+		err = -ENOENT;
+		/* logical block is not migrating */
+		break;
+
+	case SSDFS_LBLOCK_UNDER_MIGRATION:
+		switch (req->private.cmd) {
+		case SSDFS_MIGRATE_RANGE:
+		case SSDFS_MIGRATE_FRAGMENT:
+			err = 0;
+			/* continue logic */
+			break;
+
+		default:
+			err = ssdfs_blk2off_table_update_block_state(table,
+								     req);
+			if (unlikely(err)) {
+				SSDFS_ERR("fail to update block's state: "
+					  "seg %llu, logical_block %u, "
+					  "peb %llu, err %d\n",
+					  req->place.start.seg_id, blk,
+					  pebi->peb_id, err);
+			} else
+				err = -EEXIST;
+			break;
+		}
+		break;
+
+	case SSDFS_LBLOCK_UNDER_COMMIT:
+		err = -EAGAIN;
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("try again to update block: "
+			  "seg %llu, logical_block %u, peb %llu\n",
+			  req->place.start.seg_id, blk, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+		break;
+
+	default:
+		err = -ERANGE;
+		SSDFS_ERR("unexpected migration state: "
+			  "seg %llu, logical_block %u, "
+			  "peb %llu, migration_state %#x\n",
+			  req->place.start.seg_id, blk,
+			  pebi->peb_id, migration_state);
+		break;
+	}
+
+	up_write(&table->translation_lock);
+
+	if (err == -ENOENT) {
+		/* logical block is not migrating */
+		err = 0;
+	} else if (err == -EEXIST) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("migrating block has been updated in buffer: "
+			  "seg %llu, peb %llu, logical_block %u\n",
+			  req->place.start.seg_id, pebi->peb_id,
+			  blk);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return 0;
+	} else if (unlikely(err))
+		return err;
+
+	err = ssdfs_peb_reserve_block_descriptor(pebi, req);
+	if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("try again to add block: "
+			  "seg %llu, logical_block %u, peb %llu\n",
+			  req->place.start.seg_id, blk, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to reserve block descriptor: "
+			  "seg %llu, logical_block %u, peb %llu, err %d\n",
+			  req->place.start.seg_id, blk, pebi->peb_id, err);
+		return err;
+	}
+
+	err = ssdfs_peb_add_block_into_data_area(pebi, req,
+						 blk_desc_off, &pos,
+						 &data_off,
+						 &written_bytes);
+	if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("try again to add block: "
+			  "seg %llu, logical_block %u, peb %llu\n",
+			  req->place.start.seg_id, blk, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return err;
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to add block: "
+			  "seg %llu, logical_block %u, peb %llu, err %d\n",
+			  req->place.start.seg_id, blk, pebi->peb_id, err);
+		return err;
+	}
+
+	range.start = le16_to_cpu(blk_desc_off->page_desc.peb_page);
+	range.len = (written_bytes + fsi->pagesize - 1) >> fsi->log_pagesize;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("written_bytes %u, range (start %u, len %u)\n",
+		  written_bytes, range.start, range.len);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (req->private.class == SSDFS_PEB_DIFF_ON_WRITE_REQ)
+		range_state = SSDFS_BLK_VALID;
+	else if (is_ssdfs_block_full(fsi->pagesize, written_bytes))
+		range_state = SSDFS_BLK_VALID;
+	else
+		range_state = SSDFS_BLK_PRE_ALLOCATED;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("logical_blk %u, peb_page %u\n",
+		  blk, range.start);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = ssdfs_segment_blk_bmap_update_range(seg_blkbmap, pebi->pebc,
+				blk_desc_off->blk_state.peb_migration_id,
+				range_state, &range);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to update range: "
+			  "seg %llu, peb %llu, "
+			  "range (start %u, len %u), err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id, range.start, range.len,
+			  err);
+		return err;
+	}
+
+	data_off.peb_page = (u16)range.start;
+
+	if (req->private.class != SSDFS_PEB_DIFF_ON_WRITE_REQ &&
+	    !does_user_data_block_contain_diff(pebi, req))
+		SSDFS_BLK_DESC_INIT(&pos.blk_desc.buf);
+	else {
+#ifdef CONFIG_SSDFS_DEBUG
+		migration_id1 =
+			SSDFS_GET_BLK_DESC_MIGRATION_ID(&pos.blk_desc.buf);
+		if (migration_id1 >= U8_MAX) {
+			/*
+			 * continue logic
+			 */
+		} else {
+			migration_id2 =
+				ssdfs_get_peb_migration_id_checked(pebi);
+
+			if (migration_id1 != migration_id2) {
+				struct ssdfs_block_descriptor *blk_desc;
+
+				SSDFS_WARN("invalid request: "
+					   "migration_id1 %u, "
+					   "migration_id2 %d\n",
+					   migration_id1, migration_id2);
+
+				blk_desc = &pos.blk_desc.buf;
+
+				SSDFS_ERR("seg_id %llu, peb_id %llu, "
+					  "ino %llu, logical_offset %u, "
+					  "peb_index %u, peb_page %u\n",
+					  req->place.start.seg_id,
+					  pebi->peb_id,
+					  le64_to_cpu(blk_desc->ino),
+					  le32_to_cpu(blk_desc->logical_offset),
+					  le16_to_cpu(blk_desc->peb_index),
+					  le16_to_cpu(blk_desc->peb_page));
+
+				for (i = 0; i < SSDFS_BLK_STATE_OFF_MAX; i++) {
+					struct ssdfs_blk_state_offset *state;
+
+					state = &blk_desc->state[i];
+
+					SSDFS_ERR("BLK STATE OFFSET %d: "
+						  "log_start_page %u, "
+						  "log_area %#x, "
+						  "byte_offset %u, "
+						  "peb_migration_id %u\n",
+					  i,
+					  le16_to_cpu(state->log_start_page),
+					  state->log_area,
+					  le32_to_cpu(state->byte_offset),
+					  state->peb_migration_id);
+				}
+
+				BUG();
+			}
+		}
+#endif /* CONFIG_SSDFS_DEBUG */
+	}
+
+	err = ssdfs_peb_store_block_descriptor(pebi, req,
+						&pos.blk_desc.buf,
+						&data_off, &desc_off);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to store block descriptor: "
+			  "seg %llu, logical_block %u, peb %llu, err %d\n",
+			  req->place.start.seg_id, blk,
+			  pebi->peb_id, err);
+		return err;
+	}
+
+	err = ssdfs_peb_store_block_descriptor_offset(pebi,
+							(u32)logical_offset,
+							blk,
+							&pos.blk_desc.buf,
+							&desc_off);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to store block descriptor offset: "
+			  "err %d\n",
+			  err);
+		return err;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("ino %llu, seg %llu, peb %llu, logical_block %u, "
+		  "migration_state %#x\n",
+		  req->extent.ino, req->place.start.seg_id, pebi->peb_id,
+		  req->place.start.blk_index, migration_state);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (is_ssdfs_logical_block_migrating(migration_state)) {
+		err = ssdfs_blk2off_table_set_block_commit(table, blk,
+							   peb_index);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to set block commit: "
+				  "logical_blk %u, peb_index %u, err %d\n",
+				  blk, peb_index, err);
+			return err;
+		}
+	}
+
+	req->result.processed_blks += range.len;
+
+finish_update_block:
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("finish update block: "
+		  "ino %llu, seg %llu, peb %llu, logical_block %u, "
+		  "req->result.processed_blks %d\n",
+		  req->extent.ino, req->place.start.seg_id, pebi->peb_id,
+		  req->place.start.blk_index,
+		  req->result.processed_blks);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	return 0;
+}
+
+/*
+ * ssdfs_check_zone_move_request() - check request
+ * @req: segment request
+ *
+ * This method tries to check the state of request.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_check_zone_move_request(struct ssdfs_segment_request *req)
+{
+	wait_queue_head_t *wq = NULL;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!req);
+
+	SSDFS_DBG("req %p\n", req);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+check_req_state:
+	switch (atomic_read(&req->result.state)) {
+	case SSDFS_REQ_CREATED:
+	case SSDFS_REQ_STARTED:
+		wq = &req->private.wait_queue;
+
+		err = wait_event_killable_timeout(*wq,
+					has_request_been_executed(req),
+					SSDFS_DEFAULT_TIMEOUT);
+		if (err < 0)
+			WARN_ON(err < 0);
+		else
+			err = 0;
+
+		goto check_req_state;
+		break;
+
+	case SSDFS_REQ_FINISHED:
+		/* do nothing */
+		break;
+
+	case SSDFS_REQ_FAILED:
+		err = req->result.err;
+
+		if (!err) {
+			SSDFS_ERR("error code is absent: "
+				  "req %p, err %d\n",
+				  req, err);
+			err = -ERANGE;
+		}
+
+		SSDFS_ERR("flush request is failed: "
+			  "err %d\n", err);
+		return err;
+
+	default:
+		SSDFS_ERR("invalid result's state %#x\n",
+		    atomic_read(&req->result.state));
+		return -ERANGE;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_extract_left_extent() - extract left extent
+ * @req: I/O request
+ * @migration: recommended migration extent
+ * @left_fragment: difference between recommended and requested extents [out]
+ *
+ * This function tries to extract difference between recommended
+ * and requested extents from the left.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static inline
+int ssdfs_extract_left_extent(struct ssdfs_segment_request *req,
+			      struct ssdfs_zone_fragment *migration,
+			      struct ssdfs_zone_fragment *left_fragment)
+{
+	u64 seg_id;
+	u32 start_blk;
+	u32 len;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!req || !migration || !left_fragment);
+
+	SSDFS_DBG("ino %llu, logical_offset %llu, "
+		  "data_bytes %u, cno %llu, parent_snapshot %llu, "
+		  "seg %llu, logical_block %u, len %u, "
+		  "cmd %#x, type %#x, processed_blks %d\n",
+		  req->extent.ino, req->extent.logical_offset,
+		  req->extent.data_bytes, req->extent.cno,
+		  req->extent.parent_snapshot,
+		  req->place.start.seg_id,
+		  req->place.start.blk_index,
+		  req->place.len,
+		  req->private.cmd, req->private.type,
+		  req->result.processed_blks);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	seg_id = le64_to_cpu(migration->extent.seg_id);
+	start_blk = le32_to_cpu(migration->extent.logical_blk);
+	len = le32_to_cpu(migration->extent.len);
+
+	if (req->extent.ino != migration->ino) {
+		SSDFS_ERR("invalid input: "
+			  "ino1 %llu != ino2 %llu\n",
+			  req->extent.ino, migration->ino);
+		return -ERANGE;
+	}
+
+	if (req->place.start.seg_id != seg_id) {
+		SSDFS_ERR("invalid input: "
+			  "seg_id1 %llu != seg_id2 %llu\n",
+			  req->place.start.seg_id, seg_id);
+		return -ERANGE;
+	}
+
+	if (req->place.start.blk_index < start_blk) {
+		SSDFS_ERR("invalid input: "
+			  "request (seg_id %llu, logical_blk %u, len %u), "
+			  "migration (seg_id %llu, logical_blk %u, len %u)\n",
+			  req->place.start.seg_id,
+			  req->place.start.blk_index,
+			  req->place.len,
+			  seg_id, start_blk, len);
+		return -ERANGE;
+	}
+
+	if (req->place.start.blk_index == start_blk) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("no extent from the left: "
+			  "request (seg_id %llu, logical_blk %u, len %u), "
+			  "migration (seg_id %llu, logical_blk %u, len %u)\n",
+			  req->place.start.seg_id,
+			  req->place.start.blk_index,
+			  req->place.len,
+			  seg_id, start_blk, len);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return -ENODATA;
+	}
+
+	left_fragment->ino = migration->ino;
+	left_fragment->logical_blk_offset = migration->logical_blk_offset;
+	left_fragment->extent.seg_id = migration->extent.seg_id;
+	left_fragment->extent.logical_blk = migration->extent.logical_blk;
+	left_fragment->extent.len =
+			cpu_to_le32(req->place.start.blk_index - start_blk);
+
+	return 0;
+}
+
+/*
+ * ssdfs_extract_right_extent() - extract right extent
+ * @req: I/O request
+ * @migration: recommended migration extent
+ * @right_fragment: difference between recommended and requested extents [out]
+ *
+ * This function tries to extract difference between recommended
+ * and requested extents from the right.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static inline
+int ssdfs_extract_right_extent(struct ssdfs_segment_request *req,
+			       struct ssdfs_zone_fragment *migration,
+			       struct ssdfs_zone_fragment *right_fragment)
+{
+	u64 seg_id;
+	u32 start_blk;
+	u32 len;
+	u32 upper_bound1, upper_bound2;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!req || !migration || !right_fragment);
+
+	SSDFS_DBG("ino %llu, logical_offset %llu, "
+		  "data_bytes %u, cno %llu, parent_snapshot %llu, "
+		  "seg %llu, logical_block %u, len %u, "
+		  "cmd %#x, type %#x, processed_blks %d\n",
+		  req->extent.ino, req->extent.logical_offset,
+		  req->extent.data_bytes, req->extent.cno,
+		  req->extent.parent_snapshot,
+		  req->place.start.seg_id,
+		  req->place.start.blk_index,
+		  req->place.len,
+		  req->private.cmd, req->private.type,
+		  req->result.processed_blks);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	seg_id = le64_to_cpu(migration->extent.seg_id);
+	start_blk = le32_to_cpu(migration->extent.logical_blk);
+	len = le32_to_cpu(migration->extent.len);
+
+	if (req->extent.ino != migration->ino) {
+		SSDFS_ERR("invalid input: "
+			  "ino1 %llu != ino2 %llu\n",
+			  req->extent.ino, migration->ino);
+		return -ERANGE;
+	}
+
+	if (req->place.start.seg_id != seg_id) {
+		SSDFS_ERR("invalid input: "
+			  "seg_id1 %llu != seg_id2 %llu\n",
+			  req->place.start.seg_id, seg_id);
+		return -ERANGE;
+	}
+
+	upper_bound1 = req->place.start.blk_index + req->place.len;
+	upper_bound2 = start_blk + len;
+
+	if (upper_bound1 > upper_bound2) {
+		SSDFS_ERR("invalid input: "
+			  "request (seg_id %llu, logical_blk %u, len %u), "
+			  "migration (seg_id %llu, logical_blk %u, len %u)\n",
+			  req->place.start.seg_id,
+			  req->place.start.blk_index,
+			  req->place.len,
+			  seg_id, start_blk, len);
+		return -ERANGE;
+	}
+
+	if (upper_bound1 == upper_bound2) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("no extent from the right: "
+			  "request (seg_id %llu, logical_blk %u, len %u), "
+			  "migration (seg_id %llu, logical_blk %u, len %u)\n",
+			  req->place.start.seg_id,
+			  req->place.start.blk_index,
+			  req->place.len,
+			  seg_id, start_blk, len);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return -ENODATA;
+	}
+
+	right_fragment->ino = migration->ino;
+	right_fragment->logical_blk_offset =
+			migration->logical_blk_offset + upper_bound1;
+	right_fragment->extent.seg_id = migration->extent.seg_id;
+	right_fragment->extent.logical_blk = cpu_to_le32(upper_bound1);
+	right_fragment->extent.len = cpu_to_le32(upper_bound2 - upper_bound1);
+
+	return 0;
+}
+
+/*
+ * __ssdfs_zone_issue_move_request() - issue move request
+ * @pebi: pointer on PEB object
+ * @fragment: zone fragment
+ * @req_type: request type
+ * @req: I/O request
+ *
+ * This function tries to issue move request.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int __ssdfs_zone_issue_move_request(struct ssdfs_peb_info *pebi,
+				    struct ssdfs_zone_fragment *fragment,
+				    int req_type,
+				    struct ssdfs_segment_request *req)
+{
+	struct ssdfs_fs_info *fsi;
+	struct inode *inode;
+	struct ssdfs_inode_info *ii;
+	struct ssdfs_extents_btree_info *etree;
+	struct ssdfs_btree_search *search;
+	struct page *page;
+	struct ssdfs_blk2off_range new_extent;
+	struct ssdfs_raw_extent old_raw_extent;
+	struct ssdfs_raw_extent new_raw_extent;
+	u64 seg_id;
+	u32 logical_blk;
+	u32 len;
+	u64 logical_offset;
+	int i;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !fragment);
+
+	SSDFS_DBG("peb %llu, ino %llu, logical_blk_offset %llu, "
+		  "extent (seg_id %llu, logical_blk %u, len %u)\n",
+		  pebi->peb_id,
+		  fragment->ino,
+		  fragment->logical_blk_offset,
+		  le64_to_cpu(fragment->extent.seg_id),
+		  le32_to_cpu(fragment->extent.logical_blk),
+		  le32_to_cpu(fragment->extent.len));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	seg_id = le64_to_cpu(fragment->extent.seg_id);
+	logical_blk = le32_to_cpu(fragment->extent.logical_blk);
+	len = le32_to_cpu(fragment->extent.len);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(seg_id != pebi->pebc->parent_si->seg_id);
+	BUG_ON(len > PAGEVEC_SIZE);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	ssdfs_request_init(req);
+	ssdfs_get_request(req);
+
+	req->private.flags |= SSDFS_REQ_DONT_FREE_PAGES;
+
+	logical_offset = fragment->logical_blk_offset << fsi->log_pagesize;
+	ssdfs_request_prepare_logical_extent(fragment->ino, logical_offset,
+					     len, 0, 0, req);
+
+	req->place.start.seg_id = seg_id;
+	req->place.start.blk_index = logical_blk;
+	req->place.len = 0;
+
+	req->result.processed_blks = 0;
+
+	for (i = 0; i < len; i++) {
+		logical_blk += i;
+		req->place.len++;
+
+		err = ssdfs_peb_copy_page(pebi->pebc, logical_blk, req);
+		if (err == -EAGAIN) {
+			req->place.len = req->result.processed_blks;
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to copy the whole range: "
+				  "seg %llu, logical_blk %u, len %u\n",
+				  pebi->pebc->parent_si->seg_id,
+				  logical_blk, req->place.len);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to copy page: "
+				  "seg %llu, logical_blk %u, err %d\n",
+				  pebi->pebc->parent_si->seg_id,
+				  logical_blk, err);
+			return err;
+		}
+	}
+
+	for (i = 0; i < req->result.processed_blks; i++)
+		ssdfs_peb_mark_request_block_uptodate(pebi->pebc, req, i);
+
+	for (i = 0; i < pagevec_count(&req->result.pvec); i++) {
+		page = req->result.pvec.pages[i];
+
+#ifdef CONFIG_SSDFS_DEBUG
+		BUG_ON(!page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		set_page_writeback(page);
+	}
+
+	req->result.err = 0;
+	req->result.processed_blks = 0;
+	atomic_set(&req->result.state, SSDFS_UNKNOWN_REQ_RESULT);
+
+	err = ssdfs_segment_migrate_zone_extent_async(fsi,
+						      req_type,
+						      req,
+						      &seg_id,
+						      &new_extent);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to migrate zone extent: "
+			  "peb %llu, ino %llu, logical_blk_offset %llu, "
+			  "extent (seg_id %llu, logical_blk %u, len %u)\n",
+			  pebi->peb_id,
+			  fragment->ino,
+			  fragment->logical_blk_offset,
+			  le64_to_cpu(fragment->extent.seg_id),
+			  le32_to_cpu(fragment->extent.logical_blk),
+			  le32_to_cpu(fragment->extent.len));
+		goto fail_issue_move_request;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(seg_id >= U64_MAX);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	old_raw_extent.seg_id = fragment->extent.seg_id;
+	old_raw_extent.logical_blk = fragment->extent.logical_blk;
+	old_raw_extent.len = fragment->extent.len;
+
+	new_raw_extent.seg_id = cpu_to_le64(seg_id);
+	new_raw_extent.logical_blk = cpu_to_le32(new_extent.start_lblk);
+	new_raw_extent.len = cpu_to_le32(new_extent.len);
+
+	page = req->result.pvec.pages[0];
+	inode = page->mapping->host;
+	ii = SSDFS_I(inode);
+
+	etree = SSDFS_EXTREE(ii);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!etree);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	search = ssdfs_btree_search_alloc();
+	if (!search) {
+		err = -ENOMEM;
+		SSDFS_ERR("fail to allocate btree search object\n");
+		goto fail_issue_move_request;
+	}
+
+	ssdfs_btree_search_init(search);
+	err = ssdfs_extents_tree_move_extent(etree,
+					     fragment->logical_blk_offset,
+					     &old_raw_extent,
+					     &new_raw_extent,
+					     search);
+	ssdfs_btree_search_free(search);
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to move extent: "
+			  "old_extent (seg_id %llu, logical_blk %u, len %u), "
+			  "new_extent (seg_id %llu, logical_blk %u, len %u), "
+			  "err %d\n",
+			  le64_to_cpu(old_raw_extent.seg_id),
+			  le32_to_cpu(old_raw_extent.logical_blk),
+			  le32_to_cpu(old_raw_extent.len),
+			  le64_to_cpu(new_raw_extent.seg_id),
+			  le32_to_cpu(new_raw_extent.logical_blk),
+			  le32_to_cpu(new_raw_extent.len),
+			  err);
+		goto fail_issue_move_request;
+	}
+
+	return 0;
+
+fail_issue_move_request:
+	ssdfs_request_unlock_and_remove_pages(req);
+	ssdfs_put_request(req);
+
+	return err;
+}
+
+/*
+ * ssdfs_zone_issue_async_move_request() - issue async move request
+ * @pebi: pointer on PEB object
+ * @fragment: zone fragment
+ *
+ * This function tries to issue async move request.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_zone_issue_async_move_request(struct ssdfs_peb_info *pebi,
+					struct ssdfs_zone_fragment *fragment)
+{
+	struct ssdfs_segment_request *req;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !fragment);
+
+	SSDFS_DBG("peb %llu, ino %llu, logical_blk_offset %llu, "
+		  "extent (seg_id %llu, logical_blk %u, len %u)\n",
+		  pebi->peb_id,
+		  fragment->ino,
+		  fragment->logical_blk_offset,
+		  le64_to_cpu(fragment->extent.seg_id),
+		  le32_to_cpu(fragment->extent.logical_blk),
+		  le32_to_cpu(fragment->extent.len));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	req = ssdfs_request_alloc();
+	if (IS_ERR_OR_NULL(req)) {
+		err = (req == NULL ? -ENOMEM : PTR_ERR(req));
+		SSDFS_ERR("fail to allocate segment request: err %d\n",
+			  err);
+		return err;
+	}
+
+	err = __ssdfs_zone_issue_move_request(pebi, fragment,
+					      SSDFS_REQ_ASYNC,
+					      req);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to issue move request: "
+			  "peb %llu, ino %llu, logical_blk_offset %llu, "
+			  "extent (seg_id %llu, logical_blk %u, len %u)\n",
+			  pebi->peb_id,
+			  fragment->ino,
+			  fragment->logical_blk_offset,
+			  le64_to_cpu(fragment->extent.seg_id),
+			  le32_to_cpu(fragment->extent.logical_blk),
+			  le32_to_cpu(fragment->extent.len));
+		goto fail_issue_move_request;
+	}
+
+	return 0;
+
+fail_issue_move_request:
+	ssdfs_request_free(req);
+	return err;
+}
+
+/*
+ * ssdfs_zone_issue_move_request() - issue move request
+ * @pebi: pointer on PEB object
+ * @fragment: zone fragment
+ * @req: I/O request
+ *
+ * This function tries to issue move request.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_zone_issue_move_request(struct ssdfs_peb_info *pebi,
+				  struct ssdfs_zone_fragment *fragment,
+				  struct ssdfs_segment_request *req)
+{
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !fragment);
+
+	SSDFS_DBG("peb %llu, ino %llu, logical_blk_offset %llu, "
+		  "extent (seg_id %llu, logical_blk %u, len %u)\n",
+		  pebi->peb_id,
+		  fragment->ino,
+		  fragment->logical_blk_offset,
+		  le64_to_cpu(fragment->extent.seg_id),
+		  le32_to_cpu(fragment->extent.logical_blk),
+		  le32_to_cpu(fragment->extent.len));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	err = __ssdfs_zone_issue_move_request(pebi, fragment,
+					      SSDFS_REQ_ASYNC_NO_FREE,
+					      req);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to issue move request: "
+			  "peb %llu, ino %llu, logical_blk_offset %llu, "
+			  "extent (seg_id %llu, logical_blk %u, len %u)\n",
+			  pebi->peb_id,
+			  fragment->ino,
+			  fragment->logical_blk_offset,
+			  le64_to_cpu(fragment->extent.seg_id),
+			  le32_to_cpu(fragment->extent.logical_blk),
+			  le32_to_cpu(fragment->extent.len));
+		goto fail_issue_move_request;
+	}
+
+fail_issue_move_request:
+	return err;
+}
+
+/*
+ * ssdfs_zone_prepare_migration_request() - stimulate migration
+ * @pebi: pointer on PEB object
+ * @fragment: zone fragment
+ * @req: I/O request
+ *
+ * This function tries to prepare migration stimulation request
+ * during moving updated data from exhausted zone into current zone
+ * for updates.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_zone_prepare_migration_request(struct ssdfs_peb_info *pebi,
+					 struct ssdfs_zone_fragment *fragment,
+					 struct ssdfs_segment_request *req)
+{
+	struct ssdfs_zone_fragment sub_fragment;
+	u64 seg_id;
+	u32 logical_blk;
+	u32 len;
+	u32 offset = 0;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !fragment || !req);
+
+	SSDFS_DBG("peb %llu, logical_blk_offset %llu, "
+		  "extent (seg_id %llu, logical_blk %u, len %u)\n",
+		  pebi->peb_id,
+		  fragment->logical_blk_offset,
+		  le64_to_cpu(fragment->extent.seg_id),
+		  le32_to_cpu(fragment->extent.logical_blk),
+		  le32_to_cpu(fragment->extent.len));
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	seg_id = le64_to_cpu(fragment->extent.seg_id);
+	logical_blk = le32_to_cpu(fragment->extent.logical_blk);
+	len = le32_to_cpu(fragment->extent.len);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(seg_id != pebi->pebc->parent_si->seg_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	while (len > PAGEVEC_SIZE) {
+		sub_fragment.ino = fragment->ino;
+		sub_fragment.logical_blk_offset =
+				fragment->logical_blk_offset + offset;
+		sub_fragment.extent.seg_id = fragment->extent.seg_id;
+		sub_fragment.extent.logical_blk =
+					cpu_to_le32(logical_blk + offset);
+		sub_fragment.extent.len = cpu_to_le32(PAGEVEC_SIZE);
+
+		err = ssdfs_zone_issue_async_move_request(pebi, &sub_fragment);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to issue zone async move request: "
+				  "peb %llu, logical_blk_offset %llu, "
+				  "sub_extent (seg_id %llu, "
+				  "logical_blk %u, len %u), err %d\n",
+				  pebi->peb_id,
+				  sub_fragment.logical_blk_offset,
+				  le64_to_cpu(sub_fragment.extent.seg_id),
+				  le32_to_cpu(sub_fragment.extent.logical_blk),
+				  le32_to_cpu(sub_fragment.extent.len),
+				  err);
+			return err;
+		}
+
+		offset += PAGEVEC_SIZE;
+		len -= PAGEVEC_SIZE;
+	}
+
+	sub_fragment.ino = fragment->ino;
+	sub_fragment.logical_blk_offset =
+			fragment->logical_blk_offset + offset;
+	sub_fragment.extent.seg_id = fragment->extent.seg_id;
+	sub_fragment.extent.logical_blk = cpu_to_le32(logical_blk + offset);
+	sub_fragment.extent.len = cpu_to_le32(len);
+
+	err = ssdfs_zone_issue_move_request(pebi, &sub_fragment, req);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to issue zone move request: "
+			  "peb %llu, logical_blk_offset %llu, "
+			  "sub_extent (seg_id %llu, "
+			  "logical_blk %u, len %u), err %d\n",
+			  pebi->peb_id,
+			  sub_fragment.logical_blk_offset,
+			  le64_to_cpu(sub_fragment.extent.seg_id),
+			  le32_to_cpu(sub_fragment.extent.logical_blk),
+			  le32_to_cpu(sub_fragment.extent.len),
+			  err);
+		return err;
+	}
+
+	return 0;
+}
+
+/*
+ * ssdfs_zone_prepare_move_flush_request() - convert update into move request
+ * @pebi: pointer on PEB object
+ * @src: source I/O request
+ * @dst: destination I/O request
+ *
+ * This function tries to convert update request into
+ * move request.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_zone_prepare_move_flush_request(struct ssdfs_peb_info *pebi,
+					  struct ssdfs_segment_request *src,
+					  struct ssdfs_segment_request *dst)
+{
+	struct ssdfs_fs_info *fsi;
+	struct inode *inode;
+	struct ssdfs_inode_info *ii;
+	struct ssdfs_extents_btree_info *etree;
+	struct ssdfs_btree_search *search;
+	struct page *page;
+	struct ssdfs_blk2off_range new_extent;
+	struct ssdfs_raw_extent old_raw_extent;
+	struct ssdfs_raw_extent new_raw_extent;
+	u64 seg_id;
+	u32 logical_blk;
+	u32 len;
+	u64 logical_offset;
+	int i;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !src || !dst);
+
+	SSDFS_DBG("peb %llu, ino %llu, logical_offset %llu, "
+		  "data_bytes %u, cno %llu, parent_snapshot %llu, "
+		  "seg %llu, logical_block %u, cmd %#x, type %#x, "
+		  "processed_blks %d\n",
+		  pebi->peb_id, src->extent.ino, src->extent.logical_offset,
+		  src->extent.data_bytes, src->extent.cno,
+		  src->extent.parent_snapshot,
+		  src->place.start.seg_id, src->place.start.blk_index,
+		  src->private.cmd, src->private.type,
+		  src->result.processed_blks);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+
+	seg_id = src->place.start.seg_id;
+	logical_blk = src->place.start.blk_index;
+	len = src->place.len;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(seg_id != pebi->pebc->parent_si->seg_id);
+	BUG_ON(len > PAGEVEC_SIZE);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	page = src->result.pvec.pages[0];
+	inode = page->mapping->host;
+	ii = SSDFS_I(inode);
+
+	etree = SSDFS_EXTREE(ii);
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!etree);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	ssdfs_request_init(dst);
+	ssdfs_get_request(dst);
+
+	dst->private.flags |= SSDFS_REQ_DONT_FREE_PAGES;
+
+	logical_offset = src->extent.logical_offset;
+	ssdfs_request_prepare_logical_extent(src->extent.ino,
+					     logical_offset, len,
+					     0, 0, dst);
+
+	dst->place.start.seg_id = seg_id;
+	dst->place.start.blk_index = logical_blk;
+	dst->place.len = len;
+
+	dst->result.processed_blks = 0;
+
+	for (i = 0; i < pagevec_count(&src->result.pvec); i++) {
+		page = src->result.pvec.pages[i];
+
+#ifdef CONFIG_SSDFS_DEBUG
+		BUG_ON(!page);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+		dst->result.pvec.pages[i] = page;
+		src->result.pvec.pages[i] = NULL;
+	}
+
+	pagevec_reinit(&src->result.pvec);
+
+	dst->result.err = 0;
+	dst->result.processed_blks = 0;
+	atomic_set(&dst->result.state, SSDFS_UNKNOWN_REQ_RESULT);
+
+	err = ssdfs_segment_migrate_zone_extent_async(fsi,
+						      SSDFS_REQ_ASYNC_NO_FREE,
+						      dst,
+						      &seg_id,
+						      &new_extent);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to migrate zone extent: "
+			  "peb %llu, ino %llu, logical_blk_offset %llu, "
+			  "extent (seg_id %llu, logical_blk %u, len %u)\n",
+			  pebi->peb_id,
+			  src->extent.ino, src->extent.logical_offset,
+			  src->place.start.seg_id,
+			  src->place.start.blk_index,
+			  src->place.len);
+		return err;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(seg_id >= U64_MAX);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	old_raw_extent.seg_id = cpu_to_le64(src->place.start.seg_id);
+	old_raw_extent.logical_blk = cpu_to_le32(src->place.start.blk_index);
+	old_raw_extent.len = cpu_to_le32(src->place.len);
+
+	new_raw_extent.seg_id = cpu_to_le64(seg_id);
+	new_raw_extent.logical_blk = cpu_to_le32(new_extent.start_lblk);
+	new_raw_extent.len = cpu_to_le32(new_extent.len);
+
+	search = ssdfs_btree_search_alloc();
+	if (!search) {
+		SSDFS_ERR("fail to allocate btree search object\n");
+		return -ENOMEM;
+	}
+
+	ssdfs_btree_search_init(search);
+	err = ssdfs_extents_tree_move_extent(etree,
+					     src->extent.logical_offset,
+					     &old_raw_extent,
+					     &new_raw_extent,
+					     search);
+	ssdfs_btree_search_free(search);
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to move extent: "
+			  "old_extent (seg_id %llu, logical_blk %u, len %u), "
+			  "new_extent (seg_id %llu, logical_blk %u, len %u), "
+			  "err %d\n",
+			  le64_to_cpu(old_raw_extent.seg_id),
+			  le32_to_cpu(old_raw_extent.logical_blk),
+			  le32_to_cpu(old_raw_extent.len),
+			  le64_to_cpu(new_raw_extent.seg_id),
+			  le32_to_cpu(new_raw_extent.logical_blk),
+			  le32_to_cpu(new_raw_extent.len),
+			  err);
+		return err;
+	}
+
+	return 0;
+}
+
+enum {
+	SSDFS_ZONE_LEFT_EXTENT,
+	SSDFS_ZONE_MAIN_EXTENT,
+	SSDFS_ZONE_RIGHT_EXTENT,
+	SSDFS_ZONE_MIGRATING_EXTENTS
+};
+
+/*
+ * ssdfs_zone_move_extent() - move extent (ZNS SSD case)
+ * @pebi: pointer on PEB object
+ * @req: I/O request
+ *
+ * This function tries to move extent from exhausted zone
+ * into current zone for updates.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_zone_move_extent(struct ssdfs_peb_info *pebi,
+			   struct ssdfs_segment_request *req)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_invextree_info *invextree;
+	struct ssdfs_btree_search *search;
+	struct ssdfs_raw_extent extent;
+	struct ssdfs_segment_request *queue[SSDFS_ZONE_MIGRATING_EXTENTS] = {0};
+	struct ssdfs_zone_fragment migration;
+	struct ssdfs_zone_fragment left_fragment;
+	struct ssdfs_zone_fragment *left_fragment_ptr;
+	struct ssdfs_zone_fragment right_fragment;
+	struct ssdfs_zone_fragment *right_fragment_ptr;
+	size_t desc_size = sizeof(struct ssdfs_zone_fragment);
+	u32 rest_bytes;
+	int i;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !req);
+
+	SSDFS_DBG("peb %llu, ino %llu, logical_offset %llu, "
+		  "data_bytes %u, cno %llu, parent_snapshot %llu, "
+		  "seg %llu, logical_block %u, cmd %#x, type %#x, "
+		  "processed_blks %d\n",
+		  pebi->peb_id, req->extent.ino, req->extent.logical_offset,
+		  req->extent.data_bytes, req->extent.cno,
+		  req->extent.parent_snapshot,
+		  req->place.start.seg_id, req->place.start.blk_index,
+		  req->private.cmd, req->private.type,
+		  req->result.processed_blks);
+
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(req->place.start.seg_id != pebi->pebc->parent_si->seg_id);
+	BUG_ON(req->place.start.blk_index >=
+		pebi->pebc->parent_si->fsi->pages_per_seg);
+	switch (req->private.class) {
+	case SSDFS_PEB_UPDATE_REQ:
+	case SSDFS_PEB_PRE_ALLOC_UPDATE_REQ:
+	case SSDFS_PEB_DIFF_ON_WRITE_REQ:
+	case SSDFS_PEB_COLLECT_GARBAGE_REQ:
+		/* expected case */
+		break;
+	default:
+		BUG();
+		break;
+	}
+	BUG_ON(req->private.type >= SSDFS_REQ_TYPE_MAX);
+	BUG_ON(atomic_read(&req->private.refs_count) == 0);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	rest_bytes = ssdfs_request_rest_bytes(pebi, req);
+
+	memset(&migration, 0xFF, desc_size);
+	memset(&left_fragment, 0xFF, desc_size);
+	memset(&right_fragment, 0xFF, desc_size);
+
+	err = ssdfs_recommend_migration_extent(fsi, req,
+						&migration);
+	if (err == -ENODATA) {
+		err = 0;
+		/* do nothing */
+	} else if (unlikely(err)) {
+		SSDFS_ERR("fail to recommend migration extent: "
+			  "err %d\n", err);
+		goto finish_zone_move_extent;
+	} else {
+		err = ssdfs_extract_left_extent(req, &migration,
+						&left_fragment);
+		if (err == -ENODATA) {
+			err = 0;
+			SSDFS_DBG("no extent from the left\n");
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to extract left extent: "
+				  "seg_id %llu, peb_id %llu, "
+				  "logical_block %u, err %d\n",
+				  req->place.start.seg_id,
+				  pebi->peb_id,
+				  req->place.start.blk_index,
+				  err);
+			goto finish_zone_move_extent;
+		} else
+			left_fragment_ptr = &left_fragment;
+
+		err = ssdfs_extract_right_extent(req, &migration,
+						 &right_fragment);
+		if (err == -ENODATA) {
+			err = 0;
+			SSDFS_DBG("no extent from the right\n");
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to extract right extent: "
+				  "seg_id %llu, peb_id %llu, "
+				  "logical_block %u, err %d\n",
+				  req->place.start.seg_id,
+				  pebi->peb_id,
+				  req->place.start.blk_index,
+				  err);
+			goto finish_zone_move_extent;
+		} else
+			right_fragment_ptr = &right_fragment;
+	}
+
+	if (left_fragment_ptr) {
+		queue[SSDFS_ZONE_LEFT_EXTENT] = ssdfs_request_alloc();
+		if (IS_ERR_OR_NULL(queue[SSDFS_ZONE_LEFT_EXTENT])) {
+			SSDFS_ERR("fail to allocate segment request\n");
+			goto free_moving_requests;
+		}
+	}
+
+	queue[SSDFS_ZONE_MAIN_EXTENT] = ssdfs_request_alloc();
+	if (IS_ERR_OR_NULL(queue[SSDFS_ZONE_MAIN_EXTENT])) {
+		SSDFS_ERR("fail to allocate segment request\n");
+		goto free_moving_requests;
+	}
+
+	if (right_fragment_ptr) {
+		queue[SSDFS_ZONE_RIGHT_EXTENT] = ssdfs_request_alloc();
+		if (IS_ERR_OR_NULL(queue[SSDFS_ZONE_RIGHT_EXTENT])) {
+			SSDFS_ERR("fail to allocate segment request\n");
+			goto free_moving_requests;
+		}
+	}
+
+	if (left_fragment_ptr) {
+		err = ssdfs_zone_prepare_migration_request(pebi,
+					   left_fragment_ptr,
+					   queue[SSDFS_ZONE_LEFT_EXTENT]);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to prepare zone migration request: "
+				  "err %d\n", err);
+			goto free_moving_requests;
+		}
+	}
+
+	err = ssdfs_zone_prepare_move_flush_request(pebi, req,
+					queue[SSDFS_ZONE_MAIN_EXTENT]);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to prepare zone move request: "
+			  "err %d\n", err);
+		goto free_moving_requests;
+	}
+
+	if (right_fragment_ptr) {
+		err = ssdfs_zone_prepare_migration_request(pebi,
+					   left_fragment_ptr,
+					   queue[SSDFS_ZONE_RIGHT_EXTENT]);
+		if (unlikely(err)) {
+			SSDFS_ERR("fail to prepare zone migration request: "
+				  "err %d\n", err);
+			goto free_moving_requests;
+		}
+	}
+
+	for (i = 0; i < SSDFS_ZONE_MIGRATING_EXTENTS; i++) {
+		if (queue[i] == NULL)
+			continue;
+
+		err = ssdfs_check_zone_move_request(queue[i]);
+		if (unlikely(err)) {
+			SSDFS_ERR("flush request failed: "
+				  "index %d, err %d\n",
+				  i, err);
+		}
+
+		ssdfs_put_request(queue[i]);
+		ssdfs_request_free(queue[i]);
+	}
+
+	invextree = fsi->invextree;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!invextree);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	search = ssdfs_btree_search_alloc();
+	if (!search) {
+		SSDFS_ERR("fail to allocate btree search object\n");
+		return -ENOMEM;
+	}
+
+	extent.seg_id = migration.extent.seg_id;
+	extent.logical_blk = migration.extent.logical_blk;
+	extent.len = migration.extent.len;
+
+	ssdfs_btree_search_init(search);
+	err = ssdfs_invextree_add(invextree, &extent, search);
+	ssdfs_btree_search_free(search);
+
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to add invalidated extent: "
+			  "seg_id %llu, logical_blk %u, "
+			  "len %u, err %d\n",
+			  le64_to_cpu(extent.seg_id),
+			  le32_to_cpu(extent.logical_blk),
+			  le32_to_cpu(extent.len),
+			  err);
+		return err;
+	}
+
+	return 0;
+
+free_moving_requests:
+	for (i = 0; i < SSDFS_ZONE_MIGRATING_EXTENTS; i++) {
+		if (queue[i] == NULL)
+			continue;
+
+		ssdfs_put_request(queue[i]);
+		ssdfs_request_free(queue[i]);
+	}
+
+finish_zone_move_extent:
+	return err;
+}
+
+/*
+ * ssdfs_peb_update_block() - update data block
+ * @pebi: pointer on PEB object
+ * @req: I/O request
+ *
+ * This function tries to update data block in PEB.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ * %-EAGAIN     - try again to update data block.
+ * %-ENOENT     - need migrate base state before storing diff.
+ */
+static
+int ssdfs_peb_update_block(struct ssdfs_peb_info *pebi,
+			   struct ssdfs_segment_request *req)
+{
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !req);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+
+	SSDFS_DBG("ino %llu, seg %llu, peb %llu, logical_offset %llu, "
+		  "processed_blks %d, logical_block %u, data_bytes %u, "
+		  "cno %llu, parent_snapshot %llu, cmd %#x, type %#x\n",
+		  req->extent.ino, req->place.start.seg_id, pebi->peb_id,
+		  req->extent.logical_offset, req->result.processed_blks,
+		  req->place.start.blk_index,
+		  req->extent.data_bytes, req->extent.cno,
+		  req->extent.parent_snapshot,
+		  req->private.cmd, req->private.type);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	switch (atomic_read(&pebi->pebc->migration_phase)) {
+	case SSDFS_SHARED_ZONE_RECEIVES_DATA:
+		err = ssdfs_zone_move_extent(pebi, req);
+		break;
+
+	default:
+		err = __ssdfs_peb_update_block(pebi, req);
+		break;
+	}
+
+	return err;
+}
+
+/*
+ * __ssdfs_peb_update_extent() - update extent of blocks
+ * @pebi: pointer on PEB object
+ * @req: I/O request
+ *
+ * This function tries to update extent of blocks in PEB.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int __ssdfs_peb_update_extent(struct ssdfs_peb_info *pebi,
+			      struct ssdfs_segment_request *req)
+{
+	struct ssdfs_fs_info *fsi;
+	u32 blk;
+	u32 rest_bytes;
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !req);
+
+	SSDFS_DBG("peb %llu, ino %llu, logical_offset %llu, "
+		  "data_bytes %u, cno %llu, parent_snapshot %llu, "
+		  "seg %llu, logical_block %u, cmd %#x, type %#x, "
+		  "processed_blks %d\n",
+		  pebi->peb_id, req->extent.ino, req->extent.logical_offset,
+		  req->extent.data_bytes, req->extent.cno,
+		  req->extent.parent_snapshot,
+		  req->place.start.seg_id, req->place.start.blk_index,
+		  req->private.cmd, req->private.type,
+		  req->result.processed_blks);
+
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(req->place.start.seg_id != pebi->pebc->parent_si->seg_id);
+	BUG_ON(req->place.start.blk_index >=
+		pebi->pebc->parent_si->fsi->pages_per_seg);
+	switch (req->private.class) {
+	case SSDFS_PEB_UPDATE_REQ:
+	case SSDFS_PEB_PRE_ALLOC_UPDATE_REQ:
+	case SSDFS_PEB_DIFF_ON_WRITE_REQ:
+	case SSDFS_PEB_COLLECT_GARBAGE_REQ:
+		/* expected case */
+		break;
+	default:
+		BUG();
+		break;
+	}
+	BUG_ON(req->private.type >= SSDFS_REQ_TYPE_MAX);
+	BUG_ON(atomic_read(&req->private.refs_count) == 0);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	rest_bytes = ssdfs_request_rest_bytes(pebi, req);
+
+	while (rest_bytes > 0) {
+		blk = req->place.start.blk_index +
+				req->result.processed_blks;
+
+		err = __ssdfs_peb_update_block(pebi, req);
+		if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to update block: "
+				  "seg %llu, logical_block %u, "
+				  "peb %llu\n",
+				  req->place.start.seg_id, blk,
+				  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("need to migrate base state for diff: "
+				  "seg %llu, logical_block %u, "
+				  "peb %llu\n",
+				  req->place.start.seg_id, blk,
+				  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			SSDFS_ERR("fail to update block: "
+				  "seg %llu, logical_block %u, "
+				  "peb %llu, err %d\n",
+				  req->place.start.seg_id, blk,
+				  pebi->peb_id, err);
+			return err;
+		}
+
+		rest_bytes = ssdfs_request_rest_bytes(pebi, req);
+	};
+
+	return 0;
+}
+
+/*
+ * ssdfs_peb_update_extent() - update extent of blocks
+ * @pebi: pointer on PEB object
+ * @req: I/O request
+ *
+ * This function tries to update extent of blocks in PEB.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_update_extent(struct ssdfs_peb_info *pebi,
+			    struct ssdfs_segment_request *req)
+{
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !req);
+
+	SSDFS_DBG("peb %llu, ino %llu, logical_offset %llu, "
+		  "data_bytes %u, cno %llu, parent_snapshot %llu, "
+		  "seg %llu, logical_block %u, cmd %#x, type %#x, "
+		  "processed_blks %d\n",
+		  pebi->peb_id, req->extent.ino, req->extent.logical_offset,
+		  req->extent.data_bytes, req->extent.cno,
+		  req->extent.parent_snapshot,
+		  req->place.start.seg_id, req->place.start.blk_index,
+		  req->private.cmd, req->private.type,
+		  req->result.processed_blks);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	switch (atomic_read(&pebi->pebc->migration_phase)) {
+	case SSDFS_SHARED_ZONE_RECEIVES_DATA:
+		err = ssdfs_zone_move_extent(pebi, req);
+		break;
+
+	default:
+		err = __ssdfs_peb_update_extent(pebi, req);
+		break;
+	}
+
+	return err;
+}
+
+/*
+ * ssdfs_peb_migrate_pre_allocated_block() - migrate pre-allocated block
+ * @pebi: pointer on PEB object
+ * @req: I/O request
+ *
+ * This function tries to update data block in PEB.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-ERANGE     - internal error.
+ */
+static
+int ssdfs_peb_migrate_pre_allocated_block(struct ssdfs_peb_info *pebi,
+					  struct ssdfs_segment_request *req)
+{
+	struct ssdfs_fs_info *fsi;
+	struct ssdfs_segment_info *si;
+	struct ssdfs_blk2off_table *table;
+	struct ssdfs_segment_blk_bmap *seg_blkbmap;
+	struct ssdfs_phys_offset_descriptor *blk_desc_off;
+	struct ssdfs_peb_phys_offset desc_off = {0};
+	u16 peb_index;
+	u16 logical_block;
+	int processed_blks;
+	u64 logical_offset;
+	struct ssdfs_block_bmap_range range;
+	int range_state;
+	int migration_state = SSDFS_LBLOCK_UNKNOWN_STATE;
+	struct ssdfs_offset_position pos = {0};
+	u32 len;
+	u8 id;
+	int err;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !pebi->pebc || !req);
+	BUG_ON(!pebi->pebc->parent_si || !pebi->pebc->parent_si->fsi);
+	BUG_ON(req->place.start.seg_id != pebi->pebc->parent_si->seg_id);
+	BUG_ON(req->place.start.blk_index >=
+		pebi->pebc->parent_si->fsi->pages_per_seg);
+
+	switch (req->private.class) {
+	case SSDFS_PEB_COLLECT_GARBAGE_REQ:
+		/* expected state */
+		break;
+	default:
+		SSDFS_ERR("unexpected request: "
+			  "req->private.class %#x\n",
+			  req->private.class);
+		BUG();
+	};
+
+	switch (req->private.cmd) {
+	case SSDFS_MIGRATE_PRE_ALLOC_PAGE:
+		/* expected state */
+		break;
+
+	default:
+		SSDFS_ERR("unexpected request: "
+			  "req->private.cmd %#x\n",
+			  req->private.cmd);
+		BUG();
+	};
+
+	BUG_ON(req->private.type >= SSDFS_REQ_TYPE_MAX);
+	BUG_ON(atomic_read(&req->private.refs_count) == 0);
+	BUG_ON(req->extent.data_bytes > pebi->pebc->parent_si->fsi->pagesize);
+	BUG_ON(req->result.processed_blks > 0);
+
+	SSDFS_DBG("ino %llu, seg %llu, peb %llu, logical_offset %llu, "
+		  "processed_blks %d, logical_block %u, data_bytes %u, "
+		  "cno %llu, parent_snapshot %llu, cmd %#x, type %#x\n",
+		  req->extent.ino, req->place.start.seg_id, pebi->peb_id,
+		  req->extent.logical_offset, req->result.processed_blks,
+		  req->place.start.blk_index,
+		  req->extent.data_bytes, req->extent.cno,
+		  req->extent.parent_snapshot,
+		  req->private.cmd, req->private.type);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	fsi = pebi->pebc->parent_si->fsi;
+	si = pebi->pebc->parent_si;
+	table = pebi->pebc->parent_si->blk2off_table;
+	seg_blkbmap = &pebi->pebc->parent_si->blk_bmap;
+	processed_blks = req->result.processed_blks;
+	logical_block = req->place.start.blk_index + processed_blks;
+	logical_offset = req->extent.logical_offset +
+				((u64)processed_blks * fsi->pagesize);
+	logical_offset /= fsi->pagesize;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("seg %llu, peb %llu, logical_block %u, "
+		  "logical_offset %llu, "
+		  "processed_blks %d\n",
+		  req->place.start.seg_id, pebi->peb_id,
+		  logical_block, logical_offset,
+		  processed_blks);
+
+	if (req->extent.logical_offset >= U64_MAX) {
+		SSDFS_ERR("seg %llu, peb %llu, logical_block %u, "
+			  "logical_offset %llu, "
+			  "processed_blks %d\n",
+			  req->place.start.seg_id, pebi->peb_id,
+			  logical_block, logical_offset,
+			  processed_blks);
+		BUG();
+	}
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	len = req->extent.data_bytes;
+	len -= req->result.processed_blks * si->fsi->pagesize;
+	len >>= fsi->log_pagesize;
+
+	blk_desc_off = ssdfs_blk2off_table_convert(table,
+						   logical_block,
+						   &peb_index,
+						   &migration_state,
+						   &pos);
+	if (IS_ERR(blk_desc_off) && PTR_ERR(blk_desc_off) == -EAGAIN) {
+		struct completion *end = &table->full_init_end;
+
+		err = SSDFS_WAIT_COMPLETION(end);
+		if (unlikely(err)) {
+			SSDFS_ERR("blk2off init failed: "
+				  "err %d\n", err);
+			return err;
+		}
+
+		blk_desc_off = ssdfs_blk2off_table_convert(table,
+							   logical_block,
+							   &peb_index,
+							   &migration_state,
+							   &pos);
+	}
+
+	if (IS_ERR_OR_NULL(blk_desc_off)) {
+		err = (blk_desc_off == NULL ? -ERANGE : PTR_ERR(blk_desc_off));
+		SSDFS_ERR("fail to convert: "
+			  "logical_blk %u, err %d\n",
+			  logical_block, err);
+		return err;
+	}
+
+	if (migration_state == SSDFS_LBLOCK_UNDER_COMMIT) {
+#ifdef CONFIG_SSDFS_DEBUG
+		SSDFS_DBG("try again to add block: "
+			  "seg %llu, logical_block %u, peb %llu\n",
+			  req->place.start.seg_id, logical_block,
+			  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+		return -EAGAIN;
+	}
+
+	range.start = le16_to_cpu(blk_desc_off->page_desc.peb_page);
+	range.len = len;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	SSDFS_DBG("logical_blk %u, peb_page %u\n",
+		  logical_block, range.start);
+	SSDFS_DBG("range (start %u, len %u)\n",
+		  range.start, range.len);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	range_state = SSDFS_BLK_PRE_ALLOCATED;
+
+	err = ssdfs_segment_blk_bmap_update_range(seg_blkbmap, pebi->pebc,
+				blk_desc_off->blk_state.peb_migration_id,
+				range_state, &range);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to update range: "
+			  "seg %llu, peb %llu, "
+			  "range (start %u, len %u), err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id, range.start, range.len,
+			  err);
+		return err;
+	}
+
+	id = ssdfs_get_peb_migration_id_checked(pebi);
+	if (unlikely(id < 0)) {
+		SSDFS_ERR("invalid peb_migration_id: "
+			  "seg %llu, peb_id %llu, err %d\n",
+			  pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id, id);
+		return id;
+	}
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(id > U8_MAX);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	desc_off.peb_index = pebi->peb_index;
+	desc_off.peb_migration_id = id;
+	desc_off.peb_page = (u16)range.start;
+	desc_off.log_area = SSDFS_LOG_AREA_MAX;
+	desc_off.byte_offset = U32_MAX;
+
+	err = ssdfs_peb_store_block_descriptor_offset(pebi,
+						(u32)logical_offset,
+						logical_block,
+						NULL,
+						&desc_off);
+	if (unlikely(err)) {
+		SSDFS_ERR("fail to store block descriptor offset: "
+			  "logical_block %u, logical_offset %llu, "
+			  "err %d\n",
+			  logical_block, logical_offset, err);
+		return err;
+	}
+
+	req->result.processed_blks += range.len;
+	return 0;
+}
+
+/*
+ * ssdfs_process_update_request() - process update request
+ * @pebi: pointer on PEB object
+ * @req: request
+ *
+ * This function detects command of request and
+ * to call a proper function for request processing.
+ *
+ * RETURN:
+ * [success]
+ * [failure] - error code:
+ *
+ * %-EINVAL     - invalid input.
+ * %-EAGAIN     - unable to update block.
+ */
+static
+int ssdfs_process_update_request(struct ssdfs_peb_info *pebi,
+				 struct ssdfs_segment_request *req)
+{
+	int err = 0;
+
+#ifdef CONFIG_SSDFS_DEBUG
+	BUG_ON(!pebi || !req);
+
+	SSDFS_DBG("req %p, cmd %#x, type %#x\n",
+		  req, req->private.cmd, req->private.type);
+#endif /* CONFIG_SSDFS_DEBUG */
+
+	if (req->private.cmd <= SSDFS_CREATE_CMD_MAX ||
+	    req->private.cmd >= SSDFS_COLLECT_GARBAGE_CMD_MAX) {
+		SSDFS_ERR("unknown update command %d, seg %llu, peb %llu\n",
+			  req->private.cmd, pebi->pebc->parent_si->seg_id,
+			  pebi->peb_id);
+		req->result.err = -EINVAL;
+		atomic_set(&req->result.state, SSDFS_REQ_FAILED);
+		return -EINVAL;
+	}
+
+	atomic_set(&req->result.state, SSDFS_REQ_STARTED);
+
+	switch (req->private.cmd) {
+	case SSDFS_UPDATE_BLOCK:
+	case SSDFS_UPDATE_PRE_ALLOC_BLOCK:
+		err = ssdfs_peb_update_block(pebi, req);
+		if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to update block: "
+				  "seg %llu, peb %llu\n",
+				  req->place.start.seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			ssdfs_fs_error(pebi->pebc->parent_si->fsi->sb,
+				__FILE__, __func__, __LINE__,
+				"fail to update block: "
+				"seg %llu, peb %llu, err %d\n",
+				pebi->pebc->parent_si->seg_id,
+				pebi->peb_id, err);
+#ifdef CONFIG_SSDFS_DEBUG
+			BUG();
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+		break;
+
+	case SSDFS_UPDATE_EXTENT:
+	case SSDFS_UPDATE_PRE_ALLOC_EXTENT:
+		err = ssdfs_peb_update_extent(pebi, req);
+		if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to update block: "
+				  "seg %llu, peb %llu\n",
+				  req->place.start.seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			ssdfs_fs_error(pebi->pebc->parent_si->fsi->sb,
+				__FILE__, __func__, __LINE__,
+				"fail to update extent: "
+				"seg %llu, peb %llu, err %d\n",
+				pebi->pebc->parent_si->seg_id,
+				pebi->peb_id, err);
+#ifdef CONFIG_SSDFS_DEBUG
+			BUG();
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+		break;
+
+	case SSDFS_BTREE_NODE_DIFF:
+		err = ssdfs_peb_update_extent(pebi, req);
+		if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to update extent: "
+				  "seg %llu, peb %llu\n",
+				  req->place.start.seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (err == -ENOENT) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("need to migrate base state for diff: "
+				  "seg %llu, peb %llu\n",
+				  req->place.start.seg_id,
+				  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			ssdfs_fs_error(pebi->pebc->parent_si->fsi->sb,
+				__FILE__, __func__, __LINE__,
+				"fail to update extent: "
+				"seg %llu, peb %llu, err %d\n",
+				pebi->pebc->parent_si->seg_id,
+				pebi->peb_id, err);
+#ifdef CONFIG_SSDFS_DEBUG
+			BUG();
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+		break;
+
+	case SSDFS_USER_DATA_DIFF:
+		err = ssdfs_peb_update_block(pebi, req);
+		if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to update block: "
+				  "seg %llu, peb %llu\n",
+				  req->place.start.seg_id, pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			ssdfs_fs_error(pebi->pebc->parent_si->fsi->sb,
+				__FILE__, __func__, __LINE__,
+				"fail to update block: "
+				"seg %llu, peb %llu, err %d\n",
+				pebi->pebc->parent_si->seg_id,
+				pebi->peb_id, err);
+#ifdef CONFIG_SSDFS_DEBUG
+			BUG();
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+		break;
+
+	case SSDFS_COMMIT_LOG_NOW:
+	case SSDFS_START_MIGRATION_NOW:
+	case SSDFS_EXTENT_WAS_INVALIDATED:
+		/* simply continue logic */
+		break;
+
+	case SSDFS_MIGRATE_RANGE:
+		err = ssdfs_peb_update_extent(pebi, req);
+		if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to migrate extent: "
+				  "seg %llu, peb %llu\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			ssdfs_fs_error(pebi->pebc->parent_si->fsi->sb,
+				__FILE__, __func__, __LINE__,
+				"fail to migrate extent: "
+				"seg %llu, peb %llu, err %d\n",
+				pebi->pebc->parent_si->seg_id,
+				pebi->peb_id, err);
+#ifdef CONFIG_SSDFS_DEBUG
+			BUG();
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+		break;
+
+	case SSDFS_MIGRATE_PRE_ALLOC_PAGE:
+		err = ssdfs_peb_migrate_pre_allocated_block(pebi, req);
+		if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to migrate pre-alloc page: "
+				  "seg %llu, peb %llu\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			ssdfs_fs_error(pebi->pebc->parent_si->fsi->sb,
+				__FILE__, __func__, __LINE__,
+				"fail to migrate pre-alloc page: "
+				"seg %llu, peb %llu, err %d\n",
+				pebi->pebc->parent_si->seg_id,
+				pebi->peb_id, err);
+#ifdef CONFIG_SSDFS_DEBUG
+			BUG();
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+		break;
+
+	case SSDFS_MIGRATE_FRAGMENT:
+		err = ssdfs_peb_update_block(pebi, req);
+		if (err == -EAGAIN) {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("unable to migrate fragment: "
+				  "seg %llu, peb %llu\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->peb_id);
+#endif /* CONFIG_SSDFS_DEBUG */
+			return err;
+		} else if (unlikely(err)) {
+			ssdfs_fs_error(pebi->pebc->parent_si->fsi->sb,
+				__FILE__, __func__, __LINE__,
+				"fail to migrate fragment: "
+				"seg %llu, peb %llu, err %d\n",
+				pebi->pebc->parent_si->seg_id,
+				pebi->peb_id, err);
+#ifdef CONFIG_SSDFS_DEBUG
+			BUG();
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+		break;
+
+	default:
+		BUG();
+	}
+
+	if (unlikely(err)) {
+		/* request failed */
+		atomic_set(&req->result.state, SSDFS_REQ_FAILED);
+	} else if (is_ssdfs_peb_containing_user_data(pebi->pebc)) {
+		struct ssdfs_peb_container *pebc = pebi->pebc;
+		int processed_blks = req->result.processed_blks;
+		u32 pending = 0;
+
+		switch (req->private.cmd) {
+		case SSDFS_UPDATE_BLOCK:
+		case SSDFS_UPDATE_PRE_ALLOC_BLOCK:
+		case SSDFS_UPDATE_EXTENT:
+		case SSDFS_UPDATE_PRE_ALLOC_EXTENT:
+		case SSDFS_BTREE_NODE_DIFF:
+		case SSDFS_USER_DATA_DIFF:
+		case SSDFS_MIGRATE_RANGE:
+		case SSDFS_MIGRATE_PRE_ALLOC_PAGE:
+		case SSDFS_MIGRATE_FRAGMENT:
+			spin_lock(&pebc->pending_lock);
+			pending = pebc->pending_updated_user_data_pages;
+			if (pending >= processed_blks) {
+				pebc->pending_updated_user_data_pages -=
+								processed_blks;
+				pending = pebc->pending_updated_user_data_pages;
+			} else {
+				/* wrong accounting */
+				err = -ERANGE;
+			}
+			spin_unlock(&pebc->pending_lock);
+			break;
+
+		default:
+			/* do nothing */
+			break;
+		}
+
+		if (unlikely(err)) {
+			SSDFS_ERR("pending %u < processed_blks %d\n",
+				  pending, processed_blks);
+		} else {
+#ifdef CONFIG_SSDFS_DEBUG
+			SSDFS_DBG("seg_id %llu, peb_index %u, pending %u\n",
+				  pebi->pebc->parent_si->seg_id,
+				  pebi->pebc->peb_index,
+				  pending);
+#endif /* CONFIG_SSDFS_DEBUG */
+		}
+	}
+
+	return err;
+}
+
 /*
  * ssdfs_peb_has_dirty_pages() - check that PEB has dirty pages
  * @pebi: pointer on PEB object
Signature · 1 line
2.34.1