shithub: lwext4

Download patch

ref: 2548f2436138aa5fb080920be0a5ac7e02001199
parent: cb2a68d7aa16480a753ca55e3b87e80527a9b5d0
author: gkostka <[email protected]>
date: Fri Oct 16 14:50:48 EDT 2015

Extent full & simple API unification

--- a/lwext4/ext4_extent.c
+++ b/lwext4/ext4_extent.c
@@ -123,7 +123,14 @@
 	*extent = l - 1;
 }
 
-int ext4_extent_find_block(struct ext4_inode_ref *inode_ref, uint32_t iblock,
+/**@brief Get physical block in the extent tree by logical block number.
+ * There is no need to save path in the tree during this algorithm.
+ * @param inode_ref I-node to load block from
+ * @param iblock    Logical block number to find
+ * @param fblock    Output value for physical block number
+ * @return Error code*/
+static int
+ext4_extent_find_block(struct ext4_inode_ref *inode_ref, uint32_t iblock,
 			   uint32_t *fblock)
 {
 	int rc;
@@ -348,13 +355,16 @@
 	return ext4_balloc_free_block(inode_ref, fblock);
 }
 
-int ext4_extent_release_blocks_from(struct ext4_inode_ref *inode_ref,
-				    uint32_t iblock_from)
+int ext4_extent_remove_space(struct ext4_inode_ref *inode_ref, ext4_lblk_t from,
+			     ext4_lblk_t to)
 {
+	if (to != (ext4_lblk_t)-1)
+		return ENOTSUP;
+
 	/* Find the first extent to modify */
 	struct ext4_extent_path *path;
 	uint16_t i;
-	int rc = ext4_extent_find_extent(inode_ref, iblock_from, &path);
+	int rc = ext4_extent_find_extent(inode_ref, from, &path);
 	if (rc != EOK)
 		return rc;
 
@@ -368,7 +378,7 @@
 	/* First extent maybe released partially */
 	uint32_t first_iblock = ext4_extent_get_first_block(path_ptr->extent);
 	uint32_t first_fblock = ext4_extent_get_start(path_ptr->extent) +
-				iblock_from - first_iblock;
+				from - first_iblock;
 
 	uint16_t block_count = ext4_extent_get_block_count(path_ptr->extent);
 
@@ -716,7 +726,17 @@
 	return EOK;
 }
 
-int ext4_extent_append_block(struct ext4_inode_ref *inode_ref, uint32_t *iblock,
+/**@brief Append data block to the i-node.
+ * This function allocates data block, tries to append it
+ * to some existing extent or creates new extents.
+ * It includes possible extent tree modifications (splitting).
+ * @param inode_ref I-node to append block to
+ * @param iblock    Output logical number of newly allocated block
+ * @param fblock    Output physical block address of newly allocated block
+ *
+ * @return Error code*/
+static int
+ext4_extent_append_block(struct ext4_inode_ref *inode_ref, uint32_t *iblock,
 			     uint32_t *fblock, bool update_size)
 {
 	uint16_t i;
@@ -870,6 +890,28 @@
 	free(path);
 
 	return rc;
+}
+
+int ext4_extent_get_blocks(struct ext4_inode_ref *inode_ref, ext4_fsblk_t iblock,
+			   uint32_t max_blocks, ext4_fsblk_t *result, bool create,
+			   uint32_t *blocks_count)
+{
+	uint32_t iblk = iblock;
+	uint32_t fblk = 0;
+	int r;
+
+	if (blocks_count)
+		return ENOTSUP;
+	if (max_blocks != 1)
+		return ENOTSUP;
+
+	if (!create)
+		r = ext4_extent_find_block(inode_ref, iblk, &fblk);
+	else
+		r = ext4_extent_append_block(inode_ref, &iblk, &fblk, false);
+
+	*result = fblk;
+	return r;
 }
 
 #endif
--- a/lwext4/ext4_extent.h
+++ b/lwext4/ext4_extent.h
@@ -43,8 +43,8 @@
 
 #include "ext4_config.h"
 #include "ext4_types.h"
+#include "ext4_inode.h"
 
-#if !CONFIG_EXTENT_FULL
 
 /**@brief Get logical number of the block covered by extent.
  * @param extent Extent to load number from
@@ -237,35 +237,42 @@
 	header->generation = to_le32(generation);
 }
 
-/**@brief Find physical block in the extent tree by logical block number.
- * There is no need to save path in the tree during this algorithm.
- * @param inode_ref I-node to load block from
- * @param iblock    Logical block number to find
- * @param fblock    Output value for physical block number
- * @return Error code*/
-int ext4_extent_find_block(struct ext4_inode_ref *inode_ref, uint32_t iblock,
-			   uint32_t *fblock);
+/******************************************************************************/
 
+/**TODO:  */
+static inline void ext4_extent_tree_init(struct ext4_inode_ref *inode_ref)
+{
+	/* Initialize extent root header */
+	struct ext4_extent_header *header =
+			ext4_inode_get_extent_header(inode_ref->inode);
+	ext4_extent_header_set_depth(header, 0);
+	ext4_extent_header_set_entries_count(header, 0);
+	ext4_extent_header_set_generation(header, 0);
+	ext4_extent_header_set_magic(header, EXT4_EXTENT_MAGIC);
+
+	uint16_t max_entries = (EXT4_INODE_BLOCKS * sizeof(uint32_t) -
+			sizeof(struct ext4_extent_header)) /
+					sizeof(struct ext4_extent);
+
+	ext4_extent_header_set_max_entries_count(header, max_entries);
+	inode_ref->dirty  = true;
+}
+
+
+
+/**TODO:  */
+int ext4_extent_get_blocks(struct ext4_inode_ref *inode_ref, ext4_fsblk_t iblock,
+			   uint32_t max_blocks, ext4_fsblk_t *result, bool create,
+			   uint32_t *blocks_count);
+
+
 /**@brief Release all data blocks starting from specified logical block.
  * @param inode_ref   I-node to release blocks from
  * @param iblock_from First logical block to release
  * @return Error code */
-int ext4_extent_release_blocks_from(struct ext4_inode_ref *inode_ref,
-				    uint32_t iblock_from);
+int ext4_extent_remove_space(struct ext4_inode_ref *inode_ref, ext4_lblk_t from,
+			     ext4_lblk_t to);
 
-/**@brief Append data block to the i-node.
- * This function allocates data block, tries to append it
- * to some existing extent or creates new extents.
- * It includes possible extent tree modifications (splitting).
- * @param inode_ref I-node to append block to
- * @param iblock    Output logical number of newly allocated block
- * @param fblock    Output physical block address of newly allocated block
- *
- * @return Error code*/
-int ext4_extent_append_block(struct ext4_inode_ref *inode_ref, uint32_t *iblock,
-			     uint32_t *fblock, bool update_size);
-
-#endif
 
 #endif /* EXT4_EXTENT_H_ */
 /**
--- a/lwext4/ext4_extent_full.c
+++ b/lwext4/ext4_extent_full.c
@@ -37,7 +37,7 @@
 #include <inttypes.h>
 #include <stddef.h>
 
-#include "ext4_extent_full.h"
+#include "ext4_extent.h"
 
 #if CONFIG_EXTENT_FULL
 
@@ -1372,7 +1372,7 @@
 	return 1;
 }
 
-int ext4_ext_remove_space(struct ext4_inode_ref *inode_ref, ext4_lblk_t from,
+int ext4_extent_remove_space(struct ext4_inode_ref *inode_ref, ext4_lblk_t from,
 			  ext4_lblk_t to)
 {
 	struct ext4_extent_path *path = NULL;
@@ -1566,19 +1566,6 @@
 	return err;
 }
 
-int ext4_ext_tree_init(struct ext4_inode_ref *inode_ref)
-{
-	struct ext4_extent_header *eh;
-
-	eh = ext_inode_hdr(inode_ref->inode);
-	eh->depth = 0;
-	eh->entries_count = 0;
-	eh->magic = to_le16(EXT4_EXTENT_MAGIC);
-	eh->max_entries_count = to_le16(ext4_ext_space_root(inode_ref));
-	inode_ref->dirty = true;
-	return EOK;
-}
-
 /*
  * ext4_ext_next_allocated_block:
  * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
@@ -1641,7 +1628,7 @@
 	return err;
 }
 
-int ext4_ext_get_blocks(struct ext4_inode_ref *inode_ref, ext4_fsblk_t iblock,
+int ext4_extent_get_blocks(struct ext4_inode_ref *inode_ref, ext4_fsblk_t iblock,
 			uint32_t max_blocks, ext4_fsblk_t *result, bool create,
 			uint32_t *blocks_count)
 {
--- a/lwext4/ext4_extent_full.h
+++ /dev/null
@@ -1,46 +1,0 @@
-/*
- * Copyright (c) 2015 Grzegorz Kostka ([email protected])
- * Copyright (c) 2015 Kaho Ng ([email protected])
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- *   notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- *   notice, this list of conditions and the following disclaimer in the
- *   documentation and/or other materials provided with the distribution.
- * - The name of the author may not be used to endorse or promote products
- *   derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef EXT4_EXTENT_FULL_H_
-#define EXT4_EXTENT_FULL_H_
-
-#include "ext4_config.h"
-#include "ext4_types.h"
-
-#if CONFIG_EXTENT_FULL
-int ext4_ext_get_blocks(struct ext4_inode_ref *inode_ref, ext4_fsblk_t iblock,
-			uint32_t max_blocks, ext4_fsblk_t *result, bool create,
-			uint32_t *blocks_count);
-
-int ext4_ext_tree_init(struct ext4_inode_ref *inode_ref);
-
-int ext4_ext_remove_space(struct ext4_inode_ref *inode_ref, ext4_lblk_t from,
-			  ext4_lblk_t to);
-#endif
-
-#endif /* EXT4_EXTENT_H_ */
--- a/lwext4/ext4_fs.c
+++ b/lwext4/ext4_fs.c
@@ -51,7 +51,6 @@
 #include "ext4_inode.h"
 #include "ext4_ialloc.h"
 #include "ext4_extent.h"
-#include "ext4_extent_full.h"
 
 #include <string.h>
 
@@ -698,7 +697,7 @@
 		ext4_inode_set_flag(inode, EXT4_INODE_FLAG_EXTENTS);
 
 		/* Initialize extent root header */
-		ext4_ext_tree_init(inode_ref);
+		ext4_extent_tree_init(inode_ref);
 	}
 #endif
 }
@@ -974,10 +973,8 @@
 
 		/* Extents require special operation */
 		if (diff_blocks_count) {
-			int rc = ext4_ext_remove_space(
-					inode_ref,
-					new_blocks_count,
-					(ext4_lblk_t)-1);
+			int rc = ext4_extent_remove_space(inode_ref,
+					new_blocks_count, (ext4_lblk_t)-1);
 			if (rc != EOK)
 				return rc;
 
@@ -1026,8 +1023,8 @@
 	    (ext4_inode_has_flag(inode_ref->inode, EXT4_INODE_FLAG_EXTENTS))) {
 
 		ext4_fsblk_t current_fsblk;
-		int rc = ext4_ext_get_blocks(inode_ref,	iblock, 1, &current_fsblk,
-					extent_create, NULL);
+		int rc = ext4_extent_get_blocks(inode_ref, iblock, 1,
+				&current_fsblk,	extent_create, NULL);
 		if (rc != EOK)
 			return rc;
 
@@ -1412,8 +1409,8 @@
 		*iblock = (inode_size + block_size - 1) /
 				    block_size;
 
-		rc = ext4_ext_get_blocks(inode_ref, *iblock, 1,	&current_fsblk,
-					 true, NULL);
+		rc = ext4_extent_get_blocks(inode_ref, *iblock, 1,
+				&current_fsblk, true, NULL);
 
 
 		*fblock = current_fsblk;