SSDFS: flash-friendly LFS file system for ZNS SSD
RFC PATCH 11/76 ssdfs: block bitmap search operations implementation
In reply to Viacheslav DubeykoImplement internal block bitmap's search operations for pre_allocate, allocate, and collect_garbage operations.
--- fs/ssdfs/block_bitmap.c | 3401 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 3401 insertions(+)
fs/ssdfs/block_bitmap.c +3401 -0
@@ -1207,3 +1207,3404 @@ void ssdfs_block_bmap_forget_snapshot(struct ssdfs_page_vector *snapshot)ssdfs_page_vector_release (snapshot); } + +/* +* ssdfs_block_bmap_lock() - lock segment's block bitmap +* @blk_bmap: pointer on block bitmap +*/ +int ssdfs_block_bmap_lock (struct ssdfs_block_bmap *blk_bmap) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk_bmap %p\n" , blk_bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =mutex_lock_killable (&blk_bmap->lock); +if (err) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); +return err; + } + +return 0 ; +} + +/* +* ssdfs_block_bmap_is_locked() - check that block bitmap is locked +* @blk_bmap: pointer on block bitmap +*/ +bool ssdfs_block_bmap_is_locked (struct ssdfs_block_bmap *blk_bmap) +{ +return mutex_is_locked (&blk_bmap->lock); +} + +/* +* ssdfs_block_bmap_unlock() - unlock segment's block bitmap +* @blk_bmap: pointer on block bitmap +*/ +void ssdfs_block_bmap_unlock (struct ssdfs_block_bmap *blk_bmap) +{ +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk_bmap %p\n" , blk_bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + +mutex_unlock (&blk_bmap->lock); +} + +/* +* ssdfs_get_cache_type() - define cache type for block +* @blk_bmap: pointer on block bitmap +* @blk: block number +* +* RETURN: +* [success] - cache type +* [failure] - SSDFS_SEARCH_TYPE_MAX +*/ +static +int ssdfs_get_cache_type (struct ssdfs_block_bmap *blk_bmap, +u32 blk) +{ +int page_index; +u16 offset; +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +SSDFS_DBG ("blk_bmap %p, block %u\n" , blk_bmap, blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + page_index =SSDFS_BLK2PAGE (blk,SSDFS_BLK_STATE_BITS , &offset); + +for (i =0 ; i <SSDFS_SEARCH_TYPE_MAX ; i++) { +struct ssdfs_last_bmap_search *last; + + last = &blk_bmap->last_search[i]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last->page_index %d, page_index %d, " +"last->offset %u, offset %u, " +"search_type %#x\n" , + last->page_index, page_index, + last->offset, offset, i); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (last->page_index == page_index && + last->offset == offset) +return i; + } + +return SSDFS_SEARCH_TYPE_MAX ; +} + +/* +* is_block_state_cached() - check that block state is in cache +* @blk_bmap: pointer on block bitmap +* @blk: block number +* +* RETURN: +* [true] - block state is in cache +* [false] - cache doesn't contain block state +*/ +static +bool is_block_state_cached (struct ssdfs_block_bmap *blk_bmap, +u32 blk) +{ +int cache_type; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +SSDFS_DBG ("blk_bmap %p, block %u\n" , blk_bmap, blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + cache_type =ssdfs_get_cache_type (blk_bmap, blk); + +if (cache_type <0 ) { +SSDFS_ERR ("invalid cache type %d\n" , cache_type); +return false; + } + +if (cache_type >=SSDFS_SEARCH_TYPE_MAX ) +return false; + +return true; +} + +/* +* ssdfs_determine_cache_type() - detect type of cache for value +* @cache: value for caching +* +* RETURN: suggested type of cache +*/ +static +int ssdfs_determine_cache_type (unsigned long cache) +{ +size_t bytes_per_long =sizeof (cache); +size_t criterion = bytes_per_long /2 ; +u8 bytes[SSDFS_BLK_STATE_MAX ] = {0 }; +int i; + +for (i =0 ; i < bytes_per_long; i++) { +int cur_state = (int )((cache >> (i *BITS_PER_BYTE )) &0xFF ); + +switch (cur_state) { +case SSDFS_FREE_STATES_BYTE : + bytes[SSDFS_BLK_FREE ]++; +break ; + +case SSDFS_PRE_ALLOC_STATES_BYTE : + bytes[SSDFS_BLK_PRE_ALLOCATED ]++; +break ; + +case SSDFS_VALID_STATES_BYTE : + bytes[SSDFS_BLK_VALID ]++; +break ; + +case SSDFS_INVALID_STATES_BYTE : + bytes[SSDFS_BLK_INVALID ]++; +break ; + +default : +/* mix of block states */ +break ; + }; + } + +if (bytes[SSDFS_BLK_FREE ] > criterion) +return SSDFS_FREE_BLK_SEARCH ; +else if (bytes[SSDFS_BLK_VALID ] > criterion) +return SSDFS_VALID_BLK_SEARCH ; + +return SSDFS_OTHER_BLK_SEARCH ; +} + +/* +* ssdfs_cache_block_state() - cache block state from pagevec +* @blk_bmap: pointer on block bitmap +* @blk: segment's block +* @blk_state: state as hint for cache type determination +* +* This function retrieves state of @blk from pagevec +* and save retrieved value for requested type of cache. +* If @blk_state has SSDFS_BLK_STATE_MAX value then function +* defines block state and to cache value in proper place. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-EOPNOTSUPP - invalid page index. +*/ +static +int ssdfs_cache_block_state (struct ssdfs_block_bmap *blk_bmap, +u32 blk,int blk_state) +{ +struct ssdfs_page_vector *array; +int page_index; +u16 offset; +void *kaddr; +unsigned long cache; +int cache_type; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +SSDFS_DBG ("blk_bmap %p, block %u, state %#x\n" , + blk_bmap, blk, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (blk_state >SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (is_block_state_cached (blk_bmap, blk)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("block %u has been cached already\n" , blk); +#endif /* CONFIG_SSDFS_DEBUG */ +return 0 ; + } + + page_index =SSDFS_BLK2PAGE (blk,SSDFS_BLK_STATE_BITS , &offset); + +switch (blk_bmap->storage.state) { +case SSDFS_BLOCK_BMAP_STORAGE_PAGE_VEC : + array = &blk_bmap->storage.array; + +if (page_index >=ssdfs_page_vector_capacity (array)) { +SSDFS_ERR ("invalid page index %d\n" , page_index); +return -EOPNOTSUPP ; + } + +if (page_index >=ssdfs_page_vector_count (array)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("absent page index %d\n" , page_index); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOENT ; + } + + err =ssdfs_memcpy_from_page (&cache, +0 ,sizeof (unsigned long ), + array->pages[page_index], + offset,PAGE_SIZE , +sizeof (unsigned long )); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); +return err; + } +break ; + +case SSDFS_BLOCK_BMAP_STORAGE_BUFFER : +if (page_index >0 ) { +SSDFS_ERR ("invalid page_index %d\n" , page_index); +return -ERANGE ; + } + + kaddr = blk_bmap->storage.buf; + err =ssdfs_memcpy (&cache,0 ,sizeof (unsigned long ), + kaddr, offset, blk_bmap->bytes_count, +sizeof (unsigned long )); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); +return err; + } +break ; + +default : +SSDFS_ERR ("unexpected state %#x\n" , blk_bmap->storage.state); +return -ERANGE ; + } + + cache_type =ssdfs_determine_cache_type (cache); +BUG_ON (cache_type >=SSDFS_SEARCH_TYPE_MAX ); + + blk_bmap->last_search[cache_type].page_index = page_index; + blk_bmap->last_search[cache_type].offset = offset; + blk_bmap->last_search[cache_type].cache = cache; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last_search.cache %lx, cache_type %#x, " +"page_index %d, offset %u\n" , + cache, cache_type, + page_index, offset); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_define_bits_shift_in_cache() - calculate bit shift of block in cache +* @blk_bmap: pointer on block bitmap +* @cache_type: type of cache +* @blk: segment's block +* +* This function calculates bit shift of @blk in cache of +* @cache_type. +* +* RETURN: +* [success] - bit shift +* [failure] - error code: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_define_bits_shift_in_cache (struct ssdfs_block_bmap *blk_bmap, +int cache_type,u32 blk) +{ +struct ssdfs_last_bmap_search *last_search; +u32 first_cached_block, diff; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +if (blk >= blk_bmap->items_count) { +SSDFS_ERR ("invalid block %u\n" , blk); +return -EINVAL ; + } + +if (cache_type <0 ) { +SSDFS_ERR ("invalid cache type %d\n" , cache_type); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, cache_type %#x, blk %u\n" , + blk_bmap, cache_type, blk); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (cache_type >=SSDFS_SEARCH_TYPE_MAX ) { +SSDFS_ERR ("cache doesn't contain block %u\n" , blk); +return -EINVAL ; + } + + last_search = &blk_bmap->last_search[cache_type]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last_search.cache %lx\n" , last_search->cache); +#endif /* CONFIG_SSDFS_DEBUG */ + + first_cached_block =SSDFS_FIRST_CACHED_BLOCK (last_search); + +if (first_cached_block > blk) { +SSDFS_ERR ("first_cached_block %u > blk %u\n" , + first_cached_block, blk); +return -EINVAL ; + } + + diff = blk - first_cached_block; + +#ifdef CONFIG_SSDFS_DEBUG +if (diff >= (U32_MAX /SSDFS_BLK_STATE_BITS )) { +SSDFS_ERR ("invalid diff %u; blk %u, first_cached_block %u\n" , + diff, blk, first_cached_block); +return -EINVAL ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + + diff *=SSDFS_BLK_STATE_BITS ; + +#ifdef CONFIG_SSDFS_DEBUG +if (diff > (BITS_PER_LONG -SSDFS_BLK_STATE_BITS )) { +SSDFS_ERR ("invalid diff %u; bits_per_long %u, " +"bits_per_state %u\n" , + diff,BITS_PER_LONG ,SSDFS_BLK_STATE_BITS ); +return -EINVAL ; + } + +SSDFS_DBG ("diff %u\n" , diff); +#endif /* CONFIG_SSDFS_DEBUG */ + +return (int )diff; +} + +/* +* ssdfs_get_block_state_from_cache() - retrieve block state from cache +* @blk_bmap: pointer on block bitmap +* @blk: segment's block +* +* This function retrieve state of @blk from cache. +* +* RETURN: +* [success] - state of block +* [failure] - error code: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_get_block_state_from_cache (struct ssdfs_block_bmap *blk_bmap, +u32 blk) +{ +int cache_type; +struct ssdfs_last_bmap_search *last_search; +int shift; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +if (blk >= blk_bmap->items_count) { +SSDFS_ERR ("invalid block %u\n" , blk); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, block %u\n" , blk_bmap, blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + cache_type =ssdfs_get_cache_type (blk_bmap, blk); + shift =ssdfs_define_bits_shift_in_cache (blk_bmap, cache_type, blk); +if (unlikely (shift <0 )) { +SSDFS_ERR ("fail to define bits shift: " +"cache_type %d, blk %u, err %d\n" , + cache_type, blk, shift); +return shift; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (cache_type >=SSDFS_SEARCH_TYPE_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + last_search = &blk_bmap->last_search[cache_type]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last_search.cache %lx\n" , last_search->cache); +#endif /* CONFIG_SSDFS_DEBUG */ + +return (int )((last_search->cache >> shift) &SSDFS_BLK_STATE_MASK ); +} + +/* +* ssdfs_set_block_state_in_cache() - set block state in cache +* @blk_bmap: pointer on block bitmap +* @blk: segment's block +* @blk_state: new state of @blk +* +* This function sets state @blk_state of @blk in cache. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_set_block_state_in_cache (struct ssdfs_block_bmap *blk_bmap, +u32 blk,int blk_state) +{ +int cache_type; +int shift; +unsigned long value, *cached_value; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +if (blk >= blk_bmap->items_count) { +SSDFS_ERR ("invalid block %u\n" , blk); +return -EINVAL ; + } + +if (blk_state >SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, block %u, blk_state %#x\n" , + blk_bmap, blk, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + + cache_type =ssdfs_get_cache_type (blk_bmap, blk); + shift =ssdfs_define_bits_shift_in_cache (blk_bmap, cache_type, blk); +if (unlikely (shift <0 )) { +SSDFS_ERR ("fail to define bits shift: " +"cache_type %d, blk %u, err %d\n" , + cache_type, blk, shift); +return shift; + } + + value = blk_state &SSDFS_BLK_STATE_MASK ; + value <<= shift; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (cache_type >=SSDFS_SEARCH_TYPE_MAX ); + +SSDFS_DBG ("value %lx, cache %lx\n" , + value, + blk_bmap->last_search[cache_type].cache); +#endif /* CONFIG_SSDFS_DEBUG */ + + cached_value = &blk_bmap->last_search[cache_type].cache; + *cached_value &= ~((unsigned long )SSDFS_BLK_STATE_MASK << shift); + *cached_value |= value; + +return 0 ; +} + +/* +* ssdfs_save_cache_in_storage() - save cached values in storage +* @blk_bmap: pointer on block bitmap +* +* This function saves cached values in storage. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_save_cache_in_storage (struct ssdfs_block_bmap *blk_bmap) +{ +struct ssdfs_page_vector *array; +void *kaddr; +int max_capacity =SSDFS_BLK_BMAP_FRAGMENTS_CHAIN_MAX ; +int i; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +SSDFS_DBG ("blk_bmap %p\n" , blk_bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i <SSDFS_SEARCH_TYPE_MAX ; i++) { +int page_index = blk_bmap->last_search[i].page_index; +u16 offset = blk_bmap->last_search[i].offset; +unsigned long cache = blk_bmap->last_search[i].cache; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("search_type %d, page_index %d, offset %u\n" , + i, page_index, offset); +SSDFS_DBG ("last_search.cache %lx\n" , cache); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (page_index == max_capacity || offset ==U16_MAX ) +continue ; + +switch (blk_bmap->storage.state) { +case SSDFS_BLOCK_BMAP_STORAGE_PAGE_VEC : + array = &blk_bmap->storage.array; + +if (page_index >=ssdfs_page_vector_capacity (array)) { +SSDFS_ERR ("block bmap's cache is corrupted: " +"page_index %d, offset %u\n" , + page_index, (u32 )offset); +return -EINVAL ; + } + +while (page_index >=ssdfs_page_vector_count (array)) { +struct page *page; + + page =ssdfs_page_vector_allocate (array); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM : +PTR_ERR (page)); +SSDFS_ERR ("unable to allocate page\n" ); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page %p, count %d\n" , + page,page_ref_count (page)); +#endif /* CONFIG_SSDFS_DEBUG */ + } + + err =ssdfs_memcpy_to_page (array->pages[page_index], + offset,PAGE_SIZE , + &cache, +0 ,sizeof (unsigned long ), +sizeof (unsigned long )); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); +return err; + } +break ; + +case SSDFS_BLOCK_BMAP_STORAGE_BUFFER : +if (page_index >0 ) { +SSDFS_ERR ("invalid page_index %d\n" , page_index); +return -ERANGE ; + } + + kaddr = blk_bmap->storage.buf; + err =ssdfs_memcpy (kaddr, offset, blk_bmap->bytes_count, + &cache,0 ,sizeof (unsigned long ), +sizeof (unsigned long )); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); +return err; + } +break ; + +default : +SSDFS_ERR ("unexpected state %#x\n" , + blk_bmap->storage.state); +return -ERANGE ; + } + } + +return 0 ; +} + +/* +* is_cache_invalid() - check that cache is invalid for requested state +* @blk_bmap: pointer on block bitmap +* @blk_state: requested block's state +* +* RETURN: +* [true] - cache doesn't been initialized yet. +* [false] - cache is valid. +*/ +static inline +bool is_cache_invalid (struct ssdfs_block_bmap *blk_bmap,int blk_state) +{ +struct ssdfs_last_bmap_search *last_search; +int cache_type =SSDFS_GET_CACHE_TYPE (blk_state); +int max_capacity =SSDFS_BLK_BMAP_FRAGMENTS_CHAIN_MAX ; + +if (cache_type >=SSDFS_SEARCH_TYPE_MAX ) { +SSDFS_ERR ("invalid cache type %#x, blk_state %#x\n" , + cache_type, blk_state); +return true; + } + + last_search = &blk_bmap->last_search[cache_type]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last_search.cache %lx\n" , last_search->cache); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (last_search->page_index >= max_capacity || + last_search->offset ==U16_MAX ) +return true; + +return false; +} + +/* +* BYTE_CONTAINS_STATE() - check that provided byte contains state +* @value: pointer on analysed byte +* @blk_state: requested block's state +* +* RETURN: +* [true] - @value contains @blk_state. +* [false] - @value hasn't @blk_state. +*/ +static inline +bool BYTE_CONTAINS_STATE (u8 *value,int blk_state) +{ +switch (blk_state) { +case SSDFS_BLK_FREE : +return detect_free_blk[*value]; + +case SSDFS_BLK_PRE_ALLOCATED : +return detect_pre_allocated_blk[*value]; + +case SSDFS_BLK_VALID : +return detect_valid_blk[*value]; + +case SSDFS_BLK_INVALID : +return detect_invalid_blk[*value]; + }; + +return false; +} + +/* +* ssdfs_block_bmap_find_block_in_cache() - find block for state in cache +* @blk_bmap: pointer on block bitmap +* @start: starting block for search +* @max_blk: upper bound for search +* @blk_state: requested block's state +* @found_blk: pointer on found block for requested state [out] +* +* This function tries to find in block block bitmap with @blk_state +* in range [@start, @max_blk). +* +* RETURN: +* [success] - @found_blk contains found block number for @blk_state. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENODATA - requested range [@start, @max_blk) doesn't contain +* any block with @blk_state. +*/ +static +int ssdfs_block_bmap_find_block_in_cache (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 max_blk, +int blk_state,u32 *found_blk) +{ +int cache_type =SSDFS_GET_CACHE_TYPE (blk_state); +u32 items_per_byte =SSDFS_ITEMS_PER_BYTE (SSDFS_BLK_STATE_BITS ); +struct ssdfs_last_bmap_search *last_search; +u32 first_cached_blk; +u32 byte_index; +u8 blks_diff; +size_t bytes_per_long =sizeof (unsigned long ); +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !found_blk); + +if (blk_state >SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (start >= blk_bmap->items_count) { +SSDFS_ERR ("invalid start block %u\n" , start); +return -EINVAL ; + } + +if (start > max_blk) { +SSDFS_ERR ("start %u > max_blk %u\n" , start, max_blk); +return -EINVAL ; + } + +if (!is_block_state_cached (blk_bmap, start)) { +SSDFS_ERR ("cache doesn't contain start %u\n" , start); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, start %u, max_blk %u, " +"state %#x, found_blk %p\n" , + blk_bmap, start, max_blk, blk_state, found_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (cache_type >=SSDFS_SEARCH_TYPE_MAX ) { +SSDFS_ERR ("invalid cache type %#x, blk_state %#x\n" , + cache_type, blk_state); +return -EINVAL ; + } + + *found_blk = max_blk; + last_search = &blk_bmap->last_search[cache_type]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last_search.cache %lx\n" , last_search->cache); +#endif /* CONFIG_SSDFS_DEBUG */ + + first_cached_blk =SSDFS_FIRST_CACHED_BLOCK (last_search); + blks_diff = start - first_cached_blk; + byte_index = blks_diff / items_per_byte; + blks_diff = blks_diff % items_per_byte; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("first_cached_blk %u, start %u, " +"byte_index %u, bytes_per_long %zu\n" , + first_cached_blk, start, + byte_index, bytes_per_long); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (; byte_index < bytes_per_long; byte_index++) { +u8 *value = (u8 *)&last_search->cache + byte_index; +u8 found_off; + + err =FIND_FIRST_ITEM_IN_BYTE (value, blk_state, +SSDFS_BLK_STATE_BITS , +SSDFS_BLK_STATE_MASK , + blks_diff, +BYTE_CONTAINS_STATE , +FIRST_STATE_IN_BYTE , + &found_off); +if (err == -ENODATA ) { + blks_diff =0 ; +continue ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find block in byte: " +"start_off %u, blk_state %#x, err %d\n" , + blks_diff, blk_state, err); +return err; + } + + *found_blk = first_cached_blk; + *found_blk += byte_index * items_per_byte; + *found_blk += found_off; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("block %u has been found for state %#x\n" , + *found_blk, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; + } + +return -ENODATA ; +} + +static inline +int ssdfs_block_bmap_define_start_item (int page_index, +u32 start, +u32 aligned_start, +u32 aligned_end, +u32 *start_byte, +u32 *rest_bytes, +u8 *item_offset) +{ +u32 items_per_byte =SSDFS_ITEMS_PER_BYTE (SSDFS_BLK_STATE_BITS ); +u32 items_per_page =PAGE_SIZE * items_per_byte; +u32 items; +u32 offset; + +if ((page_index * items_per_page) <= aligned_start) + offset = aligned_start % items_per_page; +else + offset = aligned_start; + + *start_byte = offset / items_per_byte; + + items = items_per_page - offset; + +if (aligned_end <= start) { +SSDFS_ERR ("page_index %d, start %u, " +"aligned_start %u, aligned_end %u, " +"start_byte %u, rest_bytes %u, item_offset %u\n" , + page_index, start, + aligned_start, aligned_end, + *start_byte, *rest_bytes, *item_offset); +SSDFS_WARN ("aligned_end %u <= start %u\n" , + aligned_end, start); +return -ERANGE ; + }else + items =min_t (u32, items, aligned_end); + + *rest_bytes = items + items_per_byte -1 ; + *rest_bytes /= items_per_byte; + + *item_offset = (u8)(start - aligned_start); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page_index %d, start %u, " +"aligned_start %u, aligned_end %u, " +"start_byte %u, rest_bytes %u, item_offset %u\n" , + page_index, start, + aligned_start, aligned_end, + *start_byte, *rest_bytes, *item_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_block_bmap_find_block_in_memory_range() - find block in memory range +* @kaddr: pointer on memory range +* @blk_state: requested state of searching block +* @byte_index: index of byte in memory range [in|out] +* @search_bytes: upper bound for search +* @start_off: starting bit offset in byte +* @found_off: pointer on found byte's offset [out] +* +* This function searches a block with requested @blk_state +* into memory range. +* +* RETURN: +* [success] - found byte's offset in @found_off. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENODATA - block with requested state is not found. +*/ +static +int ssdfs_block_bmap_find_block_in_memory_range (void *kaddr, +int blk_state, +u32 *byte_index, +u32 search_bytes, +u8 start_off, +u8 *found_off) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!kaddr || !byte_index || !found_off); + +if (blk_state >SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +SSDFS_DBG ("blk_state %#x, byte_index %u, " +"search_bytes %u, start_off %u\n" , + blk_state, *byte_index, + search_bytes, start_off); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (; *byte_index < search_bytes; ++(*byte_index)) { +u8 *value = (u8 *)kaddr + *byte_index; + + err =FIND_FIRST_ITEM_IN_BYTE (value, blk_state, +SSDFS_BLK_STATE_BITS , +SSDFS_BLK_STATE_MASK , + start_off, +BYTE_CONTAINS_STATE , +FIRST_STATE_IN_BYTE , + found_off); +if (err == -ENODATA ) { + start_off =0 ; +continue ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find block in byte: " +"start_off %u, blk_state %#x, " +"err %d\n" , + start_off, blk_state, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %u has been found for state %#x, " +"err %d\n" , + *found_off, blk_state, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; + } + +return -ENODATA ; +} + +/* +* ssdfs_block_bmap_find_block_in_buffer() - find block in buffer with state +* @blk_bmap: pointer on block bitmap +* @start: start position for search +* @max_blk: upper bound for search +* @blk_state: requested state of searching block +* @found_blk: pointer on found block number [out] +* +* This function searches a block with requested @blk_state +* from @start till @max_blk (not inclusive) into buffer. +* The found block's number is returned via @found_blk. +* +* RETURN: +* [success] - found block number in @found_blk. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENODATA - block with requested state is not found. +*/ +static +int ssdfs_block_bmap_find_block_in_buffer (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 max_blk, +int blk_state,u32 *found_blk) +{ +u32 items_per_byte =SSDFS_ITEMS_PER_BYTE (SSDFS_BLK_STATE_BITS ); +u32 aligned_start, aligned_end; +u32 byte_index, search_bytes =U32_MAX ; +u32 rest_bytes =U32_MAX ; +u8 start_off =U8_MAX ; +void *kaddr; +u8 found_off =U8_MAX ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !found_blk); + +if (blk_state >SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (start >= blk_bmap->items_count) { +SSDFS_ERR ("invalid start block %u\n" , start); +return -EINVAL ; + } + +if (start > max_blk) { +SSDFS_ERR ("start %u > max_blk %u\n" , start, max_blk); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, start %u, max_blk %u, " +"state %#x, found_blk %p\n" , + blk_bmap, start, max_blk, blk_state, found_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_blk = max_blk; + + aligned_start =ALIGNED_START_BLK (start); + aligned_end =ALIGNED_END_BLK (max_blk); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk_state %#x, start %u, max_blk %u, " +"aligned_start %u, aligned_end %u\n" , + blk_state, start, max_blk, + aligned_start, aligned_end); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_block_bmap_define_start_item (0 , + start, + aligned_start, + aligned_end, + &byte_index, + &rest_bytes, + &start_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define start item: " +"blk_state %#x, start %u, max_blk %u, " +"aligned_start %u, aligned_end %u\n" , + blk_state, start, max_blk, + aligned_start, aligned_end); +return err; + } + + kaddr = blk_bmap->storage.buf; + search_bytes = byte_index + rest_bytes; + + err =ssdfs_block_bmap_find_block_in_memory_range (kaddr, blk_state, + &byte_index, + search_bytes, + start_off, + &found_off); +if (err == -ENODATA ) { +/* no item has been found */ +return err; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find block: " +"start_off %u, blk_state %#x, " +"err %d\n" , + start_off, blk_state, err); +return err; + } + + *found_blk = byte_index * items_per_byte; + *found_blk += found_off; + +if (*found_blk >= max_blk) + err = -ENODATA ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("block %u has been found for state %#x, " +"err %d\n" , + *found_blk, blk_state, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_block_bmap_find_block_in_pagevec() - find block in pagevec with state +* @blk_bmap: pointer on block bitmap +* @start: start position for search +* @max_blk: upper bound for search +* @blk_state: requested state of searching block +* @found_blk: pointer on found block number [out] +* +* This function searches a block with requested @blk_state +* from @start till @max_blk (not inclusive) into pagevec. +* The found block's number is returned via @found_blk. +* +* RETURN: +* [success] - found block number in @found_blk. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENODATA - block with requested state is not found. +*/ +static +int ssdfs_block_bmap_find_block_in_pagevec (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 max_blk, +int blk_state,u32 *found_blk) +{ +struct ssdfs_page_vector *array; +u32 items_per_byte =SSDFS_ITEMS_PER_BYTE (SSDFS_BLK_STATE_BITS ); +size_t items_per_page =PAGE_SIZE * items_per_byte; +u32 aligned_start, aligned_end; +struct page *page; +void *kaddr; +int page_index; +u8 found_off =U8_MAX ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !found_blk); + +if (blk_state >SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (start >= blk_bmap->items_count) { +SSDFS_ERR ("invalid start block %u\n" , start); +return -EINVAL ; + } + +if (start > max_blk) { +SSDFS_ERR ("start %u > max_blk %u\n" , start, max_blk); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, start %u, max_blk %u, " +"state %#x, found_blk %p\n" , + blk_bmap, start, max_blk, blk_state, found_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_blk = max_blk; + + array = &blk_bmap->storage.array; + + aligned_start =ALIGNED_START_BLK (start); + aligned_end =ALIGNED_END_BLK (max_blk); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk_state %#x, start %u, max_blk %u, " +"aligned_start %u, aligned_end %u\n" , + blk_state, start, max_blk, + aligned_start, aligned_end); +#endif /* CONFIG_SSDFS_DEBUG */ + + page_index = aligned_start / items_per_page; + +if (page_index >=ssdfs_page_vector_capacity (array)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page_index %d >= capacity %u\n" , + page_index, +ssdfs_page_vector_capacity (array)); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENODATA ; + } + +if (page_index >=ssdfs_page_vector_count (array)) { +if (blk_state !=SSDFS_BLK_FREE ) +return -ENODATA ; + +while (page_index >=ssdfs_page_vector_count (array)) { + page =ssdfs_page_vector_allocate (array); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("unable to allocate page\n" ); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page %p, count %d\n" , + page,page_ref_count (page)); +#endif /* CONFIG_SSDFS_DEBUG */ + } + + *found_blk = page_index * items_per_page; + +if (*found_blk >= max_blk) + err = -ENODATA ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("block %u has been found for state %#x, " +"err %d\n" , + *found_blk, blk_state, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; + } + +for (; page_index <ssdfs_page_vector_capacity (array); page_index++) { +u32 byte_index, search_bytes =U32_MAX ; +u32 rest_bytes =U32_MAX ; +u8 start_off =U8_MAX ; + +if (page_index ==ssdfs_page_vector_count (array)) { +if (blk_state !=SSDFS_BLK_FREE ) +return -ENODATA ; + + page =ssdfs_page_vector_allocate (array); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("unable to allocate page\n" ); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page %p, count %d\n" , + page,page_ref_count (page)); +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_blk = page_index * items_per_page; + +if (*found_blk >= max_blk) + err = -ENODATA ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("block %u has been found for state %#x, " +"err %d\n" , + *found_blk, blk_state, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; + } + + err =ssdfs_block_bmap_define_start_item (page_index, start, + aligned_start, + aligned_end, + &byte_index, + &rest_bytes, + &start_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define start item: " +"blk_state %#x, start %u, max_blk %u, " +"aligned_start %u, aligned_end %u\n" , + blk_state, start, max_blk, + aligned_start, aligned_end); +return err; + } + + search_bytes = byte_index + rest_bytes; + + kaddr =kmap_local_page (array->pages[page_index]); + err =ssdfs_block_bmap_find_block_in_memory_range (kaddr, + blk_state, + &byte_index, + search_bytes, + start_off, + &found_off); +kunmap_local (kaddr); + +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("no item has been found: " +"page_index %d, " +"page_vector_count %u, " +"page_vector_capacity %u\n" , + page_index, +ssdfs_page_vector_count (array), +ssdfs_page_vector_capacity (array)); +#endif /* CONFIG_SSDFS_DEBUG */ +continue ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find block: " +"start_off %u, blk_state %#x, " +"err %d\n" , + start_off, blk_state, err); +return err; + } + + *found_blk = page_index * items_per_page; + *found_blk += byte_index * items_per_byte; + *found_blk += found_off; + +if (*found_blk >= max_blk) + err = -ENODATA ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("block %u has been found for state %#x, " +"err %d\n" , + *found_blk, blk_state, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; + } + +return -ENODATA ; +} + +/* +* ssdfs_block_bmap_find_block_in_storage() - find block in storage with state +* @blk_bmap: pointer on block bitmap +* @start: start position for search +* @max_blk: upper bound for search +* @blk_state: requested state of searching block +* @found_blk: pointer on found block number [out] +* +* This function searches a block with requested @blk_state +* from @start till @max_blk (not inclusive) into storage. +* The found block's number is returned via @found_blk. +* +* RETURN: +* [success] - found block number in @found_blk. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENODATA - block with requested state is not found. +*/ +static +int ssdfs_block_bmap_find_block_in_storage (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 max_blk, +int blk_state,u32 *found_blk) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !found_blk); + +if (blk_state >SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (start >= blk_bmap->items_count) { +SSDFS_ERR ("invalid start block %u\n" , start); +return -EINVAL ; + } + +if (start > max_blk) { +SSDFS_ERR ("start %u > max_blk %u\n" , start, max_blk); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, start %u, max_blk %u, " +"state %#x, found_blk %p\n" , + blk_bmap, start, max_blk, blk_state, found_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (blk_bmap->storage.state) { +case SSDFS_BLOCK_BMAP_STORAGE_PAGE_VEC : + err =ssdfs_block_bmap_find_block_in_pagevec (blk_bmap, + start, + max_blk, + blk_state, + found_blk); +break ; + +case SSDFS_BLOCK_BMAP_STORAGE_BUFFER : + err =ssdfs_block_bmap_find_block_in_buffer (blk_bmap, + start, + max_blk, + blk_state, + found_blk); +break ; + +default : +SSDFS_ERR ("unexpected state %#x\n" , + blk_bmap->storage.state); +return -ERANGE ; + } + +return err; +} + +/* +* ssdfs_block_bmap_find_block() - find block with requested state +* @blk_bmap: pointer on block bitmap +* @start: start position for search +* @max_blk: upper bound for search +* @blk_state: requested state of searching block +* @found_blk: pointer on found block number [out] +* +* This function searches a block with requested @blk_state +* from @start till @max_blk (not inclusive). The found block's +* number is returned via @found_blk. If @blk_state has +* SSDFS_BLK_STATE_MAX then it needs to get block state +* for @start block number, simply. +* +* RETURN: +* [success] - found block number in @found_blk. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENODATA - block with requested state is not found. +*/ +static +int ssdfs_block_bmap_find_block (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 max_blk,int blk_state, +u32 *found_blk) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !found_blk); + +if (blk_state >SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (start >= blk_bmap->items_count) { +SSDFS_ERR ("invalid start block %u\n" , start); +return -EINVAL ; + } + +if (start > max_blk) { +SSDFS_ERR ("start %u > max_blk %u\n" , start, max_blk); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, start %u, max_blk %u, " +"state %#x, found_blk %p\n" , + blk_bmap, start, max_blk, blk_state, found_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (blk_state ==SSDFS_BLK_STATE_MAX ) { + err =ssdfs_cache_block_state (blk_bmap, start, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("unable to cache block %u state: err %d\n" , + start, err); +return err; + } + + *found_blk = start; +return 0 ; + } + + *found_blk = max_blk; + max_blk =min_t (u32, max_blk, blk_bmap->items_count); + +if (is_cache_invalid (blk_bmap, blk_state)) { + err =ssdfs_block_bmap_find_block_in_storage (blk_bmap, + start, max_blk, + blk_state, + found_blk); +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find block in pagevec: " +"start %u, max_blk %u, state %#x\n" , +0 , max_blk, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return err; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find block in pagevec: " +"start %u, max_blk %u, state %#x, err %d\n" , +0 , max_blk, blk_state, err); + goto fail_find; + } + + err =ssdfs_cache_block_state (blk_bmap, *found_blk, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("fail to cache block: " +"found_blk %u, state %#x, err %d\n" , + *found_blk, blk_state, err); + goto fail_find; + } + } + +if (*found_blk >= start && *found_blk < max_blk) + goto end_search; + +if (is_block_state_cached (blk_bmap, start)) { + err =ssdfs_block_bmap_find_block_in_cache (blk_bmap, + start, max_blk, + blk_state, + found_blk); +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find block in cache: " +"start %u, max_blk %u, state %#x\n" , + start, max_blk, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ +/* +* Continue to search in pagevec +*/ + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find block in cache: " +"start %u, max_blk %u, state %#x, err %d\n" , + start, max_blk, blk_state, err); + goto fail_find; + }else if (*found_blk >= start && *found_blk < max_blk) + goto end_search; + } + + err =ssdfs_block_bmap_find_block_in_storage (blk_bmap, start, max_blk, + blk_state, found_blk); +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find block in pagevec: " +"start %u, max_blk %u, state %#x\n" , + start, max_blk, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return err; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find block in pagevec: " +"start %u, max_blk %u, state %#x, err %d\n" , + start, max_blk, blk_state, err); + goto fail_find; + } + +switch (SSDFS_GET_CACHE_TYPE (blk_state)) { +case SSDFS_FREE_BLK_SEARCH : +case SSDFS_OTHER_BLK_SEARCH : + err =ssdfs_cache_block_state (blk_bmap, *found_blk, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("fail to cache block: " +"found_blk %u, state %#x, err %d\n" , + *found_blk, blk_state, err); + goto fail_find; + } +break ; + +default : +/* do nothing */ +break ; + } + +end_search: +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("block %u has been found for state %#x\n" , + *found_blk, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; + +fail_find: +return err; +} + +/* +* BYTE_CONTAIN_DIVERSE_STATES() - check that byte contains diverse state +* @value: pointer on analysed byte +* @blk_state: requested block's state +* +* RETURN: +* [true] - @value contains diverse states. +* [false] - @value contains @blk_state only. +*/ +static inline +bool BYTE_CONTAIN_DIVERSE_STATES (u8 *value,int blk_state) +{ +switch (blk_state) { +case SSDFS_BLK_FREE : +return *value !=SSDFS_FREE_STATES_BYTE ; + +case SSDFS_BLK_PRE_ALLOCATED : +return *value !=SSDFS_PRE_ALLOC_STATES_BYTE ; + +case SSDFS_BLK_VALID : +return *value !=SSDFS_VALID_STATES_BYTE ; + +case SSDFS_BLK_INVALID : +return *value !=SSDFS_INVALID_STATES_BYTE ; + }; + +return false; +} + +/* +* GET_FIRST_DIFF_STATE() - determine first block offset for different state +* @value: pointer on analysed byte +* @blk_state: requested block's state +* @start_off: starting block offset for analysis beginning +* +* This function tries to determine an item with different that @blk_state in +* @value starting from @start_off. +* +* RETURN: +* [success] - found block offset. +* [failure] - BITS_PER_BYTE. +*/ +static inline +u8 GET_FIRST_DIFF_STATE (u8 *value,int blk_state,u8 start_off) +{ +u8 i; +u8 bits_off; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!value); +BUG_ON (start_off >= (BITS_PER_BYTE /SSDFS_BLK_STATE_BITS )); +#endif /* CONFIG_SSDFS_DEBUG */ + + bits_off = start_off *SSDFS_BLK_STATE_BITS ; + +for (i = bits_off; i <BITS_PER_BYTE ; i +=SSDFS_BLK_STATE_BITS ) { +if (((*value >> i) &SSDFS_BLK_STATE_MASK ) != blk_state) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk_state %#x, start_off %u, blk_off %u\n" , + blk_state, start_off, i); +#endif /* CONFIG_SSDFS_DEBUG */ +return i /SSDFS_BLK_STATE_BITS ; + } + } + +return BITS_PER_BYTE ; +} + +/* +* ssdfs_find_state_area_end_in_byte() - find end block for state area in byte +* @value: pointer on analysed byte +* @blk_state: requested block's state +* @start_off: starting block offset for search +* @found_off: pointer on found end block [out] +* +* RETURN: +* [success] - @found_off contains found end offset. +* [failure] - error code: +* +* %-ENODATA - analyzed @value contains @blk_state only. +*/ +static inline +int ssdfs_find_state_area_end_in_byte (u8 *value,int blk_state, +u8 start_off,u8 *found_off) +{ +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("value %p, blk_state %#x, " +"start_off %u, found_off %p\n" , + value, blk_state, start_off, found_off); + +BUG_ON (!value || !found_off); +BUG_ON (start_off >= (BITS_PER_BYTE /SSDFS_BLK_STATE_BITS )); +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_off =BITS_PER_BYTE ; + +if (BYTE_CONTAIN_DIVERSE_STATES (value, blk_state)) { +u8 blk_offset =GET_FIRST_DIFF_STATE (value, blk_state, + start_off); + +if (blk_offset <BITS_PER_BYTE ) { + *found_off = blk_offset; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("block offset %u for *NOT* state %#x\n" , + *found_off, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; + } + } + +return -ENODATA ; +} + +/* +* ssdfs_block_bmap_find_state_area_end_in_memory() - find state area end +* @kaddr: pointer on memory range +* @blk_state: requested state of searching block +* @byte_index: index of byte in memory range [in|out] +* @search_bytes: upper bound for search +* @start_off: starting bit offset in byte +* @found_off: pointer on found end block [out] +* +* This function tries to find @blk_state area end +* in range [@start, @max_blk). +* +* RETURN: +* [success] - found byte's offset in @found_off. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENODATA - nothing has been found. +*/ +static +int ssdfs_block_bmap_find_state_area_end_in_memory (void *kaddr, +int blk_state, +u32 *byte_index, +u32 search_bytes, +u8 start_off, +u8 *found_off) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!kaddr || !byte_index || !found_off); + +if (blk_state >SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + +for (; *byte_index < search_bytes; ++(*byte_index)) { +u8 *value = (u8 *)kaddr + *byte_index; + + err =ssdfs_find_state_area_end_in_byte (value, + blk_state, + start_off, + found_off); +if (err == -ENODATA ) { + start_off =0 ; +continue ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find state area's end: " +"start_off %u, blk_state %#x, " +"err %d\n" , + start_off, blk_state, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset %u has been found for state %#x, " +"err %d\n" , + *found_off, blk_state, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; + } + +return -ENODATA ; +} + +/* +* ssdfs_block_bmap_find_state_area_end_in_buffer() - find state area end +* @bmap: pointer on block bitmap +* @start: start position for search +* @max_blk: upper bound for search +* @blk_state: area state +* @found_end: pointer on found end block [out] +* +* This function tries to find @blk_state area end +* in range [@start, @max_blk). +* +* RETURN: +* [success] - @found_end contains found end block. +* [failure] - items count in block bitmap or error: +* +* %-EINVAL - invalid input value. +*/ +static int +ssdfs_block_bmap_find_state_area_end_in_buffer (struct ssdfs_block_bmap *bmap, +u32 start,u32 max_blk, +int blk_state,u32 *found_end) +{ +u32 aligned_start, aligned_end; +u32 items_per_byte =SSDFS_ITEMS_PER_BYTE (SSDFS_BLK_STATE_BITS ); +u32 byte_index, search_bytes =U32_MAX ; +u32 rest_bytes =U32_MAX ; +u8 start_off =U8_MAX ; +void *kaddr; +u8 found_off =U8_MAX ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start %u, max_blk %u, blk_state %#x\n" , + start, max_blk, blk_state); + +BUG_ON (!bmap || !found_end); + +if (start >= bmap->items_count) { +SSDFS_ERR ("invalid start block %u\n" , start); +return -EINVAL ; + } + +if (start > max_blk) { +SSDFS_ERR ("start %u > max_blk %u\n" , start, max_blk); +return -EINVAL ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_end =U32_MAX ; + + aligned_start =ALIGNED_START_BLK (start); + aligned_end =ALIGNED_END_BLK (max_blk); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk_state %#x, start %u, max_blk %u, " +"aligned_start %u, aligned_end %u\n" , + blk_state, start, max_blk, + aligned_start, aligned_end); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_block_bmap_define_start_item (0 , + start, + aligned_start, + aligned_end, + &byte_index, + &rest_bytes, + &start_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define start item: " +"blk_state %#x, start %u, max_blk %u, " +"aligned_start %u, aligned_end %u\n" , + blk_state, start, max_blk, + aligned_start, aligned_end); +return err; + } + + kaddr = bmap->storage.buf; + search_bytes = byte_index + rest_bytes; + + err =ssdfs_block_bmap_find_state_area_end_in_memory (kaddr, blk_state, + &byte_index, + search_bytes, + start_off, + &found_off); +if (err == -ENODATA ) { + *found_end = max_blk; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("area end %u has been found for state %#x\n" , + *found_end, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ +return 0 ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find state area's end: " +"start_off %u, blk_state %#x, " +"err %d\n" , + start_off, blk_state, err); +return err; + } + + *found_end = byte_index * items_per_byte; + *found_end += found_off; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start %u, aligned_start %u, " +"aligned_end %u, byte_index %u, " +"items_per_byte %u, start_off %u, " +"found_off %u\n" , + start, aligned_start, aligned_end, byte_index, + items_per_byte, start_off, found_off); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (*found_end > max_blk) + *found_end = max_blk; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("area end %u has been found for state %#x\n" , + *found_end, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_block_bmap_find_state_area_end_in_pagevec() - find state area end +* @bmap: pointer on block bitmap +* @start: start position for search +* @max_blk: upper bound for search +* @blk_state: area state +* @found_end: pointer on found end block [out] +* +* This function tries to find @blk_state area end +* in range [@start, @max_blk). +* +* RETURN: +* [success] - @found_end contains found end block. +* [failure] - items count in block bitmap or error: +* +* %-EINVAL - invalid input value. +*/ +static int +ssdfs_block_bmap_find_state_area_end_in_pagevec (struct ssdfs_block_bmap *bmap, +u32 start,u32 max_blk, +int blk_state,u32 *found_end) +{ +struct ssdfs_page_vector *array; +u32 aligned_start, aligned_end; +u32 items_per_byte =SSDFS_ITEMS_PER_BYTE (SSDFS_BLK_STATE_BITS ); +size_t items_per_page =PAGE_SIZE * items_per_byte; +void *kaddr; +int page_index; +u8 found_off =U8_MAX ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start %u, max_blk %u, blk_state %#x\n" , + start, max_blk, blk_state); + +BUG_ON (!bmap || !found_end); + +if (start >= bmap->items_count) { +SSDFS_ERR ("invalid start block %u\n" , start); +return -EINVAL ; + } + +if (start > max_blk) { +SSDFS_ERR ("start %u > max_blk %u\n" , start, max_blk); +return -EINVAL ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_end =U32_MAX ; + + array = &bmap->storage.array; + + aligned_start =ALIGNED_START_BLK (start); + aligned_end =ALIGNED_END_BLK (max_blk); + + page_index = aligned_start / items_per_page; + +if (page_index >=ssdfs_page_vector_count (array)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page_index %d >= count %u\n" , + page_index, +ssdfs_page_vector_count (array)); +#endif /* CONFIG_SSDFS_DEBUG */ + + *found_end = max_blk; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("area end %u has been found for state %#x\n" , + *found_end, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; + } + +for (; page_index <ssdfs_page_vector_count (array); page_index++) { +u32 byte_index, search_bytes =U32_MAX ; +u32 rest_bytes =U32_MAX ; +u8 start_off =U8_MAX ; + + err =ssdfs_block_bmap_define_start_item (page_index, start, + aligned_start, + aligned_end, + &byte_index, + &rest_bytes, + &start_off); +if (unlikely (err)) { +SSDFS_ERR ("fail to define start item: " +"blk_state %#x, start %u, max_blk %u, " +"aligned_start %u, aligned_end %u\n" , + blk_state, start, max_blk, + aligned_start, aligned_end); +return err; + } + + search_bytes = byte_index + rest_bytes; + + kaddr =kmap_local_page (array->pages[page_index]); + err =ssdfs_block_bmap_find_state_area_end_in_memory (kaddr, + blk_state, + &byte_index, + search_bytes, + start_off, + &found_off); +kunmap_local (kaddr); + +if (err == -ENODATA ) { +/* nothing has been found */ +continue ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find state area's end: " +"start_off %u, blk_state %#x, " +"err %d\n" , + start_off, blk_state, err); +return err; + } + + *found_end = page_index * items_per_page; + *found_end += byte_index * items_per_byte; + *found_end += found_off; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start %u, aligned_start %u, " +"aligned_end %u, " +"page_index %d, items_per_page %zu, " +"byte_index %u, " +"items_per_byte %u, start_off %u, " +"found_off %u\n" , + start, aligned_start, aligned_end, + page_index, items_per_page, byte_index, + items_per_byte, start_off, found_off); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (*found_end > max_blk) + *found_end = max_blk; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("area end %u has been found for state %#x\n" , + *found_end, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; + } + + *found_end = max_blk; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("area end %u has been found for state %#x\n" , + *found_end, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_block_bmap_find_state_area_end() - find state area end +* @blk_bmap: pointer on block bitmap +* @start: start position for search +* @max_blk: upper bound for search +* @blk_state: area state +* @found_end: pointer on found end block [out] +* +* This function tries to find @blk_state area end +* in range [@start, @max_blk). +* +* RETURN: +* [success] - @found_end contains found end block. +* [failure] - items count in block bitmap or error: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_block_bmap_find_state_area_end (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 max_blk,int blk_state, +u32 *found_end) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start %u, max_blk %u, blk_state %#x\n" , + start, max_blk, blk_state); + +BUG_ON (!blk_bmap || !found_end); + +if (start >= blk_bmap->items_count) { +SSDFS_ERR ("invalid start block %u\n" , start); +return -EINVAL ; + } + +if (start > max_blk) { +SSDFS_ERR ("start %u > max_blk %u\n" , start, max_blk); +return -EINVAL ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + +if (blk_state ==SSDFS_BLK_FREE ) { + *found_end = blk_bmap->items_count; +return 0 ; + } + +switch (blk_bmap->storage.state) { +case SSDFS_BLOCK_BMAP_STORAGE_PAGE_VEC : + err =ssdfs_block_bmap_find_state_area_end_in_pagevec (blk_bmap, + start, + max_blk, + blk_state, + found_end); +break ; + +case SSDFS_BLOCK_BMAP_STORAGE_BUFFER : + err =ssdfs_block_bmap_find_state_area_end_in_buffer (blk_bmap, + start, + max_blk, + blk_state, + found_end); +break ; + +default : +SSDFS_ERR ("unexpected state %#x\n" , + blk_bmap->storage.state); +return -ERANGE ; + } + +return err; +} + +/* +* range_corrupted() - check that range is corrupted +* @blk_bmap: pointer on block bitmap +* @range: range for check +* +* RETURN: +* [true] - range is invalid +* [false] - range is valid +*/ +static inline +bool range_corrupted (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range) +{ +if (range->len > blk_bmap->items_count) +return true; +if (range->start > (blk_bmap->items_count - range->len)) +return true; +return false; +} + +/* +* is_whole_range_cached() - check that cache contains requested range +* @blk_bmap: pointer on block bitmap +* @range: range for check +* +* RETURN: +* [true] - cache contains the whole range +* [false] - cache doesn't include the whole range +*/ +static +bool is_whole_range_cached (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range) +{ +struct ssdfs_block_bmap_range cached_range; +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); + +if (range_corrupted (blk_bmap, range)) { +SSDFS_ERR ("invalid range (start %u, len %u); items count %zu\n" , + range->start, range->len, + blk_bmap->items_count); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, range (start %u, len %u)\n" , + blk_bmap, range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i <SSDFS_SEARCH_TYPE_MAX ; i++) { +struct ssdfs_last_bmap_search *last_search; +int cmp; + + last_search = &blk_bmap->last_search[i]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("last_search.cache %lx\n" , last_search->cache); +#endif /* CONFIG_SSDFS_DEBUG */ + + cached_range.start =SSDFS_FIRST_CACHED_BLOCK (last_search); + cached_range.len =SSDFS_ITEMS_PER_LONG (SSDFS_BLK_STATE_BITS ); + + cmp =compare_block_bmap_ranges (&cached_range, range); + +if (cmp >=0 ) +return true; +else if (ranges_have_intersection (&cached_range, range)) +return false; + } + +return false; +} + +/* +* ssdfs_set_range_in_cache() - set small range in cache +* @blk_bmap: pointer on block bitmap +* @range: requested range +* @blk_state: state for set +* +* This function sets small range in cache. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_set_range_in_cache (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range, +int blk_state) +{ +u32 blk, index; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +if (blk_state >=SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (range_corrupted (blk_bmap, range)) { +SSDFS_ERR ("invalid range (start %u, len %u); items count %zu\n" , + range->start, range->len, + blk_bmap->items_count); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, range (start %u, len %u), state %#x\n" , + blk_bmap, range->start, range->len, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (index =0 ; index < range->len; index++) { + blk = range->start + index; + err =ssdfs_set_block_state_in_cache (blk_bmap, blk, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("fail to set block %u in cache: err %d\n" , + blk, err); +return err; + } + } + +return 0 ; +} + +/* +* ssdfs_set_uncached_tiny_range() - set tiny uncached range by state +* @blk_bmap: pointer on block bitmap +* @range: range for set +* @blk_state: state for set +* +* This function caches @range, to set @range in cache by @blk_state +* and to save the cache in pagevec. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_set_uncached_tiny_range (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range, +int blk_state) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); + +if (blk_state >=SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (range_corrupted (blk_bmap, range)) { +SSDFS_ERR ("invalid range (start %u, len %u); items count %zu\n" , + range->start, range->len, + blk_bmap->items_count); +return -EINVAL ; + } + +if (range->len >SSDFS_ITEMS_PER_BYTE (SSDFS_BLK_STATE_BITS )) { +SSDFS_ERR ("range (start %u, len %u) is not tiny\n" , + range->start, range->len); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, range (start %u, len %u), state %#x\n" , + blk_bmap, range->start, range->len, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_cache_block_state (blk_bmap, range->start, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("fail to cache block %u: err %d\n" , + range->start, err); +return err; + } + + err =ssdfs_set_range_in_cache (blk_bmap, range, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("fail to set (start %u, len %u): err %d\n" , + range->start, range->len, err); +return err; + } + + err =ssdfs_save_cache_in_storage (blk_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to save cache in pagevec: err %d\n" , + err); +return err; + } + +return 0 ; +} + +/* +* __ssdfs_set_range_in_memory() - set range of bits in memory +* @blk_bmap: pointer on block bitmap +* @page_index: index of memory page +* @byte_offset: offset in bytes from the page's beginning +* @byte_value: byte value for setting +* @init_size: size in bytes for setting +* +* This function sets range of bits in memory. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - internal error. +*/ +static +int __ssdfs_set_range_in_memory (struct ssdfs_block_bmap *blk_bmap, +int page_index,u32 byte_offset, +int byte_value,size_t init_size) +{ +struct ssdfs_page_vector *array; +void *kaddr; +int max_capacity =SSDFS_BLK_BMAP_FRAGMENTS_CHAIN_MAX ; +int i; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +SSDFS_DBG ("blk_bmap %p, page_index %d, byte_offset %u, " +"byte_value %#x, init_size %zu\n" , + blk_bmap, page_index, byte_offset, + byte_value, init_size); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (blk_bmap->storage.state) { +case SSDFS_BLOCK_BMAP_STORAGE_PAGE_VEC : + array = &blk_bmap->storage.array; + +if (page_index >=ssdfs_page_vector_count (array)) { +SSDFS_ERR ("invalid page index %d, pagevec size %d\n" , + page_index, +ssdfs_page_vector_count (array)); +return -EINVAL ; + } + +if (page_index >=ssdfs_page_vector_capacity (array)) { +SSDFS_ERR ("invalid page index %d, pagevec capacity %d\n" , + page_index, +ssdfs_page_vector_capacity (array)); +return -EINVAL ; + } + +while (page_index >=ssdfs_page_vector_count (array)) { +struct page *page; + + page =ssdfs_page_vector_allocate (array); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("unable to allocate page\n" ); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page %p, count %d\n" , + page,page_ref_count (page)); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +#ifdef CONFIG_SSDFS_DEBUG +if ((byte_offset + init_size) >PAGE_SIZE ) { +SSDFS_WARN ("invalid offset: " +"byte_offset %u, init_size %zu\n" , + byte_offset, init_size); +return -ERANGE ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_memset_page (array->pages[page_index], + byte_offset,PAGE_SIZE , + byte_value, init_size); +break ; + +case SSDFS_BLOCK_BMAP_STORAGE_BUFFER : +if (page_index !=0 ) { +SSDFS_ERR ("invalid page index %d\n" , + page_index); +return -EINVAL ; + } + +#ifdef CONFIG_SSDFS_DEBUG +if ((byte_offset + init_size) > blk_bmap->bytes_count) { +SSDFS_WARN ("invalid offset: " +"byte_offset %u, init_size %zu\n" , + byte_offset, init_size); +return -ERANGE ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + + kaddr = blk_bmap->storage.buf; +memset ((u8 *)kaddr + byte_offset, byte_value, init_size); +break ; + +default : +SSDFS_ERR ("unexpected state %#x\n" , + blk_bmap->storage.state); +return -ERANGE ; + } + +for (i =0 ; i <SSDFS_SEARCH_TYPE_MAX ; i++) { + blk_bmap->last_search[i].page_index = max_capacity; + blk_bmap->last_search[i].offset =U16_MAX ; + blk_bmap->last_search[i].cache =0 ; + } + +return 0 ; +} + +/* +* ssdfs_set_range_in_storage() - set range in storage by state +* @blk_bmap: pointer on block bitmap +* @range: range for set +* @blk_state: state for set +* +* This function sets @range in storage by @blk_state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_set_range_in_storage (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range, +int blk_state) +{ +u32 aligned_start, aligned_end; +size_t items_per_byte =SSDFS_ITEMS_PER_BYTE (SSDFS_BLK_STATE_BITS ); +int byte_value; +size_t rest_items, items_per_page; +u32 blk; +int page_index; +u32 item_offset, byte_offset; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); + +if (blk_state >=SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (range_corrupted (blk_bmap, range)) { +SSDFS_ERR ("invalid range (start %u, len %u); items count %zu\n" , + range->start, range->len, + blk_bmap->items_count); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, range (start %u, len %u), state %#x\n" , + blk_bmap, range->start, range->len, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + + aligned_start = range->start + items_per_byte -1 ; + aligned_start >>=SSDFS_BLK_STATE_BITS ; + aligned_start <<=SSDFS_BLK_STATE_BITS ; + + aligned_end = range->start + range->len; + aligned_end >>=SSDFS_BLK_STATE_BITS ; + aligned_end <<=SSDFS_BLK_STATE_BITS ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("aligned_start %u, aligned_end %u\n" , + aligned_start, aligned_end); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (range->start != aligned_start) { +struct ssdfs_block_bmap_range unaligned; + + unaligned.start = range->start; + unaligned.len = aligned_start - range->start; + + err =ssdfs_set_uncached_tiny_range (blk_bmap, &unaligned, + blk_state); +if (unlikely (err)) { +SSDFS_ERR ("fail to set (start %u, len %u): err %d\n" , + unaligned.start, unaligned.len, err); +return err; + } + } + + byte_value =SSDFS_BLK_BMAP_BYTE (blk_state); + items_per_page =PAGE_SIZE * items_per_byte; + rest_items = aligned_end - aligned_start; + page_index = aligned_start / items_per_page; + item_offset = aligned_start % items_per_page; + byte_offset = item_offset / items_per_byte; + + blk = aligned_start; +while (blk < aligned_end) { +size_t iter_items, init_size; + +if (rest_items ==0 ) { +SSDFS_WARN ("unexpected items absence: blk %u\n" , + blk); +break ; + } + +if (byte_offset >=PAGE_SIZE ) { +SSDFS_ERR ("invalid byte offset %u\n" , byte_offset); +return -EINVAL ; + } + + iter_items = items_per_page - item_offset; + iter_items =min_t (size_t, iter_items, rest_items); +if (iter_items < items_per_page) { + init_size = iter_items + items_per_byte -1 ; + init_size /= items_per_byte; + }else + init_size =PAGE_SIZE ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("items_per_page %zu, item_offset %u, " +"rest_items %zu, iter_items %zu, " +"init_size %zu\n" , + items_per_page, item_offset, + rest_items, iter_items, + init_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =__ssdfs_set_range_in_memory (blk_bmap, page_index, + byte_offset, byte_value, + init_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to set range in memory: " +"page_index %d, byte_offset %u, " +"byte_value %#x, init_size %zu, " +"err %d\n" , + page_index, byte_offset, + byte_value, init_size, + err); +return err; + } + + item_offset =0 ; + byte_offset =0 ; + page_index++; + blk += iter_items; + rest_items -= iter_items; + }; + +if (aligned_end != range->start + range->len) { +struct ssdfs_block_bmap_range unaligned; + + unaligned.start = aligned_end; + unaligned.len = (range->start + range->len) - aligned_end; + + err =ssdfs_set_uncached_tiny_range (blk_bmap, &unaligned, + blk_state); +if (unlikely (err)) { +SSDFS_ERR ("fail to set (start %u, len %u): err %d\n" , + unaligned.start, unaligned.len, err); +return err; + } + } + +return 0 ; +} + +/* +* ssdfs_block_bmap_find_range() - find range of block of requested state +* @blk_bmap: pointer on block bitmap +* @start: start block for search +* @len: requested length of range +* @max_blk: upper bound for search +* @blk_state: requested state of blocks in range +* @range: found range [out] +* +* This function searches @range of blocks with requested +* @blk_state. If @blk_state has SSDFS_BLK_STATE_MAX value +* then it needs to get a continuous @range of blocks +* for detecting state of @range is began from @start +* block. +* +* RETURN: +* [success] - @range of found blocks. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_block_bmap_find_range (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 len,u32 max_blk, +int blk_state, +struct ssdfs_block_bmap_range *range) +{ +u32 found_start, found_end; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); + +if (blk_state >SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (start >= blk_bmap->items_count) { +SSDFS_ERR ("invalid start block %u\n" , start); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, start %u, len %u, max_blk %u, " +"state %#x, range %p\n" , + blk_bmap, start, len, max_blk, blk_state, range); +#endif /* CONFIG_SSDFS_DEBUG */ + + range->start =U32_MAX ; + range->len =0 ; + +if (start >= max_blk) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start %u >= max_blk %u\n" , start, max_blk); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENODATA ; + } + + err =ssdfs_block_bmap_find_block (blk_bmap, start, max_blk, + blk_state, &found_start); +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find block: " +"start %u, max_blk %u, state %#x, err %d\n" , + start, max_blk, blk_state, err); +#endif /* CONFIG_SSDFS_DEBUG */ +return err; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find block: " +"start %u, max_blk %u, state %#x, err %d\n" , + start, max_blk, blk_state, err); +return err; + } + +if (found_start >= blk_bmap->items_count) { +SSDFS_ERR ("invalid found start %u, items count %zu\n" , + found_start, blk_bmap->items_count); +return -EINVAL ; + } + + err =ssdfs_block_bmap_find_state_area_end (blk_bmap, found_start, + found_start + len, + blk_state, + &found_end); +if (unlikely (err)) { +SSDFS_ERR ("fail to find block: " +"start %u, max_blk %u, state %#x, err %d\n" , + start, max_blk, blk_state, err); +return err; + } + +if (found_end <= found_start) { +SSDFS_ERR ("invalid found (start %u, end %u), items count %zu\n" , + found_start, found_end, blk_bmap->items_count); +return -EINVAL ; + } + +if (found_end > blk_bmap->items_count) + found_end = blk_bmap->items_count; + + range->start = found_start; + range->len =min_t (u32, len, found_end - found_start); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("found_start %u, found_end %u, len %u, " +"range (start %u, len %u)\n" , + found_start, found_end, len, + range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_block_bmap_set_block_state() - set state of block +* @blk_bmap: pointer on block bitmap +* @blk: segment's block +* @blk_state: state for set +* +* This function sets @blk by @blk_state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_block_bmap_set_block_state (struct ssdfs_block_bmap *blk_bmap, +u32 blk,int blk_state) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +if (blk_state >=SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (blk >= blk_bmap->items_count) { +SSDFS_ERR ("invalid block %u\n" , blk); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, block %u, state %#x\n" , + blk_bmap, blk, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!is_block_state_cached (blk_bmap, blk)) { + err =ssdfs_cache_block_state (blk_bmap, blk, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("unable to cache block %u state: err %d\n" , + blk, err); +return err; + } + } + + err =ssdfs_set_block_state_in_cache (blk_bmap, blk, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("unable to set block %u state in cache: err %d\n" , + blk, err); +return err; + } + + err =ssdfs_save_cache_in_storage (blk_bmap); +if (unlikely (err)) { +SSDFS_ERR ("unable to save the cache in storage: err %d\n" , + err); +return err; + } + +return 0 ; +} + +/* +* ssdfs_block_bmap_set_range() - set state of blocks' range +* @blk_bmap: pointer on block bitmap +* @range: requested range +* @blk_state: state for set +* +* This function sets blocks' @range by @blk_state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input value. +*/ +static +int ssdfs_block_bmap_set_range (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range, +int blk_state) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); + +if (blk_state >=SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return -EINVAL ; + } + +if (range_corrupted (blk_bmap, range)) { +SSDFS_ERR ("invalid range (start %u, len %u); items count %zu\n" , + range->start, range->len, + blk_bmap->items_count); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, range (start %u, len %u), state %#x\n" , + blk_bmap, range->start, range->len, blk_state); + +ssdfs_debug_block_bitmap (blk_bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (range->len ==1 ) { + err =ssdfs_block_bmap_set_block_state (blk_bmap, range->start, + blk_state); +if (err) { +SSDFS_ERR ("fail to set (start %u, len %u) state %#x: " +"err %d\n" , + range->start, range->len, blk_state, err); +return err; + } + }else if (is_whole_range_cached (blk_bmap, range)) { + err =ssdfs_set_range_in_cache (blk_bmap, range, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("unable to set (start %u, len %u) state %#x " +"in cache: err %d\n" , + range->start, range->len, blk_state, err); +return err; + } + + err =ssdfs_save_cache_in_storage (blk_bmap); +if (unlikely (err)) { +SSDFS_ERR ("unable to save the cache in storage: " +"err %d\n" , err); +return err; + } + }else { +u32 next_blk; + + err =ssdfs_set_range_in_storage (blk_bmap, range, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("unable to set (start %u, len %u) state %#x " +"in storage: err %d\n" , + range->start, range->len, blk_state, err); +return err; + } + + next_blk = range->start + range->len; +if (next_blk == blk_bmap->items_count) + next_blk--; + + err =ssdfs_cache_block_state (blk_bmap, next_blk, blk_state); +if (unlikely (err)) { +SSDFS_ERR ("unable to cache block %u state: err %d\n" , + next_blk, err); +return err; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +ssdfs_debug_block_bitmap (blk_bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_block_bmap_test_block() - check state of block +* @blk_bmap: pointer on block bitmap +* @blk: segment's block +* @blk_state: checked state +* +* This function checks that requested @blk has @blk_state. +* +* RETURN: +* [true] - requested @blk has @blk_state +* [false] - requested @blk hasn't @blk_state or it took place +* some failure during checking. +*/ +bool ssdfs_block_bmap_test_block (struct ssdfs_block_bmap *blk_bmap, +u32 blk,int blk_state) +{ +u32 found; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +if (blk_state >=SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return false; + } + +if (blk >= blk_bmap->items_count) { +SSDFS_ERR ("invalid block %u\n" , blk); +return false; + } + +BUG_ON (!mutex_is_locked (&blk_bmap->lock)); + +SSDFS_DBG ("blk_bmap %p, block %u, state %#x\n" , + blk_bmap, blk, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +BUG_ON (!is_block_bmap_initialized (blk_bmap)); + + err =ssdfs_block_bmap_find_block (blk_bmap, blk, blk +1 , blk_state, + &found); +if (err) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find block %u, state %#x, err %d\n" , + blk, blk_state, err); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + +return (found != blk) ? false : true; +} + +/* +* ssdfs_block_bmap_test_range() - check state of blocks' range +* @blk_bmap: pointer on block bitmap +* @range: segment's blocks' range +* @blk_state: checked state +* +* This function checks that all blocks in requested @range have +* @blk_state. +* +* RETURN: +* [true] - all blocks in requested @range have @blk_state +* [false] - requested @range contains blocks with various states or +* it took place some failure during checking. +*/ +bool ssdfs_block_bmap_test_range (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range, +int blk_state) +{ +struct ssdfs_block_bmap_range found; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); + +if (blk_state >=SSDFS_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid block state %#x\n" , blk_state); +return false; + } + +if (range_corrupted (blk_bmap, range)) { +SSDFS_ERR ("invalid range (start %u, len %u); items count %zu\n" , + range->start, range->len, + blk_bmap->items_count); +return false; + } + +BUG_ON (!mutex_is_locked (&blk_bmap->lock)); + +SSDFS_DBG ("blk_bmap %p, range (start %u, len %u), state %#x\n" , + blk_bmap, range->start, range->len, blk_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +BUG_ON (!is_block_bmap_initialized (blk_bmap)); + + err =ssdfs_block_bmap_find_range (blk_bmap, range->start, range->len, + range->start + range->len, + blk_state, &found); +if (unlikely (err)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find range: err %d\n" , err); +#endif /* CONFIG_SSDFS_DEBUG */ +return false; + } + +if (compare_block_bmap_ranges (&found, range) ==0 ) +return true; + +return false; +} + +/* +* ssdfs_get_block_state() - detect state of block +* @blk_bmap: pointer on block bitmap +* @blk: segment's block +* +* This function retrieve state of @blk from block bitmap. +* +* RETURN: +* [success] - state of block +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENODATA - requsted @blk hasn't been found. +* %-ENOENT - block bitmap doesn't initialized. +*/ +int ssdfs_get_block_state (struct ssdfs_block_bmap *blk_bmap,u32 blk) +{ +u32 found; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); + +if (blk >= blk_bmap->items_count) { +SSDFS_ERR ("invalid block %u\n" , blk); +return -EINVAL ; + } + +if (!mutex_is_locked (&blk_bmap->lock)) { +SSDFS_WARN ("block bitmap mutex should be locked\n" ); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, block %u\n" , blk_bmap, blk); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!is_block_bmap_initialized (blk_bmap)) { +SSDFS_WARN ("block bitmap hasn't been initialized\n" ); +return -ENOENT ; + } + + err =ssdfs_block_bmap_find_block (blk_bmap, blk, blk +1 , +SSDFS_BLK_STATE_MAX , + &found); +if (err) { +SSDFS_ERR ("fail to find block %u, err %d\n" , + blk, err); +return err; + } + +if (found != blk) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("found (%u) != blk (%u)\n" , found, blk); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENODATA ; + } + +return ssdfs_get_block_state_from_cache (blk_bmap, blk); +} + +/* +* ssdfs_get_range_state() - detect state of blocks' range +* @blk_bmap: pointer on block bitmap +* @range: pointer on blocks' range +* +* This function retrieve state of @range from block bitmap. +* +* RETURN: +* [success] - state of blocks' range +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-EOPNOTSUPP - requsted @range contains various state of blocks. +* %-ENOENT - block bitmap doesn't initialized. +*/ +int ssdfs_get_range_state (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range) +{ +struct ssdfs_block_bmap_range found; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); + +if (range_corrupted (blk_bmap, range)) { +SSDFS_ERR ("invalid range: start %u, len %u; items count %zu\n" , + range->start, range->len, + blk_bmap->items_count); +return -EINVAL ; + } + +if (!mutex_is_locked (&blk_bmap->lock)) { +SSDFS_WARN ("block bitmap mutex should be locked\n" ); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, range (start %u, len %u)\n" , + blk_bmap, range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!is_block_bmap_initialized (blk_bmap)) { +SSDFS_WARN ("block bitmap hasn't been initialized\n" ); +return -ENOENT ; + } + + err =ssdfs_block_bmap_find_range (blk_bmap, range->start, range->len, + range->start + range->len, +SSDFS_BLK_STATE_MAX , &found); +if (unlikely (err)) { +SSDFS_ERR ("fail to find range: err %d\n" , err); +return err; + } + +if (compare_block_bmap_ranges (&found, range) !=0 ) { +SSDFS_ERR ("range contains various state of blocks\n" ); +return -EOPNOTSUPP ; + } + + err =ssdfs_cache_block_state (blk_bmap, range->start, +SSDFS_BLK_STATE_MAX ); +if (unlikely (err)) { +SSDFS_ERR ("fail to cache block %u: err %d\n" , + range->start, err); +return err; + } +
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.
RFC PATCH 12/76 ssdfs: block bitmap modification operations implementation
In reply to Viacheslav DubeykoThis patch implements block bitmap's modification operations:pre_allocate - pre_allocate logical block or range of blocks allocate - allocate logical block or range of blocks invalidate - invalidate logical block or range of blocks collect_garbage - get contigous range of blocks in state clean - convert the whole block bitmap into clean state
--- fs/ssdfs/block_bitmap.c | 703 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 703 insertions(+)
fs/ssdfs/block_bitmap.c +703 -0
@@ -4608,3 +4608,706 @@ int ssdfs_block_bmap_get_invalid_pages(struct ssdfs_block_bmap *blk_bmap)return blk_bmap->invalid_blks; } + +/* +* ssdfs_block_bmap_pre_allocate() - pre-allocate segment's range of blocks +* @blk_bmap: pointer on block bitmap +* @start: starting block for search +* @len: pointer on variable with requested length of range +* @range: pointer on blocks' range [in | out] +* +* This function tries to find contiguous range of free blocks and +* to set the found range in pre-allocated state. +* +* If pointer @len is NULL then it needs: +* (1) check that requested range contains free blocks only; +* (2) set the requested range of blocks in pre-allocated state. +* +* Otherwise, if pointer @len != NULL then it needs: +* (1) find the range of free blocks of requested length or lesser; +* (2) set the found range of blocks in pre-allocated state. +* +* RETURN: +* [success] - @range of pre-allocated blocks. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENOENT - block bitmap doesn't initialized. +* %-ENOSPC - block bitmap hasn't free blocks. +*/ +int ssdfs_block_bmap_pre_allocate (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 *len, +struct ssdfs_block_bmap_range *range) +{ +int free_pages; +u32 used_blks =0 ; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); +if (!mutex_is_locked (&blk_bmap->lock)) { +SSDFS_WARN ("block bitmap mutex should be locked\n" ); +return -EINVAL ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("start %u, len %p\n" , + start, len); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +if (len) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk_bmap %p, start %u, len %u\n" , + blk_bmap, start, *len); +#endif /* CONFIG_SSDFS_DEBUG */ + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk_bmap %p, range (start %u, len %u)\n" , + blk_bmap, range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (range_corrupted (blk_bmap, range)) { +SSDFS_ERR ("invalid range: start %u, len %u; " +"items count %zu\n" , + range->start, range->len, + blk_bmap->items_count); +return -EINVAL ; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("items_count %zu, used_blks %u, " +"metadata_items %u, invalid_blks %u\n" , + blk_bmap->items_count, + blk_bmap->used_blks, + blk_bmap->metadata_items, + blk_bmap->invalid_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!is_block_bmap_initialized (blk_bmap)) { +SSDFS_WARN ("block bitmap hasn't been initialized\n" ); +return -ENOENT ; + } + + err =ssdfs_block_bmap_get_free_pages (blk_bmap); +if (unlikely (err <0 )) { +SSDFS_ERR ("fail to get free pages: err %d\n" , err); +return err; + }else { + free_pages = err; + err =0 ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_pages %d\n" , free_pages); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (len) { +u32 max_blk = blk_bmap->items_count - blk_bmap->metadata_items; +u32 start_blk =0 ; + +if (free_pages < *len) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to pre_allocate: " +"free_pages %d, count %u\n" , + free_pages, *len); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOSPC ; + } + +if (!is_cache_invalid (blk_bmap,SSDFS_BLK_FREE )) { + err =ssdfs_define_last_free_page (blk_bmap, &start_blk); +if (err) { +SSDFS_ERR ("fail to define start block: " +"err %d\n" , + err); +return err; + } + } + + start_blk =max_t (u32, start_blk, start); + + err =ssdfs_block_bmap_find_range (blk_bmap, start_blk, *len, + max_blk, +SSDFS_BLK_FREE , range); +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find free blocks: " +"start_blk %u, max_blk %u, len %u\n" , + start_blk, max_blk, *len); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOSPC ; + }else if (err) { +SSDFS_ERR ("fail to find free blocks: err %d\n" , err); +return err; + } + }else { +if (free_pages < range->len) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to pre_allocate: " +"free_pages %d, count %u\n" , + free_pages, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOSPC ; + } + +if (!is_range_free (blk_bmap, range)) { +SSDFS_ERR ("range (start %u, len %u) is not free\n" , + range->start, range->len); +return -EINVAL ; + } + } + + used_blks = (u32 )blk_bmap->used_blks + range->len; + +if (used_blks > blk_bmap->items_count) { +SSDFS_ERR ("invalid used blocks count: " +"used_blks %u, items_count %zu\n" , + used_blks, + blk_bmap->items_count); +return -ERANGE ; + } + + err =ssdfs_block_bmap_set_range (blk_bmap, range, +SSDFS_BLK_PRE_ALLOCATED ); +if (unlikely (err)) { +SSDFS_ERR ("fail to set range (start %u, len %u): err %d\n" , + range->start, range->len, err); +return err; + } + + blk_bmap->used_blks += range->len; + +set_block_bmap_dirty (blk_bmap); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("range (start %u, len %u) has been pre-allocated\n" , + range->start, range->len); +#else +SSDFS_DBG ("range (start %u, len %u) has been pre-allocated\n" , + range->start, range->len); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +return 0 ; +} + +/* +* ssdfs_block_bmap_allocate() - allocate segment's range of blocks +* @blk_bmap: pointer on block bitmap +* @start: starting block for search +* @len: pointer on variable with requested length of range +* @range: pointer on blocks' range [in | out] +* +* This function tries to find contiguous range of free +* (or pre-allocated) blocks and to set the found range in +* valid state. +* +* If pointer @len is NULL then it needs: +* (1) check that requested range contains free or pre-allocated blocks; +* (2) set the requested range of blocks in valid state. +* +* Otherwise, if pointer @len != NULL then it needs: +* (1) find the range of free blocks of requested length or lesser; +* (2) set the found range of blocks in valid state. +* +* RETURN: +* [success] - @range of valid blocks. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENOENT - block bitmap doesn't initialized. +* %-ENOSPC - block bitmap hasn't free blocks. +*/ +int ssdfs_block_bmap_allocate (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 *len, +struct ssdfs_block_bmap_range *range) +{ +int state =SSDFS_BLK_FREE ; +int free_pages; +u32 used_blks =0 ; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); +if (!mutex_is_locked (&blk_bmap->lock)) { +SSDFS_WARN ("block bitmap mutex should be locked\n" ); +return -EINVAL ; + } +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("start %u, len %p\n" , + start, len); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +if (len) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk_bmap %p, start %u, len %u\n" , + blk_bmap, start, *len); +#endif /* CONFIG_SSDFS_DEBUG */ + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("blk_bmap %p, range (start %u, len %u)\n" , + blk_bmap, range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (range_corrupted (blk_bmap, range)) { +SSDFS_ERR ("invalid range: start %u, len %u; " +"items count %zu\n" , + range->start, range->len, + blk_bmap->items_count); +return -EINVAL ; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("items_count %zu, used_blks %u, " +"metadata_items %u\n" , + blk_bmap->items_count, + blk_bmap->used_blks, + blk_bmap->metadata_items); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!is_block_bmap_initialized (blk_bmap)) { +SSDFS_WARN ("block bitmap hasn't been initialized\n" ); +return -ENOENT ; + } + + err =ssdfs_block_bmap_get_free_pages (blk_bmap); +if (unlikely (err <0 )) { +SSDFS_ERR ("fail to get free pages: err %d\n" , err); +return err; + }else { + free_pages = err; + err =0 ; + } + +if (len) { +u32 max_blk = blk_bmap->items_count - blk_bmap->metadata_items; +u32 start_blk =0 ; + +if (free_pages < *len) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to allocate: " +"free_pages %d, count %u\n" , + free_pages, *len); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOSPC ; + } + +if (!is_cache_invalid (blk_bmap,SSDFS_BLK_FREE )) { + err =ssdfs_define_last_free_page (blk_bmap, &start_blk); +if (err) { +SSDFS_ERR ("fail to define start block: " +"err %d\n" , + err); +return err; + } + } + + start_blk =max_t (u32, start_blk, start); + + err =ssdfs_block_bmap_find_range (blk_bmap, start_blk, *len, + max_blk,SSDFS_BLK_FREE , + range); +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to find free blocks: " +"start_blk %u, max_blk %u, len %u\n" , + start_blk, max_blk, *len); +#endif /* CONFIG_SSDFS_DEBUG */ +return -ENOSPC ; + }else if (err) { +SSDFS_ERR ("fail to find free blocks: err %d\n" , err); +return err; + } + }else { + state =ssdfs_get_range_state (blk_bmap, range); + +if (state <0 ) { +SSDFS_ERR ("fail to get range " +"(start %u, len %u) state: err %d\n" , + range->start, range->len, state); +return state; + } + +if (state !=SSDFS_BLK_FREE && + state !=SSDFS_BLK_PRE_ALLOCATED ) { +SSDFS_ERR ("range (start %u, len %u), state %#x, " +"can't be allocated\n" , + range->start, range->len, state); +return -EINVAL ; + } + } + + err =ssdfs_block_bmap_set_range (blk_bmap, range, +SSDFS_BLK_VALID ); +if (unlikely (err)) { +SSDFS_ERR ("fail to set range (start %u, len %u): " +"err %d\n" , + range->start, range->len, err); +return err; + } + +if (state ==SSDFS_BLK_FREE ) { + used_blks = (u32 )blk_bmap->used_blks + range->len; + +if (used_blks > blk_bmap->items_count) { +SSDFS_ERR ("invalid used blocks count: " +"used_blks %u, items_count %zu\n" , + used_blks, + blk_bmap->items_count); +return -ERANGE ; + } + + blk_bmap->used_blks += range->len; + } + +set_block_bmap_dirty (blk_bmap); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("range (start %u, len %u) has been allocated\n" , + range->start, range->len); +#else +SSDFS_DBG ("range (start %u, len %u) has been allocated\n" , + range->start, range->len); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +return 0 ; +} + +/* +* ssdfs_block_bmap_invalidate() - invalidate segment's range of blocks +* @blk_bmap: pointer on block bitmap +* @len: pointer on variable with requested length of range +* @range: pointer on blocks' range [in | out] +* +* This function tries to set the requested range of blocks in +* invalid state. At first, it checks that requested range contains +* valid blocks only. And, then, it sets the requested range of blocks +* in invalid state. +* +* RETURN: +* [success] - @range of invalid blocks. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENOENT - block bitmap doesn't initialized. +*/ +int ssdfs_block_bmap_invalidate (struct ssdfs_block_bmap *blk_bmap, +struct ssdfs_block_bmap_range *range) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); +if (!mutex_is_locked (&blk_bmap->lock)) { +SSDFS_WARN ("block bitmap mutex should be locked\n" ); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, range (start %u, len %u)\n" , + blk_bmap, range->start, range->len); +SSDFS_DBG ("items_count %zu, used_blks %u, " +"metadata_items %u, invalid_blks %u\n" , + blk_bmap->items_count, + blk_bmap->used_blks, + blk_bmap->metadata_items, + blk_bmap->invalid_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("range (start %u, len %u)\n" , + range->start, range->len); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +if (!is_block_bmap_initialized (blk_bmap)) { +SSDFS_WARN ("block bitmap hasn't been initialized\n" ); +return -ENOENT ; + } + +#ifdef CONFIG_SSDFS_DEBUG +ssdfs_debug_block_bitmap (blk_bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (range_corrupted (blk_bmap, range)) { +SSDFS_ERR ("invalid range (start %u, len %u); items count %zu\n" , + range->start, range->len, blk_bmap->items_count); +return -EINVAL ; + } + +if (!is_range_valid (blk_bmap, range) && + !is_range_pre_allocated (blk_bmap, range)) { +SSDFS_ERR ("range (start %u, len %u) contains not valid blocks\n" , + range->start, range->len); +return -EINVAL ; + } + + err =ssdfs_block_bmap_set_range (blk_bmap, range, +SSDFS_BLK_INVALID ); +if (unlikely (err)) { +SSDFS_ERR ("fail to set range (start %u, len %u): err %d\n" , + range->start, range->len, err); +return err; + } + + blk_bmap->invalid_blks += range->len; + +if (range->len > blk_bmap->used_blks) { +SSDFS_ERR ("invalid range len: " +"range_len %u, used_blks %u, items_count %zu\n" , + range->len, + blk_bmap->used_blks, + blk_bmap->items_count); +return -ERANGE ; + }else + blk_bmap->used_blks -= range->len; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("items_count %zu, used_blks %u, " +"metadata_items %u, invalid_blks %u\n" , + blk_bmap->items_count, + blk_bmap->used_blks, + blk_bmap->metadata_items, + blk_bmap->invalid_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + +set_block_bmap_dirty (blk_bmap); + +#ifdef CONFIG_SSDFS_DEBUG +ssdfs_debug_block_bitmap (blk_bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("range (start %u, len %u) has been invalidated\n" , + range->start, range->len); +#else +SSDFS_DBG ("range (start %u, len %u) has been invalidated\n" , + range->start, range->len); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +return 0 ; +} + +/* +* ssdfs_block_bmap_collect_garbage() - find range of valid blocks for GC +* @blk_bmap: pointer on block bitmap +* @start: starting position for search +* @max_len: maximum requested length of valid blocks' range +* @blk_state: requested block state (pre-allocated or valid) +* @range: pointer on blocks' range [out] +* +* This function tries to find range of valid blocks for GC. +* The length of requested range is limited by @max_len. +* +* RETURN: +* [success] - @range of invalid blocks. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENOENT - block bitmap doesn't initialized. +* %-ENODATA - requested range hasn't valid blocks. +*/ +int ssdfs_block_bmap_collect_garbage (struct ssdfs_block_bmap *blk_bmap, +u32 start,u32 max_len, +int blk_state, +struct ssdfs_block_bmap_range *range) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap || !range); +if (!mutex_is_locked (&blk_bmap->lock)) { +SSDFS_WARN ("block bitmap mutex should be locked\n" ); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p, start %u, max_len %u\n" , + blk_bmap, start, max_len); +SSDFS_DBG ("items_count %zu, used_blks %u, " +"metadata_items %u, invalid_blks %u\n" , + blk_bmap->items_count, + blk_bmap->used_blks, + blk_bmap->metadata_items, + blk_bmap->invalid_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("start %u, max_len %u, blk_state %#x\n" , + start, max_len, blk_state); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +if (!is_block_bmap_initialized (blk_bmap)) { +SSDFS_WARN ("block bitmap hasn't been initialized\n" ); +return -ENOENT ; + } + +#ifdef CONFIG_SSDFS_DEBUG +ssdfs_debug_block_bitmap (blk_bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (blk_state) { +case SSDFS_BLK_PRE_ALLOCATED : +case SSDFS_BLK_VALID : +/* valid block state */ +break ; + +default : +SSDFS_ERR ("invalid block state: %#x\n" , + blk_state); +return -EINVAL ; + }; + + err =ssdfs_block_bmap_find_range (blk_bmap, start, max_len, max_len, + blk_state, range); +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("range (start %u, len %u) hasn't valid blocks\n" , + start, max_len); +#endif /* CONFIG_SSDFS_DEBUG */ +return err; + }else if (err) { +SSDFS_ERR ("fail to find valid blocks: err %d\n" , err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("range (start %u, len %u) has been collected as garbage\n" , + range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("range (start %u, len %u) has been collected as garbage\n" , + range->start, range->len); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +return 0 ; +} + +/* +* ssdfs_block_bmap_clean() - set all blocks as free/clean +* @blk_bmap: pointer on block bitmap +* +* This function tries to clean the whole bitmap. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ENOENT - block bitmap doesn't initialized. +*/ +int ssdfs_block_bmap_clean (struct ssdfs_block_bmap *blk_bmap) +{ +struct ssdfs_block_bmap_range range; +int max_capacity =SSDFS_BLK_BMAP_FRAGMENTS_CHAIN_MAX ; +int i; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!blk_bmap); +if (!mutex_is_locked (&blk_bmap->lock)) { +SSDFS_WARN ("block bitmap mutex should be locked\n" ); +return -EINVAL ; + } + +SSDFS_DBG ("blk_bmap %p\n" , blk_bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!is_block_bmap_initialized (blk_bmap)) { +SSDFS_WARN ("block bitmap hasn't been initialized\n" ); +return -ENOENT ; + } + + blk_bmap->metadata_items =0 ; + blk_bmap->used_blks =0 ; + blk_bmap->invalid_blks =0 ; + +for (i =0 ; i <SSDFS_SEARCH_TYPE_MAX ; i++) { + blk_bmap->last_search[i].page_index = max_capacity; + blk_bmap->last_search[i].offset =U16_MAX ; + blk_bmap->last_search[i].cache =0 ; + } + + range.start =0 ; + range.len = blk_bmap->items_count; + + err =ssdfs_set_range_in_storage (blk_bmap, &range,SSDFS_BLK_FREE ); +if (unlikely (err)) { +SSDFS_ERR ("fail to clean block bmap: " +"range (start %u, len %u), " +"err %d\n" , + range.start, range.len, err); +return err; + } + + err =ssdfs_cache_block_state (blk_bmap,0 ,SSDFS_BLK_FREE ); +if (unlikely (err)) { +SSDFS_ERR ("fail to cache last free page: err %d\n" , + err); +return err; + } + +return 0 ; +} + +#ifdef CONFIG_SSDFS_DEBUG +static +void ssdfs_debug_block_bitmap (struct ssdfs_block_bmap *bmap) +{ +struct ssdfs_page_vector *array; +struct page *page; +void *kaddr; +int i; + +BUG_ON (!bmap); + +SSDFS_DBG ("BLOCK BITMAP: bytes_count %zu, items_count %zu, " +"metadata_items %u, used_blks %u, invalid_blks %u, " +"flags %#x\n" , + bmap->bytes_count, + bmap->items_count, + bmap->metadata_items, + bmap->used_blks, + bmap->invalid_blks, +atomic_read (&bmap->flags)); + +SSDFS_DBG ("LAST SEARCH:\n" ); +for (i =0 ; i <SSDFS_SEARCH_TYPE_MAX ; i++) { +SSDFS_DBG ("TYPE %d: page_index %d, offset %u, cache %lx\n" , + i, + bmap->last_search[i].page_index, + bmap->last_search[i].offset, + bmap->last_search[i].cache); + } + +switch (bmap->storage.state) { +case SSDFS_BLOCK_BMAP_STORAGE_PAGE_VEC : + array = &bmap->storage.array; + +for (i =0 ; i <ssdfs_page_vector_count (array); i++) { + page = array->pages[i]; + +if (!page) { +SSDFS_WARN ("page %d is NULL\n" , i); +continue ; + } + + kaddr =kmap_local_page (page); +SSDFS_DBG ("BMAP CONTENT: page %d\n" , i); +print_hex_dump_bytes ("" ,DUMP_PREFIX_OFFSET , + kaddr,PAGE_SIZE ); +kunmap_local (kaddr); + } +break ; + +case SSDFS_BLOCK_BMAP_STORAGE_BUFFER : +SSDFS_DBG ("BMAP CONTENT:\n" ); +print_hex_dump_bytes ("" ,DUMP_PREFIX_OFFSET , + bmap->storage.buf, + bmap->bytes_count); +break ; + } +} +#endif /* CONFIG_SSDFS_DEBUG */
Signature · 1 line
2.34.1
RFC PATCH 13/76 ssdfs: introduce PEB block bitmap
In reply to Viacheslav DubeykoSSDFS implements a migration scheme. Migration scheme is a fundamental technique of GC overhead management. The key responsibility of the migration scheme is to guarantee the presence of data in the same segment for any update operations. Generally speaking, the migration scheme’s model is implemented on the basis of association an exhausted "Physical" Erase Block (PEB) with a clean one. The goal such association of two PEBs is to implement the gradual migration of data by means of the update operations in the initial (exhausted) PEB. As a result, the old, exhausted PEB becomes invalidated after complete data migration and it will be possible to apply the erase operation to convert it in the clean state. Moreover, the destination PEB in the association changes the initial PEB for some index in the segment and, finally, it becomes the only PEB for this position. Namely such technique implements the concept of logical extent with the goal to decrease the write amplification issue and to manage the GC overhead. Because the logical extent concept excludes the necessity to update metadata is tracking the position of user data on the file system’s volume. Generally speaking, the migration scheme is capable to decrease the GC activity significantly by means of excluding the necessity to update metadata and by means of self-migration of data between of PEBs is triggered by regular update operations.
To implement the migration scheme concept, SSDFS introduces
PEB container that includes source and destination erase blocks. As a result, PEB block bitmap object represents the same aggregation for source PEB's block bitmap and destination PEB's block bitmap. PEB block bitmap implements API:
(1) create - create PEB block bitmap
(2) destroy - destroy PEB block bitmap
(3) init - initialize PEB block bitmap by metadata from a log
(4) get_free_pages - get free pages in aggregation of block bitmaps
(5) get_used_pages - get used pages in aggregation of block bitmaps
(6) get_invalid_pages - get invalid pages in aggregation of block bitmaps
(7) pre_allocate - pre_allocate page/range in aggregation of block bitmaps
(8) allocate - allocate page/range in aggregation of block bitmaps
(9) invalidate - invalidate page/range in aggregation of block bitmaps
(10) update_range - change the state of range in aggregation of block bitmaps
(11) collect_garbage - find contiguous range for requested state
(12) start_migration - prepare PEB's environment for migration
(13) migrate - move range from source block bitmap into destination one
(14) finish_migration - clean source block bitmap and swap block bitmaps
--- fs/ssdfs/peb_block_bitmap.c | 1540 +++++++++++++++++++++++++++++++++++ fs/ssdfs/peb_block_bitmap.h | 165 ++++ 2 files changed, 1705 insertions(+) create mode 100644 fs/ssdfs/peb_block_bitmap.c create mode 100644 fs/ssdfs/peb_block_bitmap.h
fs/ssdfs/peb_block_bitmap.c new file +1540 -0
@@ -0,0 +1,1540 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/peb_block_bitmap.c - PEB's block bitmap implementation. +* +* 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/ +* +* (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 +*/ + +#include <linux/pagemap.h> +#include <linux/slab.h> +#include <linux/pagevec.h> + +#include "peb_mapping_queue.h" +#include "peb_mapping_table_cache.h" +#include "ssdfs.h" +#include "page_vector.h" +#include "peb_block_bitmap.h" +#include "segment_block_bitmap.h" +#include "offset_translation_table.h" +#include "page_array.h" +#include "peb_container.h" +#include "segment_bitmap.h" +#include "segment.h" + +#define SSDFS_PEB_BLK_BMAP_STATE_FNS (value, name) \ +static inline \ +bool is_peb_block_bmap_##name(struct ssdfs_peb_blk_bmap *bmap) \ +{ \ + return atomic_read(&bmap->state) == SSDFS_PEB_BLK_BMAP_##value; \ +} \ +static inline \ +void set_peb_block_bmap_##name(struct ssdfs_peb_blk_bmap *bmap) \ +{ \ + atomic_set(&bmap->state, SSDFS_PEB_BLK_BMAP_##value); \ +} \ + +/* +* is_peb_block_bmap_created() +* set_peb_block_bmap_created() +*/ +SSDFS_PEB_BLK_BMAP_STATE_FNS (CREATED , created) + +/* +* is_peb_block_bmap_initialized() +* set_peb_block_bmap_initialized() + */ +SSDFS_PEB_BLK_BMAP_STATE_FNS(INITIALIZED, initialized) + +bool ssdfs_peb_blk_bmap_initialized (struct ssdfs_peb_blk_bmap *ptr) +{ +return is_peb_block_bmap_initialized (ptr); +} + +/* +* ssdfs_peb_blk_bmap_create() - construct PEB's block bitmap +* @parent: parent segment's block bitmap +* @peb_index: PEB's index in segment's array +* @items_count: count of described items +* @flag: define necessity to allocate memory +* @init_flag: definition of block bitmap's creation state +* @init_state: block state is used during initialization +* +* This function tries to create the source and destination block +* bitmap objects. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_create (struct ssdfs_segment_blk_bmap *parent, +u16 peb_index,u32 items_count, +int init_flag,int init_state) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_segment_info *si; +struct ssdfs_peb_blk_bmap *bmap; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!parent || !parent->peb); +BUG_ON (peb_index >= parent->pebs_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("parent %p, peb_index %u, " +"items_count %u, init_flag %#x, init_state %#x\n" , + parent, peb_index, + items_count, init_flag, init_state); +#else +SSDFS_DBG ("parent %p, peb_index %u, " +"items_count %u, init_flag %#x, init_state %#x\n" , + parent, peb_index, + items_count, init_flag, init_state); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + + fsi = parent->parent_si->fsi; + si = parent->parent_si; + bmap = &parent->peb[peb_index]; +atomic_set (&bmap->state,SSDFS_PEB_BLK_BMAP_STATE_UNKNOWN ); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("seg_id %llu, peb_index %u\n" , + si->seg_id, bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (items_count > parent->pages_per_peb) { +SSDFS_ERR ("items_count %u > pages_per_peb %u\n" , + items_count, parent->pages_per_peb); +return -ERANGE ; + } + + bmap->parent = parent; + bmap->peb_index = peb_index; + bmap->pages_per_peb = parent->pages_per_peb; + +init_rwsem (&bmap->modification_lock); +atomic_set (&bmap->peb_valid_blks,0 ); +atomic_set (&bmap->peb_invalid_blks,0 ); +atomic_set (&bmap->peb_free_blks,0 ); + +atomic_set (&bmap->buffers_state,SSDFS_PEB_BMAP_BUFFERS_EMPTY ); +init_rwsem (&bmap->lock); + bmap->init_cno =U64_MAX ; + + err =ssdfs_block_bmap_create (fsi, + &bmap->buffer[SSDFS_PEB_BLK_BMAP1 ], + items_count, init_flag, init_state); +if (unlikely (err)) { +SSDFS_ERR ("fail to create source block bitmap: " +"peb_index %u, items_count %u, " +"init_flag %#x, init_state %#x\n" , + peb_index, items_count, + init_flag, init_state); + goto fail_create_peb_bmap; + } + + err =ssdfs_block_bmap_create (fsi, + &bmap->buffer[SSDFS_PEB_BLK_BMAP2 ], + items_count, +SSDFS_BLK_BMAP_CREATE , +SSDFS_BLK_FREE ); +if (unlikely (err)) { +SSDFS_ERR ("fail to create destination block bitmap: " +"peb_index %u, items_count %u\n" , + peb_index, items_count); + goto fail_create_peb_bmap; + } + +if (init_flag ==SSDFS_BLK_BMAP_CREATE ) { +atomic_set (&bmap->peb_free_blks, items_count); +atomic_add (items_count, &parent->seg_free_blks); + } + + bmap->src = &bmap->buffer[SSDFS_PEB_BLK_BMAP1 ]; + bmap->dst =NULL ; + +init_completion (&bmap->init_end); + +atomic_set (&bmap->buffers_state,SSDFS_PEB_BMAP1_SRC ); +atomic_set (&bmap->state,SSDFS_PEB_BLK_BMAP_CREATED ); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#else +SSDFS_DBG ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +return 0 ; + +fail_create_peb_bmap: +ssdfs_peb_blk_bmap_destroy (bmap); +return err; +} + +/* +* ssdfs_peb_blk_bmap_destroy() - destroy PEB's block bitmap +* @ptr: PEB's block bitmap object +* +* This function tries to destroy PEB's block bitmap object. +*/ +void ssdfs_peb_blk_bmap_destroy (struct ssdfs_peb_blk_bmap *ptr) +{ +if (!ptr) +return ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (rwsem_is_locked (&ptr->lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("ptr %p, peb_index %u, " +"state %#x, valid_logical_blks %d, " +"invalid_logical_blks %d, " +"free_logical_blks %d\n" , + ptr, ptr->peb_index, +atomic_read (&ptr->state), +atomic_read (&ptr->peb_valid_blks), +atomic_read (&ptr->peb_invalid_blks), +atomic_read (&ptr->peb_free_blks)); +#else +SSDFS_DBG ("ptr %p, peb_index %u, " +"state %#x, valid_logical_blks %d, " +"invalid_logical_blks %d, " +"free_logical_blks %d\n" , + ptr, ptr->peb_index, +atomic_read (&ptr->state), +atomic_read (&ptr->peb_valid_blks), +atomic_read (&ptr->peb_invalid_blks), +atomic_read (&ptr->peb_free_blks)); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +if (!is_peb_block_bmap_initialized (ptr)) +SSDFS_WARN ("PEB's block bitmap hasn't been initialized\n" ); + +atomic_set (&ptr->peb_valid_blks,0 ); +atomic_set (&ptr->peb_invalid_blks,0 ); +atomic_set (&ptr->peb_free_blks,0 ); + + ptr->src =NULL ; + ptr->dst =NULL ; +atomic_set (&ptr->buffers_state,SSDFS_PEB_BMAP_BUFFERS_EMPTY ); + +ssdfs_block_bmap_destroy (&ptr->buffer[SSDFS_PEB_BLK_BMAP1 ]); +ssdfs_block_bmap_destroy (&ptr->buffer[SSDFS_PEB_BLK_BMAP2 ]); + +atomic_set (&ptr->state,SSDFS_PEB_BLK_BMAP_STATE_UNKNOWN ); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#else +SSDFS_DBG ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ +} + +/* +* ssdfs_peb_blk_bmap_init() - init PEB's block bitmap +* @bmap: pointer on PEB's block bitmap object +* @source: pointer on pagevec with bitmap state +* @hdr: header of block bitmap fragment +* @cno: log's checkpoint +* +* This function tries to init PEB's block bitmap. +* +* RETURN: +* [success] - count of free pages. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - invalid internal calculations. +*/ +int ssdfs_peb_blk_bmap_init (struct ssdfs_peb_blk_bmap *bmap, +struct ssdfs_page_vector *source, +struct ssdfs_block_bitmap_fragment *hdr, +u64 cno) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_segment_info *si; +struct ssdfs_peb_container *pebc; +struct ssdfs_block_bmap *blk_bmap =NULL ; +int bmap_state =SSDFS_PEB_BLK_BMAP_STATE_UNKNOWN ; +bool is_dst_peb_clean = false; +u8 flags; +u8 type; +bool under_migration = false; +bool has_ext_ptr = false; +bool has_relation = false; +u64 old_cno =U64_MAX ; +u32 last_free_blk; +u32 metadata_blks; +u32 free_blks; +u32 used_blks; +u32 invalid_blks; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !bmap->parent || !bmap->parent->parent_si); +BUG_ON (!bmap->parent->parent_si->peb_array); +BUG_ON (!source || !hdr); +BUG_ON (ssdfs_page_vector_count (source) ==0 ); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi = bmap->parent->parent_si->fsi; + si = bmap->parent->parent_si; + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("seg_id %llu, peb_index %u, cno %llu\n" , + si->seg_id, bmap->peb_index, cno); +#else +SSDFS_DBG ("seg_id %llu, peb_index %u, cno %llu\n" , + si->seg_id, bmap->peb_index, cno); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + + bmap_state =atomic_read (&bmap->state); +switch (bmap_state) { +case SSDFS_PEB_BLK_BMAP_CREATED : +/* regular init */ +break ; + +case SSDFS_PEB_BLK_BMAP_HAS_CLEAN_DST : +/* +* PEB container is under migration. +* But the destination PEB is clean. +* It means that destination PEB doesn't need +* in init operation. +*/ + is_dst_peb_clean = true; +break ; + +default : +SSDFS_ERR ("invalid PEB block bitmap state %#x\n" , +atomic_read (&bmap->state)); +return -ERANGE ; + } + +if (bmap->peb_index >= si->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + bmap->peb_index, si->pebs_count); +return -ERANGE ; + } + + pebc = &si->peb_array[bmap->peb_index]; + + flags = hdr->flags; + type = hdr->type; + +if (flags & ~SSDFS_FRAG_BLK_BMAP_FLAG_MASK ) { +SSDFS_ERR ("invalid flags set: %#x\n" , flags); +return -EIO ; + } + +if (type >=SSDFS_FRAG_BLK_BMAP_TYPE_MAX ) { +SSDFS_ERR ("invalid type: %#x\n" , type); +return -EIO ; + } + +if (is_dst_peb_clean) { + under_migration = true; + has_relation = true; + }else { + under_migration = flags &SSDFS_MIGRATING_BLK_BMAP ; + has_ext_ptr = flags &SSDFS_PEB_HAS_EXT_PTR ; + has_relation = flags &SSDFS_PEB_HAS_RELATION ; + } + +if (type ==SSDFS_SRC_BLK_BMAP && (has_ext_ptr && has_relation)) { +SSDFS_ERR ("invalid flags set: %#x\n" , flags); +return -EIO ; + } + +down_write (&bmap->lock); + + old_cno = bmap->init_cno; +if (bmap->init_cno ==U64_MAX ) + bmap->init_cno = cno; +else if (bmap->init_cno != cno) { + err = -ERANGE ; +SSDFS_ERR ("invalid bmap state: " +"bmap->init_cno %llu, cno %llu\n" , + bmap->init_cno, cno); + goto fail_init_blk_bmap; + } + +switch (type) { +case SSDFS_SRC_BLK_BMAP : +if (under_migration && has_relation) { +if (is_dst_peb_clean) + bmap->dst = &bmap->buffer[SSDFS_PEB_BLK_BMAP2 ]; + bmap->src = &bmap->buffer[SSDFS_PEB_BLK_BMAP1 ]; + blk_bmap = bmap->src; +atomic_set (&bmap->buffers_state, +SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST ); + }else if (under_migration && has_ext_ptr) { + bmap->src = &bmap->buffer[SSDFS_PEB_BLK_BMAP1 ]; + blk_bmap = bmap->src; +atomic_set (&bmap->buffers_state, +SSDFS_PEB_BMAP1_SRC ); + }else if (under_migration) { + err = -EIO ; +SSDFS_ERR ("invalid flags set: %#x\n" , flags); + goto fail_init_blk_bmap; + }else { + bmap->src = &bmap->buffer[SSDFS_PEB_BLK_BMAP1 ]; + blk_bmap = bmap->src; +atomic_set (&bmap->buffers_state, +SSDFS_PEB_BMAP1_SRC ); + } +break ; + +case SSDFS_DST_BLK_BMAP : +if (under_migration && has_relation) { + bmap->dst = &bmap->buffer[SSDFS_PEB_BLK_BMAP2 ]; + blk_bmap = bmap->dst; +atomic_set (&bmap->buffers_state, +SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST ); + }else if (under_migration && has_ext_ptr) { + bmap->src = &bmap->buffer[SSDFS_PEB_BLK_BMAP1 ]; + blk_bmap = bmap->src; +atomic_set (&bmap->buffers_state, +SSDFS_PEB_BMAP1_SRC ); + }else { + err = -EIO ; +SSDFS_ERR ("invalid flags set: %#x\n" , flags); + goto fail_init_blk_bmap; + } +break ; + +default : +BUG (); + } + + last_free_blk =le32_to_cpu (hdr->last_free_blk); + metadata_blks =le32_to_cpu (hdr->metadata_blks); + invalid_blks =le32_to_cpu (hdr->invalid_blks); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("seg_id %llu, peb_index %u, cno %llu, " +"last_free_blk %u, metadata_blks %u, invalid_blks %u\n" , + si->seg_id, bmap->peb_index, cno, + last_free_blk, metadata_blks, invalid_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_block_bmap_lock (blk_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock bitmap: err %d\n" , err); + goto fail_init_blk_bmap; + } + + err =ssdfs_block_bmap_init (blk_bmap, source, last_free_blk, + metadata_blks, invalid_blks); +if (unlikely (err)) { +SSDFS_ERR ("fail to initialize block bitmap: " +"err %d\n" , err); + goto fail_define_pages_count; + } + + err =ssdfs_block_bmap_get_free_pages (blk_bmap); +if (unlikely (err <0 )) { +SSDFS_ERR ("fail to get free pages: err %d\n" , err); + goto fail_define_pages_count; + }else { + free_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_used_pages (blk_bmap); +if (unlikely (err <0 )) { +SSDFS_ERR ("fail to get used pages: err %d\n" , err); + goto fail_define_pages_count; + }else { + used_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_invalid_pages (blk_bmap); +if (unlikely (err <0 )) { +SSDFS_ERR ("fail to get invalid pages: err %d\n" , err); + goto fail_define_pages_count; + }else { + invalid_blks = err; + err =0 ; + } + +fail_define_pages_count: +ssdfs_block_bmap_unlock (blk_bmap); + +if (unlikely (err)) + goto fail_init_blk_bmap; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("seg_id %llu, peb_index %u, cno %llu, " +"type %#x, under_migration %#x, has_relation %#x, " +"last_free_blk %u, metadata_blks %u, " +"free_blks %u, used_blks %u, " +"invalid_blks %u, shared_free_dst_blks %d\n" , + si->seg_id, bmap->peb_index, cno, + type, under_migration, has_relation, + last_free_blk, metadata_blks, + free_blks, used_blks, invalid_blks, +atomic_read (&pebc->shared_free_dst_blks)); +SSDFS_DBG ("seg_id %llu, peb_index %u, cno %llu, " +"free_blks %d, valid_blks %d, invalid_blks %d\n" , + si->seg_id, bmap->peb_index, cno, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (type) { +case SSDFS_SRC_BLK_BMAP : +if (is_dst_peb_clean && !(flags &SSDFS_MIGRATING_BLK_BMAP )) { +down_write (&bmap->modification_lock); +atomic_set (&bmap->peb_valid_blks, used_blks); +atomic_add (fsi->pages_per_peb - used_blks, + &bmap->peb_free_blks); +up_write (&bmap->modification_lock); + +atomic_set (&pebc->shared_free_dst_blks, + fsi->pages_per_peb - used_blks); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("SRC: seg_id %llu, peb_index %u, cno %llu, " +"pages_per_peb %u, used_blks %u, " +"shared_free_dst_blks %d\n" , + si->seg_id, bmap->peb_index, cno, + fsi->pages_per_peb, used_blks, +atomic_read (&pebc->shared_free_dst_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&bmap->parent->modification_lock); +atomic_add (atomic_read (&bmap->peb_valid_blks), + &bmap->parent->seg_valid_blks); +atomic_add (atomic_read (&bmap->peb_free_blks), + &bmap->parent->seg_free_blks); +up_write (&bmap->parent->modification_lock); + }else if (under_migration && has_relation) { +int current_free_blks = +atomic_read (&bmap->peb_free_blks); + +if (used_blks > current_free_blks) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("used_blks %u > free_blks %d\n" , + used_blks, current_free_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&bmap->modification_lock); +atomic_set (&bmap->peb_free_blks,0 ); +atomic_add (used_blks, &bmap->peb_valid_blks); +up_write (&bmap->modification_lock); + +atomic_set (&pebc->shared_free_dst_blks,0 ); + +down_write (&bmap->parent->modification_lock); +atomic_sub (current_free_blks, + &bmap->parent->seg_free_blks); +atomic_add (used_blks, + &bmap->parent->seg_valid_blks); +up_write (&bmap->parent->modification_lock); + }else { +down_write (&bmap->modification_lock); +atomic_sub (used_blks, &bmap->peb_free_blks); +atomic_add (used_blks, &bmap->peb_valid_blks); +up_write (&bmap->modification_lock); + +atomic_sub (used_blks, + &pebc->shared_free_dst_blks); + +down_write (&bmap->parent->modification_lock); +atomic_sub (used_blks, + &bmap->parent->seg_free_blks); +atomic_add (used_blks, + &bmap->parent->seg_valid_blks); +up_write (&bmap->parent->modification_lock); + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("shared_free_dst_blks %d\n" , +atomic_read (&pebc->shared_free_dst_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + }else if (under_migration && has_ext_ptr) { +down_write (&bmap->modification_lock); +atomic_add (used_blks, &bmap->peb_valid_blks); +atomic_add (invalid_blks, &bmap->peb_invalid_blks); +atomic_add (free_blks, &bmap->peb_free_blks); +up_write (&bmap->modification_lock); + }else if (under_migration) { + err = -EIO ; +SSDFS_ERR ("invalid flags set: %#x\n" , flags); + goto fail_init_blk_bmap; + }else { +down_write (&bmap->modification_lock); +atomic_set (&bmap->peb_valid_blks, used_blks); +atomic_set (&bmap->peb_invalid_blks, invalid_blks); +atomic_set (&bmap->peb_free_blks, free_blks); +up_write (&bmap->modification_lock); + +down_write (&bmap->parent->modification_lock); +atomic_add (atomic_read (&bmap->peb_valid_blks), + &bmap->parent->seg_valid_blks); +atomic_add (atomic_read (&bmap->peb_invalid_blks), + &bmap->parent->seg_invalid_blks); +atomic_add (atomic_read (&bmap->peb_free_blks), + &bmap->parent->seg_free_blks); +up_write (&bmap->parent->modification_lock); + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("SRC: seg_id %llu, peb_index %u, cno %llu, " +"free_blks %d, valid_blks %d, invalid_blks %d, " +"parent (used_blks %d, free_blks %d, invalid_blks %d)\n" , + si->seg_id, bmap->peb_index, cno, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), +atomic_read (&bmap->parent->seg_valid_blks), +atomic_read (&bmap->parent->seg_free_blks), +atomic_read (&bmap->parent->seg_invalid_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ +break ; + +case SSDFS_DST_BLK_BMAP : +if (under_migration) { +down_write (&bmap->modification_lock); +atomic_add (used_blks, &bmap->peb_valid_blks); +atomic_add (invalid_blks, &bmap->peb_invalid_blks); +atomic_add (free_blks, &bmap->peb_free_blks); +up_write (&bmap->modification_lock); + +atomic_add (free_blks, &pebc->shared_free_dst_blks); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("DST: seg_id %llu, peb_index %u, cno %llu, " +"free_blks %u, " +"shared_free_dst_blks %d\n" , + si->seg_id, bmap->peb_index, cno, + free_blks, +atomic_read (&pebc->shared_free_dst_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&bmap->parent->modification_lock); +atomic_add (used_blks, + &bmap->parent->seg_valid_blks); +atomic_add (invalid_blks, + &bmap->parent->seg_invalid_blks); +atomic_add (free_blks, + &bmap->parent->seg_free_blks); +up_write (&bmap->parent->modification_lock); + }else { + err = -EIO ; +SSDFS_ERR ("invalid flags set: %#x\n" , flags); + goto fail_init_blk_bmap; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("DST: seg_id %llu, peb_index %u, cno %llu, " +"free_blks %d, valid_blks %d, invalid_blks %d, " +"parent (used_blks %d, free_blks %d, invalid_blks %d)\n" , + si->seg_id, bmap->peb_index, cno, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), +atomic_read (&bmap->parent->seg_valid_blks), +atomic_read (&bmap->parent->seg_free_blks), +atomic_read (&bmap->parent->seg_invalid_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ +break ; + +default : +BUG (); + } + +switch (type) { +case SSDFS_SRC_BLK_BMAP : +if (under_migration && has_relation) { +if (!bmap->dst) + goto finish_init_blk_bmap; +else if (!ssdfs_block_bmap_initialized (bmap->dst)) + goto finish_init_blk_bmap; + } +break ; + +case SSDFS_DST_BLK_BMAP : +if (under_migration && has_relation) { +if (!bmap->src) + goto finish_init_blk_bmap; +else if (!ssdfs_block_bmap_initialized (bmap->src)) + goto finish_init_blk_bmap; + } +break ; + +default : +BUG (); + } + +if (atomic_read (&pebc->shared_free_dst_blks) <0 ) { +SSDFS_WARN ("type %#x, under_migration %#x, has_relation %#x, " +"last_free_blk %u, metadata_blks %u, " +"free_blks %u, used_blks %u, " +"invalid_blks %u, shared_free_dst_blks %d\n" , + type, under_migration, has_relation, + last_free_blk, metadata_blks, + free_blks, used_blks, invalid_blks, +atomic_read (&pebc->shared_free_dst_blks)); + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("seg_id %llu, peb_index %u, cno %llu, " +"free_blks %d, used_blks %d, invalid_blks %d, " +"shared_free_dst_blks %d\n" , + si->seg_id, bmap->peb_index, cno, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), +atomic_read (&pebc->shared_free_dst_blks)); +SSDFS_DBG ("seg_id %llu, peb_index %u, cno %llu, " +"parent (used_blks %d, free_blks %d, invalid_blks %d)\n" , + si->seg_id, bmap->peb_index, cno, +atomic_read (&bmap->parent->seg_valid_blks), +atomic_read (&bmap->parent->seg_free_blks), +atomic_read (&bmap->parent->seg_invalid_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + +atomic_set (&bmap->state,SSDFS_PEB_BLK_BMAP_INITIALIZED ); +complete_all (&bmap->init_end); + +fail_init_blk_bmap: +if (unlikely (err)) { + bmap->init_cno = old_cno; +complete_all (&bmap->init_end); + } + +finish_init_blk_bmap: +up_write (&bmap->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_blk_bmap_init_failed() - process failure of block bitmap init +* @bmap: pointer on PEB's block bitmap object +*/ +void ssdfs_peb_blk_bmap_init_failed (struct ssdfs_peb_blk_bmap *bmap) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + +complete_all (&bmap->init_end); +} + +/* +* is_ssdfs_peb_blk_bmap_dirty() - check that PEB block bitmap is dirty +* @bmap: pointer on PEB's block bitmap object +*/ +bool is_ssdfs_peb_blk_bmap_dirty (struct ssdfs_peb_blk_bmap *bmap) +{ +bool is_src_dirty = false; +bool is_dst_dirty = false; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) +return false; + +down_read (&bmap->lock); +if (bmap->src !=NULL ) + is_src_dirty =ssdfs_block_bmap_dirtied (bmap->src); +if (bmap->dst !=NULL ) + is_dst_dirty =ssdfs_block_bmap_dirtied (bmap->dst); +up_read (&bmap->lock); + +return is_src_dirty || is_dst_dirty; +} + +/* +* ssdfs_peb_define_reserved_pages_per_log() - estimate reserved pages per log +* @bmap: pointer on PEB's block bitmap object +*/ +int ssdfs_peb_define_reserved_pages_per_log (struct ssdfs_peb_blk_bmap *bmap) +{ +struct ssdfs_segment_blk_bmap *parent = bmap->parent; +struct ssdfs_segment_info *si = parent->parent_si; +struct ssdfs_fs_info *fsi = si->fsi; +u32 page_size = fsi->pagesize; +u32 pages_per_peb = parent->pages_per_peb; +u32 pebs_per_seg = fsi->pebs_per_seg; +u16 log_pages = si->log_pages; +bool is_migrating = false; + +switch (atomic_read (&bmap->buffers_state)) { +case SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST : +case SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST : + is_migrating = true; +break ; + +default : + is_migrating = false; +break ; + } + +return ssdfs_peb_estimate_reserved_metapages (page_size, + pages_per_peb, + log_pages, + pebs_per_seg, + is_migrating); +} + +bool has_ssdfs_peb_blk_bmap_initialized (struct ssdfs_peb_blk_bmap *bmap) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !bmap->parent || !bmap->parent->parent_si); + +SSDFS_DBG ("seg_id %llu, peb_index %u\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +return ssdfs_peb_blk_bmap_initialized (bmap); +} + +int ssdfs_peb_blk_bmap_wait_init_end (struct ssdfs_peb_blk_bmap *bmap) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !bmap->parent || !bmap->parent->parent_si); + +SSDFS_DBG ("seg_id %llu, peb_index %u\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (ssdfs_peb_blk_bmap_initialized (bmap)) +return 0 ; +else { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + } + +return 0 ; +} + +/* +* ssdfs_peb_blk_bmap_get_free_pages() - determine PEB's free pages count +* @bmap: pointer on PEB's block bitmap object +* +* This function tries to detect PEB's free pages count. +* +* RETURN: +* [success] - count of free pages. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - invalid internal calculations. +*/ +int ssdfs_peb_blk_bmap_get_free_pages (struct ssdfs_peb_blk_bmap *bmap) +{ +int free_pages; +int log_pages; +int created_logs; +int reserved_pages_per_log; +int used_pages; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !bmap->parent || !bmap->parent->parent_si); + +SSDFS_DBG ("seg_id %llu, peb_index %u\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +SSDFS_ERR ("seg_id %llu, free_logical_blks %u, " +"valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , + bmap->parent->parent_si->seg_id, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + +if (bmap->src) { +SSDFS_ERR ("SRC BLOCK BITMAP: bytes_count %zu, items_count %zu, " +"metadata_items %u, used_blks %u, invalid_blks %u, " +"flags %#x\n" , + bmap->src->bytes_count, + bmap->src->items_count, + bmap->src->metadata_items, + bmap->src->used_blks, + bmap->src->invalid_blks, +atomic_read (&bmap->src->flags)); + } + +if (bmap->dst) { +SSDFS_ERR ("DST BLOCK BITMAP: bytes_count %zu, items_count %zu, " +"metadata_items %u, used_blks %u, invalid_blks %u, " +"flags %#x\n" , + bmap->dst->bytes_count, + bmap->dst->items_count, + bmap->dst->metadata_items, + bmap->dst->used_blks, + bmap->dst->invalid_blks, +atomic_read (&bmap->dst->flags)); + } + +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("seg_id %llu, free_logical_blks %u, " +"valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , + bmap->parent->parent_si->seg_id, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > bmap->pages_per_peb) { +SSDFS_WARN ("seg_id %llu, peb_index %u, " +"free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + + log_pages = bmap->parent->parent_si->log_pages; + reserved_pages_per_log =ssdfs_peb_define_reserved_pages_per_log (bmap); + free_pages =atomic_read (&bmap->peb_free_blks); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("log_pages %d, reserved_pages_per_log %d, " +"free_pages %d\n" , + log_pages, reserved_pages_per_log, free_pages); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (free_pages >0 ) { +int upper_threshold, lower_threshold; + + created_logs = (bmap->pages_per_peb - free_pages) / log_pages; + used_pages = bmap->pages_per_peb - free_pages; + +if (created_logs ==0 ) { + upper_threshold = log_pages; + lower_threshold = reserved_pages_per_log; + }else { + upper_threshold = (created_logs +1 ) * log_pages; + lower_threshold = ((created_logs -1 ) * log_pages) + + reserved_pages_per_log; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("created_logs %d, used_pages %d, " +"upper_threshold %d, lower_threshold %d\n" , + created_logs, used_pages, + upper_threshold, lower_threshold); +#endif /* CONFIG_SSDFS_DEBUG */ + +BUG_ON (used_pages > upper_threshold); + +if (used_pages == upper_threshold) + free_pages -= reserved_pages_per_log; +else if (used_pages < lower_threshold) + free_pages -= (lower_threshold - used_pages); + +if (free_pages <0 ) + free_pages =0 ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_pages %d\n" , free_pages); +#endif /* CONFIG_SSDFS_DEBUG */ + +return free_pages; +} + +/* +* ssdfs_peb_blk_bmap_get_used_pages() - determine PEB's used data pages count +* @bmap: pointer on PEB's block bitmap object +* +* This function tries to detect PEB's used data pages count. +* +* RETURN: +* [success] - count of used data pages. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - invalid internal calculations. +*/ +int ssdfs_peb_blk_bmap_get_used_pages (struct ssdfs_peb_blk_bmap *bmap) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); + +SSDFS_DBG ("peb_index %u\n" , bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > bmap->pages_per_peb) { +SSDFS_WARN ("seg_id %llu, peb_index %u, " +"free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +return atomic_read (&bmap->peb_valid_blks); +} + +/* +* ssdfs_peb_blk_bmap_get_invalid_pages() - determine PEB's invalid pages count +* @bmap: pointer on PEB's block bitmap object +* +* This function tries to detect PEB's invalid pages count. +* +* RETURN: +* [success] - count of invalid pages. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - invalid internal calculations. +*/ +int ssdfs_peb_blk_bmap_get_invalid_pages (struct ssdfs_peb_blk_bmap *bmap) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); + +SSDFS_DBG ("peb_index %u\n" , bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > bmap->pages_per_peb) { +SSDFS_WARN ("seg_id %llu, peb_index %u, " +"free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +return atomic_read (&bmap->peb_invalid_blks); +} + +/* +* ssdfs_src_blk_bmap_get_free_pages() - determine free pages count +* @bmap: pointer on PEB's block bitmap object +* +* This function tries to detect the free pages count +* in the source bitmap. +* +* RETURN: +* [success] - count of free pages. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - invalid internal calculations. +*/ +int ssdfs_src_blk_bmap_get_free_pages (struct ssdfs_peb_blk_bmap *bmap) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); + +SSDFS_DBG ("peb_index %u\n" , bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +down_read (&bmap->lock); + +if (bmap->src ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_get_src_free_pages; + } + + err =ssdfs_block_bmap_lock (bmap->src); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_get_src_free_pages; + } + + err =ssdfs_block_bmap_get_free_pages (bmap->src); +ssdfs_block_bmap_unlock (bmap->src); + +finish_get_src_free_pages: +up_read (&bmap->lock); + +return err; +} + +/* +* ssdfs_src_blk_bmap_get_used_pages() - determine used pages count +* @bmap: pointer on PEB's block bitmap object +* +* This function tries to detect the used pages count +* in the source bitmap. +* +* RETURN: +* [success] - count of used pages. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - invalid internal calculations. +*/ +int ssdfs_src_blk_bmap_get_used_pages (struct ssdfs_peb_blk_bmap *bmap) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); + +SSDFS_DBG ("peb_index %u\n" , bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +down_read (&bmap->lock); + +if (bmap->src ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_get_src_used_pages; + } + + err =ssdfs_block_bmap_lock (bmap->src); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_get_src_used_pages; + } + + err =ssdfs_block_bmap_get_used_pages (bmap->src); +ssdfs_block_bmap_unlock (bmap->src); + +finish_get_src_used_pages: +up_read (&bmap->lock); + +return err; +} + +/* +* ssdfs_src_blk_bmap_get_invalid_pages() - determine invalid pages count +* @bmap: pointer on PEB's block bitmap object +* +* This function tries to detect the invalid pages count +* in the source bitmap. +* +* RETURN: +* [success] - count of invalid pages. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - invalid internal calculations. +*/ +int ssdfs_src_blk_bmap_get_invalid_pages (struct ssdfs_peb_blk_bmap *bmap) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); + +SSDFS_DBG ("peb_index %u\n" , bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +down_read (&bmap->lock); + +if (bmap->src ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_get_src_invalid_pages; + } + + err =ssdfs_block_bmap_lock (bmap->src); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_get_src_invalid_pages; + } + + err =ssdfs_block_bmap_get_invalid_pages (bmap->src); +ssdfs_block_bmap_unlock (bmap->src); + +finish_get_src_invalid_pages: +up_read (&bmap->lock); + +return err; +} + +/* +* ssdfs_dst_blk_bmap_get_free_pages() - determine free pages count +* @bmap: pointer on PEB's block bitmap object +* +* This function tries to detect the free pages count +* in the destination bitmap. +* +* RETURN: +* [success] - count of free pages. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - invalid internal calculations. +*/ +int ssdfs_dst_blk_bmap_get_free_pages (struct ssdfs_peb_blk_bmap *bmap) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); + +SSDFS_DBG ("peb_index %u\n" , bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +down_read (&bmap->lock); + +if (bmap->dst ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_get_dst_free_pages; + } + + err =ssdfs_block_bmap_lock (bmap->dst); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_get_dst_free_pages; + } + + err =ssdfs_block_bmap_get_free_pages (bmap->dst); +ssdfs_block_bmap_unlock (bmap->dst); + +finish_get_dst_free_pages: +up_read (&bmap->lock); + +return err; +} + +/* +* ssdfs_dst_blk_bmap_get_used_pages() - determine used pages count +* @bmap: pointer on PEB's block bitmap object +* +* This function tries to detect the used pages count +* in the destination bitmap. +* +* RETURN: +* [success] - count of used pages. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - invalid internal calculations. +*/ +int ssdfs_dst_blk_bmap_get_used_pages (struct ssdfs_peb_blk_bmap *bmap) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); + +SSDFS_DBG ("peb_index %u\n" , bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +down_read (&bmap->lock); + +if (bmap->dst ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_get_dst_used_pages; + } + + err =ssdfs_block_bmap_lock (bmap->dst); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_get_dst_used_pages; + } + + err =ssdfs_block_bmap_get_used_pages (bmap->dst); +ssdfs_block_bmap_unlock (bmap->dst); + +finish_get_dst_used_pages: +up_read (&bmap->lock); + +return err; +} + +/* +* ssdfs_dst_blk_bmap_get_invalid_pages() - determine invalid pages count +* @bmap: pointer on PEB's block bitmap object +* +* This function tries to detect the invalid pages count +* in the destination bitmap. +* +* RETURN: +* [success] - count of invalid pages. +* [failure] - error code: +* +* %-EINVAL - invalid input value. +* %-ERANGE - invalid internal calculations. +*/ +int ssdfs_dst_blk_bmap_get_invalid_pages (struct ssdfs_peb_blk_bmap *bmap) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); + +SSDFS_DBG ("peb_index %u\n" , bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +down_read (&bmap->lock); + +if (bmap->dst ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_get_dst_invalid_pages; + } + + err =ssdfs_block_bmap_lock (bmap->dst); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_get_dst_invalid_pages; + } + + err =ssdfs_block_bmap_get_invalid_pages (bmap->dst); +ssdfs_block_bmap_unlock (bmap->dst); + +finish_get_dst_invalid_pages: +up_read (&bmap->lock); + +return err; +}
fs/ssdfs/peb_block_bitmap.h new file +165 -0
165 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 14/76 ssdfs: PEB block bitmap modification operations
In reply to Viacheslav DubeykoThis patch implements PEB block bitmap modification operations:pre_allocate - pre_allocate page/range in aggregation of block bitmaps allocate - allocate page/range in aggregation of block bitmaps invalidate - invalidate page/range in aggregation of block bitmaps update_range - change the state of range in aggregation of block bitmaps collect_garbage - find contiguous range for requested state start_migration - prepare PEB's environment for migration migrate - move range from source block bitmap into destination one finish_migration - clean source block bitmap and swap block bitmaps
--- fs/ssdfs/peb_block_bitmap.c | 2418 +++++++++++++++++++++++++++++++++++ 1 file changed, 2418 insertions(+)
fs/ssdfs/peb_block_bitmap.c +2418 -0
@@ -1538,3 +1538,2421 @@ int ssdfs_dst_blk_bmap_get_invalid_pages(struct ssdfs_peb_blk_bmap *bmap)return err; } + +/* +* ssdfs_peb_blk_bmap_reserve_metapages() - reserve metadata pages +* @bmap: PEB's block bitmap object +* @bmap_index: source or destination block bitmap? +* @count: amount of metadata pages +* +* This function tries to reserve some amount of metadata pages. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ENOSPC - unable to reserve metapages. +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_reserve_metapages (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +u32 count) +{ +struct ssdfs_block_bmap *cur_bmap =NULL ; +int reserving_blks =0 ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); + +SSDFS_DBG ("seg %llu, bmap %p, bmap_index %u, count %u, " +"free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u\n" , + bmap->parent->parent_si->seg_id, + bmap, bmap_index, count, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (bmap_index <0 || bmap_index >=SSDFS_PEB_BLK_BMAP_INDEX_MAX ) { +SSDFS_WARN ("invalid bmap_index %u\n" , + bmap_index); +return -ERANGE ; + } + +down_read (&bmap->lock); + +down_write (&bmap->parent->modification_lock); +down_write (&bmap->modification_lock); + + reserving_blks =min_t (int, (int )count, +atomic_read (&bmap->peb_free_blks)); + reserving_blks =min_t (int, reserving_blks, +atomic_read (&bmap->parent->seg_free_blks)); + +if (count >atomic_read (&bmap->peb_free_blks) || + count >atomic_read (&bmap->parent->seg_free_blks)) { + err = -ENOSPC ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to reserve: " +"count %u, free_logical_blks %d, " +"parent->free_logical_blks %d\n" , + count, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->parent->seg_free_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (reserving_blks >0 ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("try to reserve: " +"reserving_blks %d\n" , + reserving_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + }else + goto finish_calculate_reserving_blks; + } + +atomic_sub (reserving_blks, &bmap->peb_free_blks); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + +if (atomic_read (&bmap->peb_free_blks) <0 ) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > + bmap->pages_per_peb) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +atomic_sub (reserving_blks, &bmap->parent->seg_free_blks); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("parent->free_logical_blks %u, " +"parent->valid_logical_blks %u, " +"parent->invalid_logical_blks %u, " +"pages_per_peb %u\n" , +atomic_read (&bmap->parent->seg_free_blks), +atomic_read (&bmap->parent->seg_valid_blks), +atomic_read (&bmap->parent->seg_invalid_blks), + bmap->parent->pages_per_peb); + +if (atomic_read (&bmap->peb_free_blks) <0 ) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > + bmap->pages_per_peb) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_calculate_reserving_blks: +up_write (&bmap->modification_lock); +up_write (&bmap->parent->modification_lock); + +if (reserving_blks <=0 && err) + goto finish_reserve_metapages; + +if (bmap_index ==SSDFS_PEB_BLK_BMAP_SOURCE ) + cur_bmap = bmap->src; +else if (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ) + cur_bmap = bmap->dst; +else + cur_bmap =NULL ; + +if (cur_bmap ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_reserve_metapages; + } + + err =ssdfs_block_bmap_lock (cur_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_reserve_metapages; + } + + err =ssdfs_block_bmap_reserve_metadata_pages (cur_bmap, + reserving_blks); +ssdfs_block_bmap_unlock (cur_bmap); + +if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to reserve metadata pages: " +"reserving_blks %d\n" , + reserving_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&bmap->parent->modification_lock); +down_write (&bmap->modification_lock); +atomic_add (reserving_blks, &bmap->peb_free_blks); +atomic_add (reserving_blks, &bmap->parent->seg_free_blks); +up_write (&bmap->modification_lock); +up_write (&bmap->parent->modification_lock); + + goto finish_reserve_metapages; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to reserve metadata pages: " +"reserving_blks %d, err %d\n" , + reserving_blks, err); + +down_write (&bmap->parent->modification_lock); +down_write (&bmap->modification_lock); +atomic_add (reserving_blks, &bmap->peb_free_blks); +atomic_add (reserving_blks, &bmap->parent->seg_free_blks); +up_write (&bmap->modification_lock); +up_write (&bmap->parent->modification_lock); + + goto finish_reserve_metapages; + } + +finish_reserve_metapages: +up_read (&bmap->lock); + +return err; +} + +/* +* ssdfs_peb_blk_bmap_free_metapages() - free metadata pages +* @bmap: PEB's block bitmap object +* @bmap_index: source or destination block bitmap? +* @count: amount of metadata pages +* +* This function tries to free some amount of metadata pages. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_free_metapages (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +u32 count) +{ +struct ssdfs_block_bmap *cur_bmap =NULL ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap); + +SSDFS_DBG ("seg %llu, bmap %p, bmap_index %u, count %u, " +"free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u\n" , + bmap->parent->parent_si->seg_id, + bmap, bmap_index, count, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +if (bmap_index <0 || bmap_index >=SSDFS_PEB_BLK_BMAP_INDEX_MAX ) { +SSDFS_WARN ("invalid bmap_index %u\n" , + bmap_index); +return -ERANGE ; + } + +down_read (&bmap->lock); + +if (bmap_index ==SSDFS_PEB_BLK_BMAP_SOURCE ) + cur_bmap = bmap->src; +else if (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ) + cur_bmap = bmap->dst; +else + cur_bmap =NULL ; + +if (cur_bmap ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_free_metapages; + } + + err =ssdfs_block_bmap_lock (cur_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_free_metapages; + } + + err =ssdfs_block_bmap_free_metadata_pages (cur_bmap, count); +ssdfs_block_bmap_unlock (cur_bmap); + +if (unlikely (err)) { +SSDFS_ERR ("fail to free metadata pages: " +"count %u, err %d\n" , + count, err); + goto finish_free_metapages; + } + +down_write (&bmap->parent->modification_lock); +down_write (&bmap->modification_lock); + +atomic_add (count, &bmap->peb_free_blks); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > + bmap->pages_per_peb) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +atomic_add (count, &bmap->parent->seg_free_blks); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("parent->free_logical_blks %u, " +"parent->valid_logical_blks %u, " +"parent->invalid_logical_blks %u, " +"pages_per_peb %u\n" , +atomic_read (&bmap->parent->seg_free_blks), +atomic_read (&bmap->parent->seg_valid_blks), +atomic_read (&bmap->parent->seg_invalid_blks), + bmap->parent->pages_per_peb); + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > + bmap->pages_per_peb) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +up_write (&bmap->modification_lock); +up_write (&bmap->parent->modification_lock); + +finish_free_metapages: +up_read (&bmap->lock); + +return err; +} + +/* +* ssdfs_peb_blk_bmap_pre_allocate() - pre-allocate a range of blocks +* @bmap: PEB's block bitmap object +* @bmap_index: source or destination block bitmap? +* @len: pointer on variable with requested length of range +* @range: pointer on blocks' range [in | out] +* +* This function tries to find contiguous range of free blocks and +* to set the found range in pre-allocated state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_pre_allocate (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +u32 *len, +struct ssdfs_block_bmap_range *range) +{ +struct ssdfs_segment_info *si; +struct ssdfs_peb_container *pebc; +struct ssdfs_block_bmap *cur_bmap =NULL ; +bool is_migrating = false; +int src_used_blks =0 ; +int src_invalid_blks =0 ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !range || !bmap->src); + +SSDFS_DBG ("bmap %p, bmap_index %u, len %p\n" , + bmap, bmap_index, len); +SSDFS_DBG ("seg %llu, free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , + bmap->parent->parent_si->seg_id, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +if (bmap_index <0 || bmap_index >=SSDFS_PEB_BLK_BMAP_INDEX_MAX ) { +SSDFS_WARN ("invalid bmap_index %u\n" , + bmap_index); +return -ERANGE ; + } + + si = bmap->parent->parent_si; + +if (bmap->peb_index >= si->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + bmap->peb_index, si->pebs_count); +return -ERANGE ; + } + + pebc = &si->peb_array[bmap->peb_index]; + +switch (atomic_read (&bmap->buffers_state)) { +case SSDFS_PEB_BMAP1_SRC : +case SSDFS_PEB_BMAP2_SRC : +BUG_ON (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ); +break ; + +case SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST : +case SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST : +/* valid state */ + is_migrating = true; +break ; + +default : +SSDFS_WARN ("invalid buffers_state %#x\n" , +atomic_read (&bmap->buffers_state)); +return -ERANGE ; + } + +down_read (&bmap->lock); +down_write (&bmap->parent->modification_lock); +down_write (&bmap->modification_lock); + +if (bmap_index ==SSDFS_PEB_BLK_BMAP_SOURCE ) { + cur_bmap = bmap->src; + is_migrating = false; + }else if (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ) { + cur_bmap = bmap->src; + +if (cur_bmap ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_pre_allocate; + } + + err =ssdfs_block_bmap_lock (cur_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , + err); + goto finish_pre_allocate; + } + + src_used_blks =ssdfs_block_bmap_get_used_pages (cur_bmap); +if (src_used_blks <0 ) { + err = src_used_blks; +SSDFS_ERR ("fail to get SRC used blocks: err %d\n" , + err); + goto finish_check_src_bmap; + } + + src_invalid_blks =ssdfs_block_bmap_get_invalid_pages (cur_bmap); +if (src_invalid_blks <0 ) { + err = src_invalid_blks; +SSDFS_ERR ("fail to get SRC invalid blocks: err %d\n" , + err); + goto finish_check_src_bmap; + } + +finish_check_src_bmap: +ssdfs_block_bmap_unlock (cur_bmap); + +if (unlikely (err)) + goto finish_pre_allocate; + + cur_bmap = bmap->dst; + }else + cur_bmap =NULL ; + +if (cur_bmap ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_pre_allocate; + } + + err =ssdfs_block_bmap_lock (cur_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_pre_allocate; + } + +if (is_migrating) { +int start_blk = src_used_blks + src_invalid_blks; + + start_blk =max_t (int, start_blk, +atomic_read (&bmap->peb_valid_blks)); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("src_used_blks %d, src_invalid_blks %d, " +"valid_blks %d, start_blk %d\n" , + src_used_blks, src_invalid_blks, +atomic_read (&bmap->peb_valid_blks), + start_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_block_bmap_pre_allocate (cur_bmap, start_blk, + len, range); + }else + err =ssdfs_block_bmap_pre_allocate (cur_bmap,0 , len, range); + +ssdfs_block_bmap_unlock (cur_bmap); + +if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to pre-allocate blocks: " +"len %u, err %d\n" , + *len, err); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_pre_allocate; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to pre-allocate blocks: " +"len %u, err %d\n" , + *len, err); + goto finish_pre_allocate; + } + +if (!is_migrating) { +if (range->len >atomic_read (&bmap->peb_free_blks)) { + err = -ERANGE ; +SSDFS_ERR ("range %u > free_logical_blks %d\n" , + range->len, +atomic_read (&bmap->peb_free_blks)); + goto finish_pre_allocate; + } + +atomic_sub (range->len, &bmap->peb_free_blks); +atomic_add (range->len, &bmap->peb_valid_blks); +atomic_add (range->len, &bmap->parent->seg_valid_blks); + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + +if (atomic_read (&bmap->peb_free_blks) <0 ) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > + bmap->pages_per_peb) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +if (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ) { +int shared_free_blks; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("range->len %u, shared_free_dst_blks %d\n" , + range->len, +atomic_read (&pebc->shared_free_dst_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + + shared_free_blks = +atomic_sub_return (range->len, + &pebc->shared_free_dst_blks); +if (shared_free_blks <0 ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("range->len %u, shared_free_dst_blks %d\n" , + range->len, +atomic_read (&pebc->shared_free_dst_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("parent->free_logical_blks %u, " +"parent->valid_logical_blks %u, " +"parent->invalid_logical_blks %u, " +"pages_per_peb %u\n" , +atomic_read (&bmap->parent->seg_free_blks), +atomic_read (&bmap->parent->seg_valid_blks), +atomic_read (&bmap->parent->seg_invalid_blks), + bmap->parent->pages_per_peb); + +if (atomic_read (&bmap->peb_free_blks) <0 ) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > + bmap->pages_per_peb) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_pre_allocate: +up_write (&bmap->modification_lock); +up_write (&bmap->parent->modification_lock); +up_read (&bmap->lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("PRE-ALLOCATED: range (start %u, len %u), err %d\n" , + range->start, range->len, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_peb_blk_bmap_allocate() - allocate a range of blocks +* @bmap: PEB's block bitmap object +* @bmap_index: source or destination block bitmap? +* @len: pointer on variable with requested length of range +* @range: pointer on blocks' range [in | out] +* +* This function tries to find contiguous range of free blocks and +* to set the found range in allocated state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_allocate (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +u32 *len, +struct ssdfs_block_bmap_range *range) +{ +struct ssdfs_segment_info *si; +struct ssdfs_peb_container *pebc; +struct ssdfs_block_bmap *cur_bmap =NULL ; +bool is_migrating = false; +int src_used_blks =0 ; +int src_invalid_blks =0 ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !range || !bmap->src); + +SSDFS_DBG ("bmap %p, bmap_index %u, len %p\n" , + bmap, bmap_index, len); +SSDFS_DBG ("seg %llu, free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , + bmap->parent->parent_si->seg_id, +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +if (bmap_index <0 || bmap_index >=SSDFS_PEB_BLK_BMAP_INDEX_MAX ) { +SSDFS_WARN ("invalid bmap_index %u\n" , + bmap_index); +return -ERANGE ; + } + + si = bmap->parent->parent_si; + +if (bmap->peb_index >= si->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + bmap->peb_index, si->pebs_count); +return -ERANGE ; + } + + pebc = &si->peb_array[bmap->peb_index]; + +switch (atomic_read (&bmap->buffers_state)) { +case SSDFS_PEB_BMAP1_SRC : +case SSDFS_PEB_BMAP2_SRC : +BUG_ON (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ); +break ; + +case SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST : +case SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST : +/* valid state */ + is_migrating = true; +break ; + +default : +SSDFS_WARN ("invalid buffers_state %#x\n" , +atomic_read (&bmap->buffers_state)); +return -ERANGE ; + } + +down_read (&bmap->lock); +down_write (&bmap->parent->modification_lock); +down_write (&bmap->modification_lock); + +if (bmap_index ==SSDFS_PEB_BLK_BMAP_SOURCE ) { + cur_bmap = bmap->src; + is_migrating = false; + }else if (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ) { + cur_bmap = bmap->src; + +if (cur_bmap ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_allocate; + } + + err =ssdfs_block_bmap_lock (cur_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , + err); + goto finish_allocate; + } + + src_used_blks =ssdfs_block_bmap_get_used_pages (cur_bmap); +if (src_used_blks <0 ) { + err = src_used_blks; +SSDFS_ERR ("fail to get SRC used blocks: err %d\n" , + err); + goto finish_check_src_bmap; + } + + src_invalid_blks =ssdfs_block_bmap_get_invalid_pages (cur_bmap); +if (src_invalid_blks <0 ) { + err = src_invalid_blks; +SSDFS_ERR ("fail to get SRC invalid blocks: err %d\n" , + err); + goto finish_check_src_bmap; + } + +finish_check_src_bmap: +ssdfs_block_bmap_unlock (cur_bmap); + +if (unlikely (err)) + goto finish_allocate; + + cur_bmap = bmap->dst; + }else + cur_bmap =NULL ; + +if (cur_bmap ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_allocate; + } + + err =ssdfs_block_bmap_lock (cur_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_allocate; + } + +if (is_migrating) { +int start_blk = src_used_blks + src_invalid_blks; + + start_blk =max_t (int, start_blk, +atomic_read (&bmap->peb_valid_blks)); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("src_used_blks %d, src_invalid_blks %d, " +"valid_blks %d, start_blk %d\n" , + src_used_blks, src_invalid_blks, +atomic_read (&bmap->peb_valid_blks), + start_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_block_bmap_allocate (cur_bmap, start_blk, + len, range); + }else + err =ssdfs_block_bmap_allocate (cur_bmap,0 , len, range); + +ssdfs_block_bmap_unlock (cur_bmap); + +if (err == -ENOSPC ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to allocate blocks: " +"len %u, err %d\n" , + *len, err); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_allocate; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to allocate blocks: " +"len %u, err %d\n" , + *len, err); + goto finish_allocate; + } + +if (!is_migrating) { +if (range->len >atomic_read (&bmap->peb_free_blks)) { + err = -ERANGE ; +SSDFS_ERR ("range %u > free_logical_blks %d\n" , + range->len, +atomic_read (&bmap->peb_free_blks)); + goto finish_allocate; + } + +atomic_sub (range->len, &bmap->peb_free_blks); +atomic_add (range->len, &bmap->peb_valid_blks); +atomic_add (range->len, &bmap->parent->seg_valid_blks); + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + +if (atomic_read (&bmap->peb_free_blks) <0 ) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > + bmap->pages_per_peb) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +if (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ) { +int shared_free_blks; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("range->len %u, shared_free_dst_blks %d\n" , + range->len, +atomic_read (&pebc->shared_free_dst_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + + shared_free_blks = +atomic_sub_return (range->len, + &pebc->shared_free_dst_blks); +if (shared_free_blks <0 ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("range->len %u, shared_free_dst_blks %d\n" , + range->len, +atomic_read (&pebc->shared_free_dst_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("parent->free_logical_blks %u, " +"parent->valid_logical_blks %u, " +"parent->invalid_logical_blks %u, " +"pages_per_peb %u\n" , +atomic_read (&bmap->parent->seg_free_blks), +atomic_read (&bmap->parent->seg_valid_blks), +atomic_read (&bmap->parent->seg_invalid_blks), + bmap->parent->pages_per_peb); + +if (atomic_read (&bmap->peb_free_blks) <0 ) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > + bmap->pages_per_peb) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_allocate: +up_write (&bmap->modification_lock); +up_write (&bmap->parent->modification_lock); +up_read (&bmap->lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("ALLOCATED: range (start %u, len %u), err %d\n" , + range->start, range->len, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_peb_blk_bmap_invalidate() - invalidate a range of blocks +* @bmap: PEB's block bitmap object +* @bmap_index: source or destination block bitmap? +* @range: pointer on blocks' range [in | out] +* +* This function tries to set the requested range of blocks in +* invalid state. At first, it checks that requested range contains +* valid blocks only. And, then, it sets the requested range of blocks +* in invalid state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_invalidate (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +struct ssdfs_block_bmap_range *range) +{ +struct ssdfs_block_bmap *cur_bmap =NULL ; +bool is_migrating = false; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !range || !bmap->src); + +SSDFS_DBG ("seg %llu, bmap %p, bmap_index %u, " +"range (start %u, len %u)\n" , + bmap->parent->parent_si->seg_id, + bmap, bmap_index, range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +if (bmap_index <0 || bmap_index >=SSDFS_PEB_BLK_BMAP_INDEX_MAX ) { +SSDFS_WARN ("invalid bmap_index %u\n" , + bmap_index); +return -ERANGE ; + } + +switch (atomic_read (&bmap->buffers_state)) { +case SSDFS_PEB_BMAP1_SRC : +case SSDFS_PEB_BMAP2_SRC : +BUG_ON (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ); +break ; + +case SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST : +case SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST : +/* valid state */ + is_migrating = true; +break ; + +default : +SSDFS_WARN ("invalid buffers_state %#x\n" , +atomic_read (&bmap->buffers_state)); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u, " +"is_migrating %#x\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb, is_migrating); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&bmap->lock); +down_write (&bmap->parent->modification_lock); +down_write (&bmap->modification_lock); + +if (bmap_index ==SSDFS_PEB_BLK_BMAP_SOURCE ) + cur_bmap = bmap->src; +else if (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ) + cur_bmap = bmap->dst; +else + cur_bmap =NULL ; + +if (cur_bmap ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_invalidate; + } + + err =ssdfs_block_bmap_lock (cur_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_invalidate; + } + + err =ssdfs_block_bmap_invalidate (cur_bmap, range); + +ssdfs_block_bmap_unlock (cur_bmap); + +if (unlikely (err)) { +SSDFS_ERR ("fail to invalidate blocks: " +"len %u, err %d\n" , + range->len, err); + goto finish_invalidate; + } + +if (!is_migrating) { +if (range->len >atomic_read (&bmap->peb_valid_blks)) { + err = -ERANGE ; +SSDFS_ERR ("range %u > valid_logical_blks %d\n" , + range->len, +atomic_read (&bmap->peb_valid_blks)); + goto finish_invalidate; + } + +atomic_sub (range->len, &bmap->peb_valid_blks); +atomic_add (range->len, &bmap->peb_invalid_blks); + +atomic_sub (range->len, &bmap->parent->seg_valid_blks); +atomic_add (range->len, &bmap->parent->seg_invalid_blks); + }else if (is_migrating && + bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ) { +if (range->len >atomic_read (&bmap->peb_valid_blks)) { + err = -ERANGE ; +SSDFS_ERR ("range %u > valid_logical_blks %d\n" , + range->len, +atomic_read (&bmap->peb_valid_blks)); + goto finish_invalidate; + } + +atomic_sub (range->len, &bmap->peb_valid_blks); +atomic_add (range->len, &bmap->peb_invalid_blks); + +atomic_sub (range->len, &bmap->parent->seg_valid_blks); +atomic_add (range->len, &bmap->parent->seg_invalid_blks); + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + +if (atomic_read (&bmap->peb_free_blks) <0 ) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > + bmap->pages_per_peb) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } + +SSDFS_DBG ("parent->free_logical_blks %u, " +"parent->valid_logical_blks %u, " +"parent->invalid_logical_blks %u, " +"pages_per_peb %u\n" , +atomic_read (&bmap->parent->seg_free_blks), +atomic_read (&bmap->parent->seg_valid_blks), +atomic_read (&bmap->parent->seg_invalid_blks), + bmap->parent->pages_per_peb); + +if (atomic_read (&bmap->peb_free_blks) <0 ) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } + +if ((atomic_read (&bmap->peb_free_blks) + +atomic_read (&bmap->peb_valid_blks) + +atomic_read (&bmap->peb_invalid_blks)) > + bmap->pages_per_peb) { +SSDFS_WARN ("free_logical_blks %u, valid_logical_blks %u, " +"invalid_logical_blks %u, pages_per_peb %u\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks), + bmap->pages_per_peb); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_invalidate: +up_write (&bmap->modification_lock); +up_write (&bmap->parent->modification_lock); +up_read (&bmap->lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("INVALIDATED: seg %llu, " +"range (start %u, len %u), err %d\n" , + bmap->parent->parent_si->seg_id, + range->start, range->len, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_peb_blk_bmap_update_range() - update a range of blocks' state +* @bmap: PEB's block bitmap object +* @bmap_index: source or destination block bitmap? +* @new_range_state: new state of the range +* @range: pointer on blocks' range [in | out] +* +* This function tries to change a range of blocks' state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_update_range (struct ssdfs_peb_blk_bmap *bmap, +int bmap_index, +int new_range_state, +struct ssdfs_block_bmap_range *range) +{ +struct ssdfs_block_bmap *cur_bmap =NULL ; +int range_state; +#ifdef CONFIG_SSDFS_DEBUG +int free_blks, used_blks, invalid_blks; +#endif /* CONFIG_SSDFS_DEBUG */ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !range); +BUG_ON (!(new_range_state ==SSDFS_BLK_PRE_ALLOCATED || + new_range_state ==SSDFS_BLK_VALID )); + +SSDFS_DBG ("bmap %p, peb_index %u, state %#x, " +"new_range_state %#x, " +"range (start %u, len %u)\n" , + bmap, bmap->peb_index, +atomic_read (&bmap->state), + new_range_state, + range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +if (bmap_index <0 || bmap_index >=SSDFS_PEB_BLK_BMAP_INDEX_MAX ) { +SSDFS_WARN ("invalid bmap_index %u\n" , + bmap_index); +return -ERANGE ; + } + +switch (atomic_read (&bmap->buffers_state)) { +case SSDFS_PEB_BMAP1_SRC : +case SSDFS_PEB_BMAP2_SRC : +BUG_ON (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ); +break ; + +case SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST : +case SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST : +/* valid state */ +break ; + +default : +SSDFS_WARN ("invalid buffers_state %#x\n" , +atomic_read (&bmap->buffers_state)); +return -ERANGE ; + } + +down_read (&bmap->lock); + +if (bmap_index ==SSDFS_PEB_BLK_BMAP_SOURCE ) + cur_bmap = bmap->src; +else if (bmap_index ==SSDFS_PEB_BLK_BMAP_DESTINATION ) + cur_bmap = bmap->dst; +else + cur_bmap =NULL ; + +if (cur_bmap ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_update_range; + } + + err =ssdfs_block_bmap_lock (cur_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_update_range; + } + +#ifdef CONFIG_SSDFS_DEBUG + err =ssdfs_block_bmap_get_free_pages (cur_bmap); +if (err <0 ) { +SSDFS_ERR ("fail to get free pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto finish_process_bmap; + }else { + free_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_used_pages (cur_bmap); +if (err <0 ) { +SSDFS_ERR ("fail to get used pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto finish_process_bmap; + }else { + used_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_invalid_pages (cur_bmap); +if (err <0 ) { +SSDFS_ERR ("fail to get invalid pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto finish_process_bmap; + }else { + invalid_blks = err; + err =0 ; + } + +if (unlikely (err)) + goto finish_process_bmap; + +SSDFS_DBG ("BEFORE: free_blks %d, used_blks %d, invalid_blks %d\n" , + free_blks, used_blks, invalid_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + + range_state =ssdfs_get_range_state (cur_bmap, range); +if (range_state <0 ) { + err = range_state; +SSDFS_ERR ("fail to detect range state: " +"range (start %u, len %u), err %d\n" , + range->start, range->len, err); + goto finish_process_bmap; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("current range_state %#x\n" , + range_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (range_state) { +case SSDFS_BLK_FREE : +/* valid block state */ +break ; + +case SSDFS_BLK_PRE_ALLOCATED : +if (new_range_state ==SSDFS_BLK_PRE_ALLOCATED ) { +/* do nothing */ + goto finish_process_bmap; + } +break ; + +case SSDFS_BLK_VALID : +if (new_range_state ==SSDFS_BLK_PRE_ALLOCATED ) { + err = -ERANGE ; +SSDFS_WARN ("fail to change state: " +"range_state %#x, " +"new_range_state %#x\n" , + range_state, new_range_state); + goto finish_process_bmap; + }else if (new_range_state ==SSDFS_BLK_VALID ) { +/* do nothing */ + goto finish_process_bmap; + } +break ; + +default : + err = -ERANGE ; +SSDFS_ERR ("invalid range state: %#x\n" , + range_state); + goto finish_process_bmap; + }; + +if (new_range_state ==SSDFS_BLK_PRE_ALLOCATED ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("try to pre-allocate: " +"range (start %u, len %u)\n" , + range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + err =ssdfs_block_bmap_pre_allocate (cur_bmap,0 ,NULL , range); + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("try to allocate: " +"range (start %u, len %u)\n" , + range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + err =ssdfs_block_bmap_allocate (cur_bmap,0 ,NULL , range); + } + +finish_process_bmap: +ssdfs_block_bmap_unlock (cur_bmap); + +if (unlikely (err)) { +SSDFS_ERR ("fail to update range: " +"range (start %u, len %u), " +"new_range_state %#x, err %d\n" , + range->start, range->len, + new_range_state, err); + goto finish_update_range; + } + +#ifdef CONFIG_SSDFS_DEBUG + err =ssdfs_block_bmap_lock (cur_bmap); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_update_range; + } + + err =ssdfs_block_bmap_get_free_pages (cur_bmap); +if (err <0 ) { +SSDFS_ERR ("fail to get free pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_bmap; + }else { + free_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_used_pages (cur_bmap); +if (err <0 ) { +SSDFS_ERR ("fail to get used pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_bmap; + }else { + used_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_invalid_pages (cur_bmap); +if (err <0 ) { +SSDFS_ERR ("fail to get invalid pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_bmap; + }else { + invalid_blks = err; + err =0 ; + } + +unlock_bmap: +ssdfs_block_bmap_unlock (cur_bmap); + +if (unlikely (err)) + goto finish_update_range; + +SSDFS_DBG ("AFTER: free_blks %d, used_blks %d, invalid_blks %d\n" , + free_blks, used_blks, invalid_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_update_range: +up_read (&bmap->lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("UPDATED: range (start %u, len %u), " +"new_range_state %#x, err %d\n" , + range->start, range->len, + new_range_state, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_peb_blk_bmap_collect_garbage() - find range of valid blocks for GC +* @bmap: PEB's block bitmap object +* @start: starting position for search +* @max_len: maximum requested length of valid blocks' range +* @blk_state: requested block state (pre-allocated or valid) +* @range: pointer on blocks' range [out] +* +* This function tries to find range of valid or pre_allocated blocks +* for GC in source block bitmap. The length of requested range is +* limited by @max_len. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_collect_garbage (struct ssdfs_peb_blk_bmap *bmap, +u32 start,u32 max_len, +int blk_state, +struct ssdfs_block_bmap_range *range) +{ +struct ssdfs_block_bmap *src =NULL ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !range || !bmap->src); + +SSDFS_DBG ("bmap %p, start %u, max_len %u\n" , + bmap, start, max_len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +switch (atomic_read (&bmap->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 : +SSDFS_WARN ("invalid buffers_state %#x\n" , +atomic_read (&bmap->buffers_state)); +return -ERANGE ; + } + +down_read (&bmap->lock); + + src = bmap->src; + +if (src ==NULL ) { + err = -ERANGE ; +SSDFS_WARN ("bmap pointer is empty\n" ); + goto finish_collect_garbage; + } + + err =ssdfs_block_bmap_lock (src); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_collect_garbage; + } + + err =ssdfs_block_bmap_collect_garbage (src, start, max_len, + blk_state, range); + +ssdfs_block_bmap_unlock (src); + +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("range (start %u, len %u) hasn't valid blocks\n" , + range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_collect_garbage; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find valid blocks: " +"len %u, err %d\n" , + range->len, err); + goto finish_collect_garbage; + } + +finish_collect_garbage: +up_read (&bmap->lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("GARBAGE: range (start %u, len %u), " +"blk_state %#x, err %d\n" , + range->start, range->len, + blk_state, err); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_peb_blk_bmap_start_migration() - prepare migration environment +* @bmap: PEB's block bitmap object +* +* This method tries to prepare PEB's environment for migration. +* The destination block bitmap is cleaned in buffer and pointer +* is set. Also valid/invalid/free block counters are prepared +* for migration operation. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_start_migration (struct ssdfs_peb_blk_bmap *bmap) +{ +struct ssdfs_segment_info *si; +struct ssdfs_peb_container *pebc; +int buffers_state, new_buffers_state; +int buffer_index; +int free_blks =0 ; +int invalid_blks; +int used_blks; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !bmap->src); + +SSDFS_DBG ("bmap %p, peb_index %u, state %#x\n" , + bmap, bmap->peb_index, +atomic_read (&bmap->state)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_logical_blocks %d, valid_logical_block %d, " +"invalid_logical_block %d\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + + si = bmap->parent->parent_si; + +if (bmap->peb_index >= si->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + bmap->peb_index, si->pebs_count); +return -ERANGE ; + } + + pebc = &si->peb_array[bmap->peb_index]; + +down_write (&bmap->lock); +down_write (&bmap->parent->modification_lock); +down_write (&bmap->modification_lock); + + buffers_state =atomic_read (&bmap->buffers_state); + +switch (buffers_state) { +case SSDFS_PEB_BMAP1_SRC : + new_buffers_state =SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST ; + buffer_index =SSDFS_PEB_BLK_BMAP2 ; +break ; + +case SSDFS_PEB_BMAP2_SRC : + new_buffers_state =SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST ; + buffer_index =SSDFS_PEB_BLK_BMAP1 ; +break ; + +case SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST : +case SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST : + err = -ENOENT ; +SSDFS_WARN ("bmap is under migration: " +"peb_index %u, state %#x\n" , + bmap->peb_index, buffers_state); + goto finish_migration_start; + +default : + err = -ERANGE ; +SSDFS_WARN ("fail to start migration: " +"buffers_state %#x\n" , + buffers_state); + goto finish_migration_start; + } + + err =ssdfs_block_bmap_lock (&bmap->buffer[buffer_index]); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_migration_start; + } + +switch (atomic_read (&bmap->buffers_state)) { +case SSDFS_PEB_BMAP1_SRC : +#ifdef CONFIG_SSDFS_DEBUG +WARN_ON (buffers_state !=SSDFS_PEB_BMAP1_SRC ); +BUG_ON (!bmap->src || bmap->dst); +#endif /* CONFIG_SSDFS_DEBUG */ +break ; + +case SSDFS_PEB_BMAP2_SRC : +#ifdef CONFIG_SSDFS_DEBUG +WARN_ON (buffers_state !=SSDFS_PEB_BMAP2_SRC ); +BUG_ON (!bmap->src || bmap->dst); +#endif /* CONFIG_SSDFS_DEBUG */ +break ; + +default : + err = -ENOENT ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("block bitmap has been prepared: " +"peb_index %u\n" , + bmap->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_block_bitmap_preparation; + } + + err =ssdfs_block_bmap_clean (&bmap->buffer[buffer_index]); +if (unlikely (err == -ENOENT )) { + err = -ERANGE ; +SSDFS_WARN ("unable to clean block bitmap: " +"peb_index %u\n" , + bmap->peb_index); + goto finish_block_bitmap_preparation; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to clean block bitmap: " +"peb_index %u\n" , + bmap->peb_index); + goto finish_block_bitmap_preparation; + } + + bmap->dst = &bmap->buffer[buffer_index]; +atomic_set (&bmap->buffers_state, new_buffers_state); + + free_blks =atomic_read (&bmap->peb_free_blks); +atomic_sub (free_blks, &bmap->peb_free_blks); +atomic_sub (free_blks, &bmap->parent->seg_free_blks); + + invalid_blks =atomic_xchg (&bmap->peb_invalid_blks,0 ); +atomic_sub (invalid_blks, &bmap->parent->seg_invalid_blks); +atomic_add (invalid_blks, &bmap->peb_free_blks); +atomic_set (&pebc->shared_free_dst_blks, invalid_blks); +atomic_add (invalid_blks, &bmap->parent->seg_free_blks); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("shared_free_dst_blks %d\n" , +atomic_read (&pebc->shared_free_dst_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + + used_blks =atomic_read (&bmap->peb_valid_blks); + + err =ssdfs_block_bmap_get_free_pages (bmap->dst); +if (err <0 ) { +SSDFS_ERR ("fail to get free pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto finish_block_bitmap_preparation; + }else { + free_blks = err; + err =0 ; + } + +if (free_blks < (invalid_blks + used_blks)) { + err = -ERANGE ; +SSDFS_ERR ("free_blks %d < (invalid_blks %d + used_blks %d)\n" , + free_blks, invalid_blks, used_blks); + goto finish_block_bitmap_preparation; + } + + free_blks -= invalid_blks + used_blks; + +atomic_add (free_blks, &bmap->peb_free_blks); +atomic_add (free_blks, &bmap->parent->seg_free_blks); + +finish_block_bitmap_preparation: +ssdfs_block_bmap_unlock (&bmap->buffer[buffer_index]); + +if (unlikely (err)) + goto finish_migration_start; + +#ifdef CONFIG_SSDFS_DEBUG + err =ssdfs_block_bmap_lock (bmap->dst); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_migration_start; + } + + err =ssdfs_block_bmap_get_free_pages (bmap->dst); +if (err <0 ) { +SSDFS_ERR ("fail to get free pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_dst_bmap; + }else { + free_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_used_pages (bmap->dst); +if (err <0 ) { +SSDFS_ERR ("fail to get used pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_dst_bmap; + }else { + used_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_invalid_pages (bmap->dst); +if (err <0 ) { +SSDFS_ERR ("fail to get invalid pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_dst_bmap; + }else { + invalid_blks = err; + err =0 ; + } + +unlock_dst_bmap: +ssdfs_block_bmap_unlock (bmap->dst); + +if (unlikely (err)) + goto finish_migration_start; + +SSDFS_DBG ("DST: free_blks %d, used_blks %d, invalid_blks %d\n" , + free_blks, used_blks, invalid_blks); + + err =ssdfs_block_bmap_lock (bmap->src); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_migration_start; + } + + err =ssdfs_block_bmap_get_free_pages (bmap->src); +if (err <0 ) { +SSDFS_ERR ("fail to get free pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_src_bmap; + }else { + free_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_used_pages (bmap->src); +if (err <0 ) { +SSDFS_ERR ("fail to get used pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_src_bmap; + }else { + used_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_invalid_pages (bmap->src); +if (err <0 ) { +SSDFS_ERR ("fail to get invalid pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_src_bmap; + }else { + invalid_blks = err; + err =0 ; + } + +unlock_src_bmap: +ssdfs_block_bmap_unlock (bmap->src); + +if (unlikely (err)) + goto finish_migration_start; + +SSDFS_DBG ("SRC: free_blks %d, used_blks %d, invalid_blks %d\n" , + free_blks, used_blks, invalid_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_migration_start: +up_write (&bmap->modification_lock); +up_write (&bmap->parent->modification_lock); +up_write (&bmap->lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free_logical_blocks %d, valid_logical_block %d, " +"invalid_logical_block %d\n" , +atomic_read (&bmap->peb_free_blks), +atomic_read (&bmap->peb_valid_blks), +atomic_read (&bmap->peb_invalid_blks)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (err == -ENOENT ) +return 0 ; +else if (unlikely (err)) +return err; + +return 0 ; +} + +/* +* ssdfs_peb_blk_bmap_migrate() - migrate valid blocks +* @bmap: PEB's block bitmap object +* @new_range_state: new state of range +* @range: pointer on blocks' range +* +* This method tries to move @range of blocks from source +* block bitmap into destination block bitmap. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_migrate (struct ssdfs_peb_blk_bmap *bmap, +int new_range_state, +struct ssdfs_block_bmap_range *range) +{ +int buffers_state; +int range_state; +struct ssdfs_block_bmap *src; +struct ssdfs_block_bmap *dst; +int free_blks; +#ifdef CONFIG_SSDFS_DEBUG +int used_blks, invalid_blks; +#endif /* CONFIG_SSDFS_DEBUG */ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !range); +BUG_ON (!(new_range_state ==SSDFS_BLK_PRE_ALLOCATED || + new_range_state ==SSDFS_BLK_VALID )); + +SSDFS_DBG ("bmap %p, peb_index %u, state %#x, " +"new_range_state %#x, range (start %u, len %u)\n" , + bmap, bmap->peb_index, +atomic_read (&bmap->state), + new_range_state, + range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + +down_read (&bmap->lock); + + buffers_state =atomic_read (&bmap->buffers_state); + +switch (buffers_state) { +case SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST : +case SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST : + src = bmap->src; + dst = bmap->dst; +break ; + +default : + err = -ERANGE ; +SSDFS_WARN ("fail to migrate: " +"buffers_state %#x, " +"range (start %u, len %u)\n" , + buffers_state, + range->start, range->len); + goto finish_migrate; + } + +#ifdef CONFIG_SSDFS_DEBUG +if (!src || !dst) { + err = -ERANGE ; +SSDFS_WARN ("empty pointers\n" ); + goto finish_migrate; + } +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_block_bmap_lock (src); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_migrate; + } + + range_state =ssdfs_get_range_state (src, range); +if (range_state <0 ) { + err = range_state; +SSDFS_ERR ("fail to detect range state: " +"range (start %u, len %u), err %d\n" , + range->start, range->len, err); + goto finish_process_source_bmap; + } + +switch (range_state) { +case SSDFS_BLK_PRE_ALLOCATED : +/* valid block state */ + err =ssdfs_block_bmap_invalidate (src, range); +break ; + +case SSDFS_BLK_VALID : +if (new_range_state ==SSDFS_BLK_PRE_ALLOCATED ) { + err = -ERANGE ; +SSDFS_WARN ("fail to change state: " +"range_state %#x, " +"new_range_state %#x\n" , + range_state, new_range_state); + goto finish_process_source_bmap; + } + + err =ssdfs_block_bmap_invalidate (src, range); +break ; + +case SSDFS_BLK_INVALID : +/* range was invalidated already */ +break ; + +default : + err = -ERANGE ; +SSDFS_ERR ("invalid range state: %#x\n" , + range_state); + goto finish_process_source_bmap; + }; + +finish_process_source_bmap: +ssdfs_block_bmap_unlock (src); + +if (unlikely (err)) { +SSDFS_ERR ("fail to invalidate blocks: " +"start %u, len %u, err %d\n" , + range->start, range->len, err); + goto finish_migrate; + } + + err =ssdfs_block_bmap_lock (dst); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_migrate; + } + + err =ssdfs_block_bmap_get_free_pages (dst); +if (err <0 ) { +SSDFS_ERR ("fail to get free pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto do_bmap_unlock; + }else { + free_blks = err; + err =0 ; + } + +if (free_blks < range->len) { +u32 freed_metapages = range->len - free_blks; + + err =ssdfs_block_bmap_free_metadata_pages (dst, + freed_metapages); +if (unlikely (err)) { +SSDFS_ERR ("fail to free metadata pages: err %d\n" , + err); + goto do_bmap_unlock; + } + } + +if (new_range_state ==SSDFS_BLK_PRE_ALLOCATED ) + err =ssdfs_block_bmap_pre_allocate (dst,0 ,NULL , range); +else + err =ssdfs_block_bmap_allocate (dst,0 ,NULL , range); + +do_bmap_unlock: +ssdfs_block_bmap_unlock (dst); + +if (unlikely (err)) { +SSDFS_ERR ("fail to allocate blocks: " +"start %u, len %u, err %d\n" , + range->start, range->len, err); + goto finish_migrate; + } + +#ifdef CONFIG_SSDFS_DEBUG + err =ssdfs_block_bmap_lock (src); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_migrate; + } + + err =ssdfs_block_bmap_get_free_pages (src); +if (err <0 ) { +SSDFS_ERR ("fail to get free pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_src_bmap; + }else { + free_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_used_pages (src); +if (err <0 ) { +SSDFS_ERR ("fail to get used pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_src_bmap; + }else { + used_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_invalid_pages (src); +if (err <0 ) { +SSDFS_ERR ("fail to get invalid pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_src_bmap; + }else { + invalid_blks = err; + err =0 ; + } + +unlock_src_bmap: +ssdfs_block_bmap_unlock (src); + +if (unlikely (err)) + goto finish_migrate; + +SSDFS_DBG ("SRC: free_blks %d, used_blks %d, invalid_blks %d\n" , + free_blks, used_blks, invalid_blks); + + err =ssdfs_block_bmap_lock (dst); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_migrate; + } + + err =ssdfs_block_bmap_get_free_pages (dst); +if (err <0 ) { +SSDFS_ERR ("fail to get free pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_dst_bmap; + }else { + free_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_used_pages (dst); +if (err <0 ) { +SSDFS_ERR ("fail to get used pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_dst_bmap; + }else { + used_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_invalid_pages (dst); +if (err <0 ) { +SSDFS_ERR ("fail to get invalid pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_dst_bmap; + }else { + invalid_blks = err; + err =0 ; + } + +unlock_dst_bmap: +ssdfs_block_bmap_unlock (dst); + +if (unlikely (err)) + goto finish_migrate; + +SSDFS_DBG ("DST: free_blks %d, used_blks %d, invalid_blks %d\n" , + free_blks, used_blks, invalid_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_migrate: +up_read (&bmap->lock); + +return err; +} + +/* +* ssdfs_peb_blk_bmap_finish_migration() - stop migration +* @bmap: PEB's block bitmap object +* +* This method tries to make destination block bitmap as +* source and to forget about old source block bitmap. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_peb_blk_bmap_finish_migration (struct ssdfs_peb_blk_bmap *bmap) +{ +struct ssdfs_segment_info *si; +struct ssdfs_peb_container *pebc; +int buffers_state, new_buffers_state; +int buffer_index; +#ifdef CONFIG_SSDFS_DEBUG +int free_blks, used_blks, invalid_blks; +#endif /* CONFIG_SSDFS_DEBUG */ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !bmap->src); + +SSDFS_DBG ("bmap %p, peb_index %u, state %#x\n" , + bmap, bmap->peb_index, +atomic_read (&bmap->state)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err =SSDFS_WAIT_COMPLETION (&bmap->init_end); +if (unlikely (err)) { +init_failed: +SSDFS_ERR ("PEB block bitmap init failed: " +"seg_id %llu, peb_index %u, " +"err %d\n" , + bmap->parent->parent_si->seg_id, + bmap->peb_index, err); +return err; + } + +if (!ssdfs_peb_blk_bmap_initialized (bmap)) { + err = -ERANGE ; + goto init_failed; + } + } + + si = bmap->parent->parent_si; + +if (bmap->peb_index >= si->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + bmap->peb_index, si->pebs_count); +return -ERANGE ; + } + + pebc = &si->peb_array[bmap->peb_index]; + +down_write (&bmap->lock); + + buffers_state =atomic_read (&bmap->buffers_state); + +switch (buffers_state) { +case SSDFS_PEB_BMAP1_SRC_PEB_BMAP2_DST : +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap->src || !bmap->dst); +#endif /* CONFIG_SSDFS_DEBUG */ + new_buffers_state =SSDFS_PEB_BMAP2_SRC ; + buffer_index =SSDFS_PEB_BLK_BMAP2 ; +break ; + +case SSDFS_PEB_BMAP2_SRC_PEB_BMAP1_DST : +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap->src || !bmap->dst); +#endif /* CONFIG_SSDFS_DEBUG */ + new_buffers_state =SSDFS_PEB_BMAP1_SRC ; + buffer_index =SSDFS_PEB_BLK_BMAP1 ; +break ; + +default : + err = -ERANGE ; +SSDFS_WARN ("fail to start migration: " +"buffers_state %#x\n" , + buffers_state); + goto finish_migration_stop; + } + +#ifdef CONFIG_SSDFS_DEBUG + err =ssdfs_block_bmap_lock (bmap->dst); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_migration_stop; + } + + err =ssdfs_block_bmap_get_free_pages (bmap->dst); +if (err <0 ) { +SSDFS_ERR ("fail to get free pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_dst_bmap; + }else { + free_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_used_pages (bmap->dst); +if (err <0 ) { +SSDFS_ERR ("fail to get used pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_dst_bmap; + }else { + used_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_invalid_pages (bmap->dst); +if (err <0 ) { +SSDFS_ERR ("fail to get invalid pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_dst_bmap; + }else { + invalid_blks = err; + err =0 ; + } + +unlock_dst_bmap: +ssdfs_block_bmap_unlock (bmap->dst); + +if (unlikely (err)) + goto finish_migration_stop; + +SSDFS_DBG ("DST: free_blks %d, used_blks %d, invalid_blks %d\n" , + free_blks, used_blks, invalid_blks); + + err =ssdfs_block_bmap_lock (bmap->src); +if (unlikely (err)) { +SSDFS_ERR ("fail to lock block bitmap: err %d\n" , err); + goto finish_migration_stop; + } + + err =ssdfs_block_bmap_get_free_pages (bmap->src); +if (err <0 ) { +SSDFS_ERR ("fail to get free pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_src_bmap; + }else { + free_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_used_pages (bmap->src); +if (err <0 ) { +SSDFS_ERR ("fail to get used pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_src_bmap; + }else { + used_blks = err; + err =0 ; + } + + err =ssdfs_block_bmap_get_invalid_pages (bmap->src); +if (err <0 ) { +SSDFS_ERR ("fail to get invalid pages count: " +"peb_index %u, err %d\n" , + bmap->peb_index, err); + goto unlock_src_bmap; + }else { + invalid_blks = err; + err =0 ; + } + +unlock_src_bmap: +ssdfs_block_bmap_unlock (bmap->src); + +if (unlikely (err)) + goto finish_migration_stop; + +SSDFS_DBG ("SRC: free_blks %d, used_blks %d, invalid_blks %d\n" , + free_blks, used_blks, invalid_blks); + +if ((free_blks + used_blks + invalid_blks) > bmap->pages_per_peb) { +SSDFS_WARN ("free_blks %d, used_blks %d, " +"invalid_blks %d, pages_per_peb %u\n" , + free_blks, used_blks, invalid_blks, + bmap->pages_per_peb); + err = -ERANGE ; + goto finish_migration_stop; + } + +if (used_blks !=0 ) { +SSDFS_ERR ("PEB contains valid blocks %d\n" , + used_blks); + err = -ERANGE ; + goto finish_migration_stop; + } + +SSDFS_DBG ("shared_free_dst_blks %d, pages_per_peb %u\n" , +atomic_read (&pebc->shared_free_dst_blks), + bmap->pages_per_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_block_bmap_clear_dirty_state (bmap->src); + + bmap->src = &bmap->buffer[buffer_index]; + bmap->dst =NULL ; +atomic_set (&bmap->buffers_state, new_buffers_state); + +finish_migration_stop: +up_write (&bmap->lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("finished\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +}
Signature · 1 line
2.34.1
RFC PATCH 15/76 ssdfs: introduce segment block bitmap
In reply to Viacheslav DubeykoSSDFS splits a partition/volume on sequence of fixed-sized segments. Every segment can include one or several Logical Erase Blocks (LEB). LEB can be mapped into "Physical" Erase Block (PEB). PEB block bitmap object represents the aggregation of source PEB's block bitmap and destination PEB's block bitmap. Finally, segment block bitmap implements an array of PEB block bitmaps. Segment block bitmap has API:
(1) create - create segment block bitmap
(2) destroy - destroy segment block bitmap
(3) partial_init - initialize by state of one PEB block bitmap
(4) get_free_pages - get free pages in segment block bitmap
(5) get_used_pages - get used pages in segment block bitmap
(6) get_invalid_pages - get invalid pages in segment block bitmap
(7) reserve_block - reserve a free block
(8) reserved_extent - reserve some number of free blocks
(9) pre_allocate - pre_allocate page/range in segment block bitmap
(10) allocate - allocate page/range in segment block bitmap
(11) update_range - change the state of range in segment block bitmap
--- fs/ssdfs/segment_block_bitmap.c | 1425 +++++++++++++++++++++++++++++++ fs/ssdfs/segment_block_bitmap.h | 205 +++++ 2 files changed, 1630 insertions(+) create mode 100644 fs/ssdfs/segment_block_bitmap.c create mode 100644 fs/ssdfs/segment_block_bitmap.h
fs/ssdfs/segment_block_bitmap.c new file +1425 -0
@@ -0,0 +1,1425 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/segment_block_bitmap.c - segment's block bitmap implementation. +* +* 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/ +* +* (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 +*/ + +#include <linux/pagemap.h> +#include <linux/slab.h> +#include <linux/pagevec.h> + +#include "peb_mapping_queue.h" +#include "peb_mapping_table_cache.h" +#include "ssdfs.h" +#include "page_vector.h" +#include "peb_block_bitmap.h" +#include "segment_block_bitmap.h" +#include "offset_translation_table.h" +#include "page_array.h" +#include "peb_container.h" +#include "segment_bitmap.h" +#include "page_array.h" +#include "segment.h" + +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_t ssdfs_seg_blk_page_leaks; +atomic64_t ssdfs_seg_blk_memory_leaks; +atomic64_t ssdfs_seg_blk_cache_leaks; +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ + +/* +* void ssdfs_seg_blk_cache_leaks_increment(void *kaddr) +* void ssdfs_seg_blk_cache_leaks_decrement(void *kaddr) +* void *ssdfs_seg_blk_kmalloc(size_t size, gfp_t flags) +* void *ssdfs_seg_blk_kzalloc(size_t size, gfp_t flags) +* void *ssdfs_seg_blk_kcalloc(size_t n, size_t size, gfp_t flags) +* void ssdfs_seg_blk_kfree(void *kaddr) +* struct page *ssdfs_seg_blk_alloc_page(gfp_t gfp_mask) +* struct page *ssdfs_seg_blk_add_pagevec_page(struct pagevec *pvec) +* void ssdfs_seg_blk_free_page(struct page *page) +* void ssdfs_seg_blk_pagevec_release(struct pagevec *pvec) +*/ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING + SSDFS_MEMORY_LEAKS_CHECKER_FNS(seg_blk) +#else + SSDFS_MEMORY_ALLOCATOR_FNS(seg_blk) +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ + +void ssdfs_seg_blk_memory_leaks_init (void ) +{ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_set (&ssdfs_seg_blk_page_leaks,0 ); +atomic64_set (&ssdfs_seg_blk_memory_leaks,0 ); +atomic64_set (&ssdfs_seg_blk_cache_leaks,0 ); +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +} + +void ssdfs_seg_blk_check_memory_leaks (void ) +{ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +if (atomic64_read (&ssdfs_seg_blk_page_leaks) !=0 ) { +SSDFS_ERR ("SEGMENT BLOCK BITMAP: " +"memory leaks include %lld pages\n" , +atomic64_read (&ssdfs_seg_blk_page_leaks)); + } + +if (atomic64_read (&ssdfs_seg_blk_memory_leaks) !=0 ) { +SSDFS_ERR ("SEGMENT BLOCK BITMAP: " +"memory allocator suffers from %lld leaks\n" , +atomic64_read (&ssdfs_seg_blk_memory_leaks)); + } + +if (atomic64_read (&ssdfs_seg_blk_cache_leaks) !=0 ) { +SSDFS_ERR ("SEGMENT BLOCK BITMAP: " +"caches suffers from %lld leaks\n" , +atomic64_read (&ssdfs_seg_blk_cache_leaks)); + } +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +} + +#define SSDFS_SEG_BLK_BMAP_STATE_FNS (value, name) \ +static inline \ +bool is_seg_block_bmap_##name(struct ssdfs_segment_blk_bmap *bmap) \ +{ \ + return atomic_read(&bmap->state) == SSDFS_SEG_BLK_BMAP_##value; \ +} \ +static inline \ +void set_seg_block_bmap_##name(struct ssdfs_segment_blk_bmap *bmap) \ +{ \ + atomic_set(&bmap->state, SSDFS_SEG_BLK_BMAP_##value); \ +} \ + +/* +* is_seg_block_bmap_created() +* set_seg_block_bmap_created() +*/ +SSDFS_SEG_BLK_BMAP_STATE_FNS (CREATED , created) + +/* +* ssdfs_segment_blk_bmap_create() - create segment block bitmap +* @si: segment object +* @init_flag: definition of block bitmap's creation state +* @init_state: block state is used during initialization +* +* This method tries to create segment block bitmap. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ENOMEM - unable to allocate memory. +* %-ERANGE - internal error. + */ +int ssdfs_segment_blk_bmap_create (struct ssdfs_segment_info *si, +int init_flag,int init_state) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_segment_blk_bmap *bmap; +int i; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!si || !si->fsi); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("si %p, seg_id %llu, " +"init_flag %#x, init_state %#x\n" , + si, si->seg_id, + init_flag, init_state); +#else +SSDFS_DBG ("si %p, seg_id %llu, " +"init_flag %#x, init_state %#x\n" , + si, si->seg_id, + init_flag, init_state); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + + fsi = si->fsi; + bmap = &si->blk_bmap; + + bmap->parent_si = si; +atomic_set (&bmap->state,SSDFS_SEG_BLK_BMAP_STATE_UNKNOWN ); + + bmap->pages_per_peb = fsi->pages_per_peb; + bmap->pages_per_seg = fsi->pages_per_seg; + +init_rwsem (&bmap->modification_lock); +atomic_set (&bmap->seg_valid_blks,0 ); +atomic_set (&bmap->seg_invalid_blks,0 ); +atomic_set (&bmap->seg_free_blks,0 ); + + bmap->pebs_count = si->pebs_count; + + bmap->peb =ssdfs_seg_blk_kcalloc (bmap->pebs_count, +sizeof (struct ssdfs_peb_blk_bmap ), +GFP_KERNEL ); +if (!bmap->peb) { +SSDFS_ERR ("fail to allocate PEBs' block bitmaps\n" ); +return -ENOMEM ; + } + +for (i =0 ; i < bmap->pebs_count; i++) { + err =ssdfs_peb_blk_bmap_create (bmap, i, fsi->pages_per_peb, + init_flag, init_state); +if (unlikely (err)) { +SSDFS_ERR ("fail to create PEB's block bitmap: " +"peb_index %u, err %d\n" , + i, err); + goto fail_create_seg_blk_bmap; + } + } + +set_seg_block_bmap_created (bmap); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#else +SSDFS_DBG ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +return 0 ; + +fail_create_seg_blk_bmap: +ssdfs_segment_blk_bmap_destroy (bmap); +return err; +} + +/* +* ssdfs_segment_blk_bmap_destroy() - destroy segment block bitmap +* @ptr: segment block bitmap pointer +*/ +void ssdfs_segment_blk_bmap_destroy (struct ssdfs_segment_blk_bmap *ptr) +{ +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ptr); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ptr->parent_si) { +/* object is not created yet */ +return ; + } + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("seg_id %llu, state %#x\n" , + ptr->parent_si->seg_id, +atomic_read (&ptr->state)); +#else +SSDFS_DBG ("seg_id %llu, state %#x\n" , + ptr->parent_si->seg_id, +atomic_read (&ptr->state)); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +atomic_set (&ptr->seg_valid_blks,0 ); +atomic_set (&ptr->seg_invalid_blks,0 ); +atomic_set (&ptr->seg_free_blks,0 ); + +for (i =0 ; i < ptr->pebs_count; i++) +ssdfs_peb_blk_bmap_destroy (&ptr->peb[i]); + +ssdfs_seg_blk_kfree (ptr->peb); + ptr->peb =NULL ; + +atomic_set (&ptr->state,SSDFS_SEG_BLK_BMAP_STATE_UNKNOWN ); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#else +SSDFS_DBG ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ +} + +/* +* ssdfs_segment_blk_bmap_partial_init() - partial init of segment bitmap +* @bmap: pointer on segment block bitmap +* @peb_index: PEB's index +* @source: pointer on pagevec with bitmap state +* @hdr: header of block bitmap fragment +* @cno: log's checkpoint +*/ +int ssdfs_segment_blk_bmap_partial_init (struct ssdfs_segment_blk_bmap *bmap, +u16 peb_index, +struct ssdfs_page_vector *source, +struct ssdfs_block_bitmap_fragment *hdr, +u64 cno) +{ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !bmap->peb || !bmap->parent_si); +BUG_ON (!source || !hdr); +BUG_ON (ssdfs_page_vector_count (source) ==0 ); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("seg_id %llu, peb_index %u, cno %llu\n" , + bmap->parent_si->seg_id, peb_index, cno); +#else +SSDFS_DBG ("seg_id %llu, peb_index %u, cno %llu\n" , + bmap->parent_si->seg_id, peb_index, cno); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +if (atomic_read (&bmap->state) !=SSDFS_SEG_BLK_BMAP_CREATED ) { +SSDFS_ERR ("invalid segment block bitmap state %#x\n" , +atomic_read (&bmap->state)); +return -ERANGE ; + } + +if (peb_index >= bmap->pebs_count) { +SSDFS_ERR ("peb_index %u >= seg_blkbmap->pebs_count %u\n" , + peb_index, bmap->pebs_count); +return -ERANGE ; + } + + err =ssdfs_peb_blk_bmap_init (&bmap->peb[peb_index], + source, hdr, cno); + +#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_segment_blk_bmap_init_failed() - process failure of segment bitmap init +* @bmap: pointer on segment block bitmap +* @peb_index: PEB's index +*/ +void ssdfs_segment_blk_bmap_init_failed (struct ssdfs_segment_blk_bmap *bmap, +u16 peb_index) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !bmap->peb); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (peb_index >= bmap->pebs_count) { +SSDFS_WARN ("peb_index %u >= seg_blkbmap->pebs_count %u\n" , + peb_index, bmap->pebs_count); +return ; + } + +ssdfs_peb_blk_bmap_init_failed (&bmap->peb[peb_index]); +} + +/* +* is_ssdfs_segment_blk_bmap_dirty() - check that PEB block bitmap is dirty +* @bmap: pointer on segment block bitmap +* @peb_index: PEB's index +*/ +bool is_ssdfs_segment_blk_bmap_dirty (struct ssdfs_segment_blk_bmap *bmap, +u16 peb_index) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !bmap->peb); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (peb_index >= bmap->pebs_count) { +SSDFS_WARN ("peb_index %u >= seg_blkbmap->pebs_count %u\n" , + peb_index, bmap->pebs_count); +return false; + } + +return is_ssdfs_peb_blk_bmap_dirty (&bmap->peb[peb_index]); +} + +/* +* ssdfs_define_bmap_index() - define block bitmap for operation +* @pebc: pointer on PEB container +* @bmap_index: pointer on block bitmap index value [out] +* @peb_index: pointer on PEB's index [out] +* +* This method tries to define bitmap index and PEB's index +* for operation with block bitmap. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +static +int ssdfs_define_bmap_index (struct ssdfs_peb_container *pebc, +int *bmap_index,u16 *peb_index) +{ +struct ssdfs_segment_info *si; +int migration_state, items_state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc || !pebc->parent_si); +BUG_ON (!bmap_index || !peb_index); +BUG_ON (!rwsem_is_locked (&pebc->lock)); +BUG_ON (!mutex_is_locked (&pebc->migration_lock)); + +SSDFS_DBG ("seg_id %llu, peb_index %u\n" , + pebc->parent_si->seg_id, + pebc->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + + si = pebc->parent_si; + *bmap_index =-1 ; + *peb_index =U16_MAX ; + +try_define_bmap_index: + migration_state =atomic_read (&pebc->migration_state); + items_state =atomic_read (&pebc->items_state); +switch (migration_state) { +case SSDFS_PEB_NOT_MIGRATING : + *bmap_index =SSDFS_PEB_BLK_BMAP_SOURCE ; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc->src_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + *peb_index = pebc->src_peb->peb_index; +break ; + +case SSDFS_PEB_UNDER_MIGRATION : +switch (items_state) { +case SSDFS_PEB1_DST_CONTAINER : +case SSDFS_PEB2_DST_CONTAINER : +case SSDFS_PEB1_SRC_EXT_PTR_DST_CONTAINER : +case SSDFS_PEB2_SRC_EXT_PTR_DST_CONTAINER : + *bmap_index =SSDFS_PEB_BLK_BMAP_SOURCE ; +break ; + +case SSDFS_PEB1_SRC_PEB2_DST_CONTAINER : +case SSDFS_PEB2_SRC_PEB1_DST_CONTAINER : +switch (atomic_read (&pebc->migration_phase)) { +case SSDFS_SRC_PEB_NOT_EXHAUSTED : + *bmap_index =SSDFS_PEB_BLK_BMAP_SOURCE ; +break ; + +default : + *bmap_index =SSDFS_PEB_BLK_BMAP_DESTINATION ; +break ; + } +break ; + +default : + err = -ERANGE ; +SSDFS_WARN ("invalid items_state %#x\n" , + items_state); + goto finish_define_bmap_index; + }; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc->dst_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + *peb_index = pebc->dst_peb->peb_index; +break ; + +case SSDFS_PEB_MIGRATION_PREPARATION : +case SSDFS_PEB_RELATION_PREPARATION : +case SSDFS_PEB_FINISHING_MIGRATION : +#ifdef CONFIG_SSDFS_DEBUG +/* unexpected situation */ +SSDFS_WARN ("unexpected situation\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + err = -EAGAIN ; + goto finish_define_bmap_index; +break ; + +default : + err = -ERANGE ; +SSDFS_WARN ("invalid migration_state %#x\n" , + migration_state); + goto finish_define_bmap_index; + } + +finish_define_bmap_index: +if (err == -EAGAIN ) { +DEFINE_WAIT (wait); + + err =0 ; + +mutex_unlock (&pebc->migration_lock); +up_read (&pebc->lock); +prepare_to_wait (&pebc->migration_wq, &wait, +TASK_UNINTERRUPTIBLE ); +schedule (); +finish_wait (&pebc->migration_wq, &wait); +down_read (&pebc->lock); +mutex_lock (&pebc->migration_lock); + goto try_define_bmap_index; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to define bmap_index: " +"seg %llu, peb_index %u, err %d\n" , + pebc->parent_si->seg_id, + pebc->peb_index, err); +return err; + } + +return 0 ; +} + +bool has_ssdfs_segment_blk_bmap_initialized (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_container *pebc) +{ +struct ssdfs_peb_blk_bmap *peb_blkbmap; +u16 peb_index; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ptr || !ptr->peb || !ptr->parent_si || !pebc); + +SSDFS_DBG ("seg_id %llu, peb_index %u\n" , + ptr->parent_si->seg_id, + pebc->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (atomic_read (&ptr->state) !=SSDFS_SEG_BLK_BMAP_CREATED ) { +SSDFS_ERR ("invalid segment block bitmap state %#x\n" , +atomic_read (&ptr->state)); +return false; + } + +down_read (&pebc->lock); +if (pebc->dst_peb) + peb_index = pebc->dst_peb->peb_index; +else + peb_index = pebc->src_peb->peb_index; +up_read (&pebc->lock); + +if (peb_index >= ptr->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + peb_index, ptr->pebs_count); +return false; + } + + peb_blkbmap = &ptr->peb[peb_index]; + +return has_ssdfs_peb_blk_bmap_initialized (peb_blkbmap); +} + +int ssdfs_segment_blk_bmap_wait_init_end (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_container *pebc) +{ +struct ssdfs_peb_blk_bmap *peb_blkbmap; +u16 peb_index; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ptr || !ptr->peb || !ptr->parent_si || !pebc); + +SSDFS_DBG ("seg_id %llu, peb_index %u\n" , + ptr->parent_si->seg_id, + pebc->peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (atomic_read (&ptr->state) !=SSDFS_SEG_BLK_BMAP_CREATED ) { +SSDFS_ERR ("invalid segment block bitmap state %#x\n" , +atomic_read (&ptr->state)); +return -ERANGE ; + } + +down_read (&pebc->lock); +if (pebc->dst_peb) + peb_index = pebc->dst_peb->peb_index; +else + peb_index = pebc->src_peb->peb_index; +up_read (&pebc->lock); + +if (peb_index >= ptr->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + peb_index, ptr->pebs_count); +return -ERANGE ; + } + + peb_blkbmap = &ptr->peb[peb_index]; + +return ssdfs_peb_blk_bmap_wait_init_end (peb_blkbmap); +} + +/* +* ssdfs_segment_blk_bmap_reserve_metapages() - reserve metapages +* @ptr: segment block bitmap object +* @pebc: pointer on PEB container +* @count: amount of metadata pages for reservation +* +* This method tries to reserve @count metadata pages into +* block bitmap. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_segment_blk_bmap_reserve_metapages (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_container *pebc, +u32 count) +{ +struct ssdfs_peb_blk_bmap *peb_blkbmap; +int bmap_index =SSDFS_PEB_BLK_BMAP_INDEX_MAX ; +u16 peb_index; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ptr || !ptr->peb || !ptr->parent_si || !pebc); +BUG_ON (!rwsem_is_locked (&pebc->lock)); + +SSDFS_DBG ("seg_id %llu, peb_index %u, count %u\n" , + ptr->parent_si->seg_id, + pebc->peb_index, count); +SSDFS_DBG ("free_logical_blks %d, valid_logical_blks %d, " +"invalid_logical_blks %d, pages_per_seg %u\n" , +atomic_read (&ptr->seg_free_blks), +atomic_read (&ptr->seg_valid_blks), +atomic_read (&ptr->seg_invalid_blks), + ptr->pages_per_seg); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (atomic_read (&ptr->state) !=SSDFS_SEG_BLK_BMAP_CREATED ) { +SSDFS_ERR ("invalid segment block bitmap state %#x\n" , +atomic_read (&ptr->state)); +return -ERANGE ; + } + + err =ssdfs_define_bmap_index (pebc, &bmap_index, &peb_index); +if (unlikely (err)) { +SSDFS_ERR ("fail to define bmap_index: " +"seg %llu, peb_index %u, err %d\n" , + ptr->parent_si->seg_id, + pebc->peb_index, err); +return err; + } + +if (peb_index >= ptr->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + peb_index, ptr->pebs_count); +return -ERANGE ; + } + + peb_blkbmap = &ptr->peb[peb_index]; + +return ssdfs_peb_blk_bmap_reserve_metapages (peb_blkbmap, + bmap_index, + count); +} + +/* +* ssdfs_segment_blk_bmap_free_metapages() - free metapages +* @ptr: segment block bitmap object +* @pebc: pointer on PEB container +* @count: amount of metadata pages for freeing +* +* This method tries to free @count metadata pages into +* block bitmap. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_segment_blk_bmap_free_metapages (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_container *pebc, +u32 count) +{ +struct ssdfs_peb_blk_bmap *peb_blkbmap; +int bmap_index =SSDFS_PEB_BLK_BMAP_INDEX_MAX ; +u16 peb_index; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ptr || !ptr->peb || !ptr->parent_si || !pebc); +BUG_ON (!rwsem_is_locked (&pebc->lock)); + +SSDFS_DBG ("seg_id %llu, peb_index %u, count %u\n" , + ptr->parent_si->seg_id, + pebc->peb_index, count); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (atomic_read (&ptr->state) !=SSDFS_SEG_BLK_BMAP_CREATED ) { +SSDFS_ERR ("invalid segment block bitmap state %#x\n" , +atomic_read (&ptr->state)); +return -ERANGE ; + } + + err =ssdfs_define_bmap_index (pebc, &bmap_index, &peb_index); +if (unlikely (err)) { +SSDFS_ERR ("fail to define bmap_index: " +"seg %llu, peb_index %u, err %d\n" , + ptr->parent_si->seg_id, + pebc->peb_index, err); +return err; + } + +if (peb_index >= ptr->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + peb_index, ptr->pebs_count); +return -ERANGE ; + } + + peb_blkbmap = &ptr->peb[peb_index]; + +return ssdfs_peb_blk_bmap_free_metapages (peb_blkbmap, + bmap_index, + count); +} + +/* +* ssdfs_segment_blk_bmap_reserve_extent() - reserve free extent +* @ptr: segment block bitmap object +* @count: number of logical blocks +* +* This function tries to reserve some number of free blocks. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-E2BIG - segment hasn't enough free space. +*/ +int ssdfs_segment_blk_bmap_reserve_extent (struct ssdfs_segment_blk_bmap *ptr, +u32 count) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_segment_info *si; +int free_blks; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ptr || !ptr->peb || !ptr->parent_si); + +SSDFS_DBG ("seg_id %llu\n" , + ptr->parent_si->seg_id); +SSDFS_DBG ("BEFORE: free_logical_blks %d, valid_logical_blks %d, " +"invalid_logical_blks %d, pages_per_seg %u\n" , +atomic_read (&ptr->seg_free_blks), +atomic_read (&ptr->seg_valid_blks), +atomic_read (&ptr->seg_invalid_blks), + ptr->pages_per_seg); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (atomic_read (&ptr->state) !=SSDFS_SEG_BLK_BMAP_CREATED ) { +SSDFS_ERR ("invalid segment block bitmap state %#x\n" , +atomic_read (&ptr->state)); +return -ERANGE ; + } + +down_read (&ptr->modification_lock); + + free_blks =atomic_read (&ptr->seg_free_blks); + +if (free_blks < count) { + err = -E2BIG ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("segment %llu hasn't enough free pages: " +"free_pages %u, requested_pages %u\n" , + ptr->parent_si->seg_id, free_blks, count); +#endif /* CONFIG_SSDFS_DEBUG */ + +atomic_set (&ptr->seg_free_blks,0 ); + }else { +atomic_sub (count, &ptr->seg_free_blks); + } + +up_read (&ptr->modification_lock); + +if (err) + goto finish_reserve_extent; + + si = ptr->parent_si; + fsi = si->fsi; + +if (si->seg_type ==SSDFS_USER_DATA_SEG_TYPE ) { +u64 reserved =0 ; +u32 pending =0 ; + +spin_lock (&fsi->volume_state_lock); + reserved = fsi->reserved_new_user_data_pages; +if (fsi->reserved_new_user_data_pages >= count) { + fsi->reserved_new_user_data_pages -= count; + }else + err = -ERANGE ; +spin_unlock (&fsi->volume_state_lock); + +if (err) { +SSDFS_ERR ("count %u is bigger than reserved %llu\n" , + count, reserved); + goto finish_reserve_extent; + } + +spin_lock (&si->pending_lock); + si->pending_new_user_data_pages += count; + pending = si->pending_new_user_data_pages; +spin_unlock (&si->pending_lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("seg_id %llu, pending %u\n" , + si->seg_id, pending); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("AFTER: free_logical_blks %d, valid_logical_blks %d, " +"invalid_logical_blks %d, pages_per_seg %u\n" , +atomic_read (&ptr->seg_free_blks), +atomic_read (&ptr->seg_valid_blks), +atomic_read (&ptr->seg_invalid_blks), + ptr->pages_per_seg); +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_reserve_extent: +return err; +} + +/* +* ssdfs_segment_blk_bmap_reserve_block() - reserve free block +* @ptr: segment block bitmap object +* @count: number of logical blocks +* +* This function tries to reserve a free block. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +* %-E2BIG - segment hasn't enough free space. +*/ +int ssdfs_segment_blk_bmap_reserve_block (struct ssdfs_segment_blk_bmap *ptr) +{ +return ssdfs_segment_blk_bmap_reserve_extent (ptr,1 ); +} + +/* +* ssdfs_segment_blk_bmap_pre_allocate() - pre-allocate range of blocks +* @ptr: segment block bitmap object +* @pebc: pointer on PEB container +* @len: pointer on variable with requested length of range +* @range: pointer on blocks' range [in | out] +* +* This function tries to find contiguous range of free blocks and +* to set the found range in pre-allocated state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_segment_blk_bmap_pre_allocate (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_container *pebc, +u32 *len, +struct ssdfs_block_bmap_range *range) +{ +struct ssdfs_peb_blk_bmap *peb_blkbmap; +int bmap_index =SSDFS_PEB_BLK_BMAP_INDEX_MAX ; +u16 peb_index; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ptr || !ptr->peb || !ptr->parent_si || !pebc); +BUG_ON (!rwsem_is_locked (&pebc->lock)); + +SSDFS_DBG ("seg_id %llu, peb_index %u\n" , + ptr->parent_si->seg_id, + pebc->peb_index); +SSDFS_DBG ("free_logical_blks %d, valid_logical_blks %d, " +"invalid_logical_blks %d, pages_per_seg %u\n" , +atomic_read (&ptr->seg_free_blks), +atomic_read (&ptr->seg_valid_blks), +atomic_read (&ptr->seg_invalid_blks), + ptr->pages_per_seg); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (atomic_read (&ptr->state) !=SSDFS_SEG_BLK_BMAP_CREATED ) { +SSDFS_ERR ("invalid segment block bitmap state %#x\n" , +atomic_read (&ptr->state)); +return -ERANGE ; + } + + err =ssdfs_define_bmap_index (pebc, &bmap_index, &peb_index); +if (unlikely (err)) { +SSDFS_ERR ("fail to define bmap_index: " +"seg %llu, peb_index %u, err %d\n" , + ptr->parent_si->seg_id, + pebc->peb_index, err); +return err; + } + +if (peb_index >= ptr->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + peb_index, ptr->pebs_count); +return -ERANGE ; + } + + peb_blkbmap = &ptr->peb[peb_index]; + +return ssdfs_peb_blk_bmap_pre_allocate (peb_blkbmap, bmap_index, + len, range); +} + +/* +* ssdfs_segment_blk_bmap_allocate() - allocate range of blocks +* @ptr: segment block bitmap object +* @pebc: pointer on PEB container +* @len: pointer on variable with requested length of range +* @range: pointer on blocks' range [in | out] +* +* This function tries to find contiguous range of free blocks and +* to set the found range in allocated state. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_segment_blk_bmap_allocate (struct ssdfs_segment_blk_bmap *ptr, +struct ssdfs_peb_container *pebc, +u32 *len, +struct ssdfs_block_bmap_range *range) +{ +struct ssdfs_peb_blk_bmap *peb_blkbmap; +int bmap_index =SSDFS_PEB_BLK_BMAP_INDEX_MAX ; +u16 peb_index; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ptr || !ptr->peb || !ptr->parent_si || !pebc); +BUG_ON (!rwsem_is_locked (&pebc->lock)); + +SSDFS_DBG ("seg_id %llu, peb_index %u\n" , + ptr->parent_si->seg_id, + pebc->peb_index); +SSDFS_DBG ("free_logical_blks %d, valid_logical_blks %d, " +"invalid_logical_blks %d, pages_per_seg %u\n" , +atomic_read (&ptr->seg_free_blks), +atomic_read (&ptr->seg_valid_blks), +atomic_read (&ptr->seg_invalid_blks), + ptr->pages_per_seg); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (atomic_read (&ptr->state) !=SSDFS_SEG_BLK_BMAP_CREATED ) { +SSDFS_ERR ("invalid segment block bitmap state %#x\n" , +atomic_read (&ptr->state)); +return -ERANGE ; + } + + err =ssdfs_define_bmap_index (pebc, &bmap_index, &peb_index); +if (unlikely (err)) { +SSDFS_ERR ("fail to define bmap_index: " +"seg %llu, peb_index %u, err %d\n" , + ptr->parent_si->seg_id, + pebc->peb_index, err); +return err; + } + +if (peb_index >= ptr->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + peb_index, ptr->pebs_count); +return -ERANGE ; + } + + peb_blkbmap = &ptr->peb[peb_index]; + +return ssdfs_peb_blk_bmap_allocate (peb_blkbmap, bmap_index, + len, range); +} + +/* +* ssdfs_segment_blk_bmap_update_range() - update range of blocks' state +* @ptr: segment block bitmap object +* @pebc: pointer on PEB container +* @peb_migration_id: migration_id of PEB +* @range_state: new state of range +* @range: pointer on blocks' range [in | out] +* +* This function tries to change state of @range. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal error. +*/ +int ssdfs_segment_blk_bmap_update_range (struct ssdfs_segment_blk_bmap *bmap, +struct ssdfs_peb_container *pebc, +u8 peb_migration_id, +int range_state, +struct ssdfs_block_bmap_range *range) +{ +struct ssdfs_segment_info *si; +struct ssdfs_peb_container *dst_pebc; +struct ssdfs_peb_blk_bmap *dst_blkbmap; +int bmap_index =SSDFS_PEB_BLK_BMAP_INDEX_MAX ; +u16 peb_index; +int migration_state, migration_phase, items_state; +bool need_migrate = false; +bool need_move = false; +int src_migration_id =-1 , dst_migration_id =-1 ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!bmap || !bmap->peb || !bmap->parent_si); +BUG_ON (!pebc || !range); +BUG_ON (!rwsem_is_locked (&pebc->lock)); +BUG_ON (!mutex_is_locked (&pebc->migration_lock)); + +SSDFS_DBG ("seg_id %llu, peb_index %u, peb_migration_id %u, " +"range (start %u, len %u)\n" , + bmap->parent_si->seg_id, pebc->peb_index, + peb_migration_id, range->start, range->len); +#endif /* CONFIG_SSDFS_DEBUG */ + + si = pebc->parent_si; + +try_define_bmap_index: + migration_state =atomic_read (&pebc->migration_state); + migration_phase =atomic_read (&pebc->migration_phase); + items_state =atomic_read (&pebc->items_state); +switch (migration_state) { +case SSDFS_PEB_NOT_MIGRATING : + need_migrate = false; + need_move = false; + bmap_index =SSDFS_PEB_BLK_BMAP_SOURCE ; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc->src_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + + src_migration_id = +ssdfs_get_peb_migration_id_checked (pebc->src_peb); +if (unlikely (src_migration_id <0 )) { + err = src_migration_id; +SSDFS_ERR ("invalid peb_migration_id: " +"err %d\n" , + err); + goto finish_define_bmap_index; + } + +if (peb_migration_id > src_migration_id) { + err = -ERANGE ; +SSDFS_ERR ("migration_id %u > src_migration_id %u\n" , + peb_migration_id, + src_migration_id); + goto finish_define_bmap_index; + } + peb_index = pebc->src_peb->peb_index; +break ; + +case SSDFS_PEB_UNDER_MIGRATION : +switch (items_state) { +case SSDFS_PEB1_DST_CONTAINER : +case SSDFS_PEB2_DST_CONTAINER : + need_migrate = false; + need_move = false; + bmap_index =SSDFS_PEB_BLK_BMAP_SOURCE ; +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc->dst_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + + dst_migration_id = +ssdfs_get_peb_migration_id_checked (pebc->dst_peb); +if (unlikely (dst_migration_id <0 )) { + err = dst_migration_id; +SSDFS_ERR ("invalid peb_migration_id: " +"err %d\n" , + err); + goto finish_define_bmap_index; + } + +if (peb_migration_id != dst_migration_id) { + err = -ERANGE ; +SSDFS_ERR ("migration_id %u != " +"dst_migration_id %u\n" , + peb_migration_id, + dst_migration_id); + goto finish_define_bmap_index; + } + peb_index = pebc->dst_peb->peb_index; +break ; + +case SSDFS_PEB1_SRC_EXT_PTR_DST_CONTAINER : +case SSDFS_PEB2_SRC_EXT_PTR_DST_CONTAINER : +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc->src_peb || !pebc->dst_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + + src_migration_id = +ssdfs_get_peb_migration_id_checked (pebc->src_peb); +if (unlikely (src_migration_id <0 )) { + err = src_migration_id; +SSDFS_ERR ("invalid peb_migration_id: " +"err %d\n" , + err); + goto finish_define_bmap_index; + } + + dst_migration_id = +ssdfs_get_peb_migration_id_checked (pebc->dst_peb); +if (unlikely (dst_migration_id <0 )) { + err = dst_migration_id; +SSDFS_ERR ("invalid peb_migration_id: " +"err %d\n" , + err); + goto finish_define_bmap_index; + } + +if (src_migration_id == dst_migration_id) { + err = -ERANGE ; +SSDFS_ERR ("src_migration_id %u == " +"dst_migration_id %u\n" , + src_migration_id, + dst_migration_id); + goto finish_define_bmap_index; + } + +if (peb_migration_id == src_migration_id) { +int state; + + need_migrate = true; + need_move = false; + + dst_pebc = pebc->dst_peb->pebc; + state =atomic_read (&dst_pebc->items_state); +switch (state) { +case SSDFS_PEB1_DST_CONTAINER : +case SSDFS_PEB2_DST_CONTAINER : + bmap_index =SSDFS_PEB_BLK_BMAP_SOURCE ; +break ; + +case SSDFS_PEB1_SRC_PEB2_DST_CONTAINER : +case SSDFS_PEB2_SRC_PEB1_DST_CONTAINER : + bmap_index = +SSDFS_PEB_BLK_BMAP_DESTINATION ; +break ; + +default : +BUG (); + } + + peb_index =U16_MAX ; + }else if (peb_migration_id == dst_migration_id) { + err = -ERANGE ; +SSDFS_WARN ("invalid request: " +"peb_migration_id %u, " +"dst_migration_id %u\n" , + peb_migration_id, + dst_migration_id); + goto finish_define_bmap_index; + }else { + err = -ERANGE ; +SSDFS_ERR ("fail to select PEB: " +"peb_migration_id %u, " +"src_migration_id %u, " +"dst_migration_id %u\n" , + peb_migration_id, + src_migration_id, + dst_migration_id); + goto finish_define_bmap_index; + } +break ; + +case SSDFS_PEB1_SRC_PEB2_DST_CONTAINER : +case SSDFS_PEB2_SRC_PEB1_DST_CONTAINER : +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc->src_peb || !pebc->dst_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + + src_migration_id = +ssdfs_get_peb_migration_id_checked (pebc->src_peb); +if (unlikely (src_migration_id <0 )) { + err = src_migration_id; +SSDFS_ERR ("invalid peb_migration_id: " +"err %d\n" , + err); + goto finish_define_bmap_index; + } + + dst_migration_id = +ssdfs_get_peb_migration_id_checked (pebc->dst_peb); +if (unlikely (dst_migration_id <0 )) { + err = dst_migration_id; +SSDFS_ERR ("invalid peb_migration_id: " +"err %d\n" , + err); + goto finish_define_bmap_index; + } + +if (src_migration_id == dst_migration_id) { + err = -ERANGE ; +SSDFS_ERR ("src_migration_id %u == " +"dst_migration_id %u\n" , + src_migration_id, + dst_migration_id); + goto finish_define_bmap_index; + } + +if (peb_migration_id == src_migration_id) { +switch (migration_phase) { +case SSDFS_SRC_PEB_NOT_EXHAUSTED : + need_migrate = false; + need_move = false; + bmap_index = +SSDFS_PEB_BLK_BMAP_SOURCE ; + peb_index = pebc->src_peb->peb_index; +break ; + +default : + need_migrate = true; + need_move = false; + bmap_index = +SSDFS_PEB_BLK_BMAP_INDEX_MAX ; + peb_index = pebc->src_peb->peb_index; +break ; + } + }else if (peb_migration_id == dst_migration_id) { + need_migrate = false; + need_move = false; + bmap_index =SSDFS_PEB_BLK_BMAP_DESTINATION ; + peb_index = pebc->dst_peb->peb_index; + }else if ((peb_migration_id +1 ) == src_migration_id) { +switch (migration_phase) { +case SSDFS_SRC_PEB_NOT_EXHAUSTED : + need_migrate = false; + need_move = false; + bmap_index = +SSDFS_PEB_BLK_BMAP_SOURCE ; + peb_index = pebc->src_peb->peb_index; +break ; + +default : + need_migrate = false; + need_move = true; + bmap_index = +SSDFS_PEB_BLK_BMAP_DESTINATION ; + peb_index = pebc->dst_peb->peb_index; +break ; + } + }else { + err = -ERANGE ; +SSDFS_ERR ("fail to select PEB: " +"peb_migration_id %u, " +"src_migration_id %u, " +"dst_migration_id %u\n" , + peb_migration_id, + src_migration_id, + dst_migration_id); + goto finish_define_bmap_index; + } +break ; + +default : + err = -ERANGE ; +SSDFS_WARN ("invalid items_state %#x\n" , + items_state); + goto finish_define_bmap_index; + }; +break ; + +case SSDFS_PEB_MIGRATION_PREPARATION : +case SSDFS_PEB_RELATION_PREPARATION : +case SSDFS_PEB_FINISHING_MIGRATION : +#ifdef CONFIG_SSDFS_DEBUG +/* unexpected situation */ +SSDFS_WARN ("unexpected situation\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + err = -EAGAIN ; + goto finish_define_bmap_index; +break ; + +default : + err = -ERANGE ; +SSDFS_WARN ("invalid migration_state %#x\n" , + migration_state); + goto finish_define_bmap_index; + } + +finish_define_bmap_index: +if (err == -EAGAIN ) { +DEFINE_WAIT (wait); + + err =0 ; +mutex_unlock (&pebc->migration_lock); +up_read (&pebc->lock); +prepare_to_wait (&pebc->migration_wq, &wait, +TASK_UNINTERRUPTIBLE ); +schedule (); +finish_wait (&pebc->migration_wq, &wait); +down_read (&pebc->lock); +mutex_lock (&pebc->migration_lock); + goto try_define_bmap_index; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to define bmap_index: " +"seg %llu, peb_index %u, err %d\n" , + pebc->parent_si->seg_id, + pebc->peb_index, err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (need_migrate && need_move); + +SSDFS_DBG ("seg_id %llu, migration_state %#x, items_state %#x, " +"peb_migration_id %u, src_migration_id %d, " +"dst_migration_id %d, migration_phase %#x\n" , + si->seg_id, migration_state, items_state, + peb_migration_id, src_migration_id, + dst_migration_id, migration_phase); +SSDFS_DBG ("seg_id %llu, need_migrate %#x, need_move %#x\n" , + si->seg_id, need_migrate, need_move); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (need_migrate) { +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (peb_index >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (peb_index >= bmap->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + peb_index, bmap->pebs_count); +return -ERANGE ; + } + + dst_blkbmap = &bmap->peb[peb_index]; + + err =ssdfs_peb_blk_bmap_migrate (dst_blkbmap, + range_state, + range); +if (unlikely (err)) { +SSDFS_ERR ("fail to migrate: " +"range (start %u, len %u), " +"range_state %#x, " +"err %d\n" , + range->start, range->len, + range_state, err); +SSDFS_ERR ("seg_id %llu, peb_index %u, " +"peb_migration_id %u, " +"range (start %u, len %u)\n" , + bmap->parent_si->seg_id, + pebc->peb_index, + peb_migration_id, + range->start, range->len); +SSDFS_ERR ("seg_id %llu, migration_state %#x, " +"items_state %#x, " +"peb_migration_id %u, src_migration_id %d, " +"dst_migration_id %d, migration_phase %#x\n" , + si->seg_id, migration_state, items_state, + peb_migration_id, src_migration_id, + dst_migration_id, migration_phase); +SSDFS_ERR ("seg_id %llu, need_migrate %#x, " +"need_move %#x\n" , + si->seg_id, need_migrate, need_move); +return err; + } + }else if (need_move) { +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebc->dst_peb); +#endif /* CONFIG_SSDFS_DEBUG */ + + peb_index = pebc->dst_peb->peb_index; + +if (peb_index >= bmap->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + peb_index, bmap->pebs_count); +return -ERANGE ; + } + + dst_blkbmap = &bmap->peb[peb_index]; + +if (range_state ==SSDFS_BLK_PRE_ALLOCATED ) { + err =ssdfs_peb_blk_bmap_pre_allocate (dst_blkbmap, + bmap_index, +NULL , + range); + }else { + err =ssdfs_peb_blk_bmap_allocate (dst_blkbmap, + bmap_index, +NULL , + range); + } + +if (unlikely (err)) { +SSDFS_ERR ("fail to move: " +"range (start %u, len %u), " +"range_state %#x, " +"err %d\n" , + range->start, range->len, + range_state, err); +SSDFS_ERR ("seg_id %llu, peb_index %u, " +"peb_migration_id %u, " +"range (start %u, len %u)\n" , + bmap->parent_si->seg_id, + pebc->peb_index, + peb_migration_id, + range->start, range->len); +SSDFS_ERR ("seg_id %llu, migration_state %#x, " +"items_state %#x, " +"peb_migration_id %u, src_migration_id %d, " +"dst_migration_id %d, migration_phase %#x\n" , + si->seg_id, migration_state, items_state, + peb_migration_id, src_migration_id, + dst_migration_id, migration_phase); +SSDFS_ERR ("seg_id %llu, need_migrate %#x, " +"need_move %#x\n" , + si->seg_id, need_migrate, need_move); +return err; + } + }else { +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (peb_index >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (peb_index >= bmap->pebs_count) { +SSDFS_ERR ("peb_index %u >= pebs_count %u\n" , + peb_index, bmap->pebs_count); +return -ERANGE ; + } + + dst_blkbmap = &bmap->peb[peb_index]; + + err =ssdfs_peb_blk_bmap_update_range (dst_blkbmap, + bmap_index, + range_state, + range); +if (unlikely (err)) { +SSDFS_ERR ("fail to update range: " +"range (start %u, len %u), " +"range_state %#x, " +"err %d\n" , + range->start, range->len, + range_state, err); +SSDFS_ERR ("seg_id %llu, peb_index %u, " +"peb_migration_id %u, " +"range (start %u, len %u)\n" , + bmap->parent_si->seg_id, + pebc->peb_index, + peb_migration_id, + range->start, range->len); +SSDFS_ERR ("seg_id %llu, migration_state %#x, " +"items_state %#x, " +"peb_migration_id %u, src_migration_id %d, " +"dst_migration_id %d, migration_phase %#x\n" , + si->seg_id, migration_state, items_state, + peb_migration_id, src_migration_id, + dst_migration_id, migration_phase); +SSDFS_ERR ("seg_id %llu, need_migrate %#x, " +"need_move %#x\n" , + si->seg_id, need_migrate, need_move); +return err; + } + } + +return 0 ; +}
fs/ssdfs/segment_block_bitmap.h new file +205 -0
205 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 16/76 ssdfs: introduce segment request queue
In reply to Viacheslav DubeykoSSDFS implements a current segment concept. This concept implies that current segment object receives requests for adding new data or metadata. Also, PEB container object can receive requests for update of existing data or metadata. Segment request queue implements concept of such add new data or update requests.
Segment request object defines:
(1) logical extent (inode ID, logical offset from file's beginning
in bytes, valid bytes count in request);
(2) volume extent (segment ID, logical block ID, length in logical
blocks);(3) request class (read, create, update, and so on);
(4) request command (read page, create block, update block, and so on);
(5) request type (synchronous, asynchronous, and so on).
Current segment has create queue and every PEB container object has update queue. Caller needs to allocate segment request object, initialize it, and add into particular request queue. PEB's dedicated thread takes requests from the queue and executes requested action.
--- fs/ssdfs/request_queue.c | 1240 ++++++++++++++++++++++++++++++++++++++ fs/ssdfs/request_queue.h | 417 +++++++++++++ 2 files changed, 1657 insertions(+) create mode 100644 fs/ssdfs/request_queue.c create mode 100644 fs/ssdfs/request_queue.h
fs/ssdfs/request_queue.c new file +1240 -0
@@ -0,0 +1,1240 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/request_queue.c - request queue implementation. +* +* 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/ +* +* (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 +*/ + +#include <linux/slab.h> +#include <linux/pagevec.h> + +#include "peb_mapping_queue.h" +#include "peb_mapping_table_cache.h" +#include "ssdfs.h" +#include "request_queue.h" +#include "segment_bitmap.h" +#include "offset_translation_table.h" +#include "page_array.h" +#include "page_vector.h" +#include "peb_container.h" +#include "segment.h" +#include "btree_search.h" +#include "btree_node.h" +#include "btree.h" +#include "snapshots_tree.h" + +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_t ssdfs_req_queue_page_leaks; +atomic64_t ssdfs_req_queue_memory_leaks; +atomic64_t ssdfs_req_queue_cache_leaks; +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ + +/* +* void ssdfs_req_queue_cache_leaks_increment(void *kaddr) +* void ssdfs_req_queue_cache_leaks_decrement(void *kaddr) +* void *ssdfs_req_queue_kmalloc(size_t size, gfp_t flags) +* void *ssdfs_req_queue_kzalloc(size_t size, gfp_t flags) +* void *ssdfs_req_queue_kcalloc(size_t n, size_t size, gfp_t flags) +* void ssdfs_req_queue_kfree(void *kaddr) +* struct page *ssdfs_req_queue_alloc_page(gfp_t gfp_mask) +* struct page *ssdfs_req_queue_add_pagevec_page(struct pagevec *pvec) +* void ssdfs_req_queue_free_page(struct page *page) +* void ssdfs_req_queue_pagevec_release(struct pagevec *pvec) +*/ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING + SSDFS_MEMORY_LEAKS_CHECKER_FNS(req_queue) +#else + SSDFS_MEMORY_ALLOCATOR_FNS(req_queue) +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ + +void ssdfs_req_queue_memory_leaks_init (void ) +{ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_set (&ssdfs_req_queue_page_leaks,0 ); +atomic64_set (&ssdfs_req_queue_memory_leaks,0 ); +atomic64_set (&ssdfs_req_queue_cache_leaks,0 ); +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +} + +void ssdfs_req_queue_check_memory_leaks (void ) +{ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +if (atomic64_read (&ssdfs_req_queue_page_leaks) !=0 ) { +SSDFS_ERR ("REQUESTS QUEUE: " +"memory leaks include %lld pages\n" , +atomic64_read (&ssdfs_req_queue_page_leaks)); + } + +if (atomic64_read (&ssdfs_req_queue_memory_leaks) !=0 ) { +SSDFS_ERR ("REQUESTS QUEUE: " +"memory allocator suffers from %lld leaks\n" , +atomic64_read (&ssdfs_req_queue_memory_leaks)); + } + +if (atomic64_read (&ssdfs_req_queue_cache_leaks) !=0 ) { +SSDFS_ERR ("REQUESTS QUEUE: " +"caches suffers from %lld leaks\n" , +atomic64_read (&ssdfs_req_queue_cache_leaks)); + } +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +} + +static struct kmem_cache *ssdfs_seg_req_obj_cachep; + +void ssdfs_zero_seg_req_obj_cache_ptr (void ) +{ + ssdfs_seg_req_obj_cachep =NULL ; +} + +static +void ssdfs_init_seg_req_object_once (void *obj) +{ +struct ssdfs_segment_request *req_obj = obj; + +memset (req_obj,0 ,sizeof (struct ssdfs_segment_request )); +} + +void ssdfs_shrink_seg_req_obj_cache (void ) +{ +if (ssdfs_seg_req_obj_cachep) +kmem_cache_shrink (ssdfs_seg_req_obj_cachep); +} + +void ssdfs_destroy_seg_req_obj_cache (void ) +{ +if (ssdfs_seg_req_obj_cachep) +kmem_cache_destroy (ssdfs_seg_req_obj_cachep); +} + +int ssdfs_init_seg_req_obj_cache (void ) +{ + ssdfs_seg_req_obj_cachep =kmem_cache_create ("ssdfs_seg_req_obj_cache" , +sizeof (struct ssdfs_segment_request ),0 , +SLAB_RECLAIM_ACCOUNT | +SLAB_MEM_SPREAD | +SLAB_ACCOUNT , + ssdfs_init_seg_req_object_once); +if (!ssdfs_seg_req_obj_cachep) { +SSDFS_ERR ("unable to create segment request objects cache\n" ); +return -ENOMEM ; + } + +return 0 ; +} + +/* +* ssdfs_requests_queue_init() - initialize request queue +* @rq: initialized request queue +*/ +void ssdfs_requests_queue_init (struct ssdfs_requests_queue *rq) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!rq); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock_init (&rq->lock); +INIT_LIST_HEAD (&rq->list); +} + +/* +* is_ssdfs_requests_queue_empty() - check that requests queue is empty +* @rq: requests queue +*/ +bool is_ssdfs_requests_queue_empty (struct ssdfs_requests_queue *rq) +{ +bool is_empty; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!rq); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock (&rq->lock); + is_empty =list_empty_careful (&rq->list); +spin_unlock (&rq->lock); + +return is_empty; +} + +/* +* ssdfs_requests_queue_add_head() - add request at the head of queue +* @rq: requests queue +* @req: request +*/ +void ssdfs_requests_queue_add_head (struct ssdfs_requests_queue *rq, +struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!rq || !req); + +SSDFS_DBG ("seg_id %llu, class %#x, cmd %#x\n" , + req->place.start.seg_id, + req->private.class, + req->private.cmd); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock (&rq->lock); +list_add (&req->list, &rq->list); +spin_unlock (&rq->lock); +} + +/* +* ssdfs_requests_queue_add_head_inc() - add request at the head of queue +* @fsi: pointer on shared file system object +* @rq: requests queue +* @req: request +*/ +void ssdfs_requests_queue_add_head_inc (struct ssdfs_fs_info *fsi, +struct ssdfs_requests_queue *rq, +struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi || !rq || !req); + +SSDFS_DBG ("seg_id %llu, class %#x, cmd %#x\n" , + req->place.start.seg_id, + req->private.class, + req->private.cmd); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_requests_queue_add_head (rq, req); +atomic64_inc (&fsi->flush_reqs); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("flush_reqs %lld\n" , +atomic64_read (&fsi->flush_reqs)); +#endif /* CONFIG_SSDFS_DEBUG */ +} + +/* +* ssdfs_requests_queue_add_tail() - add request at the tail of queue +* @rq: requests queue +* @req: request +*/ +void ssdfs_requests_queue_add_tail (struct ssdfs_requests_queue *rq, +struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!rq || !req); + +SSDFS_DBG ("seg_id %llu, class %#x, cmd %#x\n" , + req->place.start.seg_id, + req->private.class, + req->private.cmd); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock (&rq->lock); +list_add_tail (&req->list, &rq->list); +spin_unlock (&rq->lock); +} + +/* +* ssdfs_requests_queue_add_tail_inc() - add request at the tail of queue +* @fsi: pointer on shared file system object +* @rq: requests queue +* @req: request +*/ +void ssdfs_requests_queue_add_tail_inc (struct ssdfs_fs_info *fsi, +struct ssdfs_requests_queue *rq, +struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi || !rq || !req); + +SSDFS_DBG ("seg_id %llu, class %#x, cmd %#x\n" , + req->place.start.seg_id, + req->private.class, + req->private.cmd); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_requests_queue_add_tail (rq, req); +atomic64_inc (&fsi->flush_reqs); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("flush_reqs %lld\n" , +atomic64_read (&fsi->flush_reqs)); +#endif /* CONFIG_SSDFS_DEBUG */ +} + +/* +* is_request_command_valid() - check request's command validity +* @class: request's class +* @cmd: request's command +*/ +static inline +bool is_request_command_valid (int class,int cmd) +{ +bool is_valid = false; + +switch (class) { +case SSDFS_PEB_READ_REQ : + is_valid = cmd >SSDFS_UNKNOWN_CMD && + cmd <SSDFS_READ_CMD_MAX ; +break ; + +case SSDFS_PEB_PRE_ALLOCATE_DATA_REQ : +case SSDFS_PEB_CREATE_DATA_REQ : +case SSDFS_PEB_PRE_ALLOCATE_LNODE_REQ : +case SSDFS_PEB_CREATE_LNODE_REQ : +case SSDFS_PEB_PRE_ALLOCATE_HNODE_REQ : +case SSDFS_PEB_CREATE_HNODE_REQ : +case SSDFS_PEB_PRE_ALLOCATE_IDXNODE_REQ : +case SSDFS_PEB_CREATE_IDXNODE_REQ : +case SSDFS_ZONE_USER_DATA_MIGRATE_REQ : + is_valid = cmd >SSDFS_READ_CMD_MAX && + cmd <SSDFS_CREATE_CMD_MAX ; +break ; + +case SSDFS_PEB_UPDATE_REQ : +case SSDFS_PEB_PRE_ALLOC_UPDATE_REQ : + is_valid = cmd >SSDFS_CREATE_CMD_MAX && + cmd <SSDFS_UPDATE_CMD_MAX ; +break ; + +case SSDFS_PEB_DIFF_ON_WRITE_REQ : + is_valid = cmd >SSDFS_UPDATE_CMD_MAX && + cmd <SSDFS_DIFF_ON_WRITE_MAX ; +break ; + +case SSDFS_PEB_COLLECT_GARBAGE_REQ : + is_valid = cmd >SSDFS_DIFF_ON_WRITE_MAX && + cmd <SSDFS_COLLECT_GARBAGE_CMD_MAX ; +break ; + +default : + is_valid = false; + } + +return is_valid; +} + +/* +* ssdfs_requests_queue_remove_first() - get request and remove from queue +* @rq: requests queue +* @req: first request [out] +* +* This function get first request in @rq, remove it from queue +* and return as @req. +* +* RETURN: +* [success] - @req contains pointer on request. +* [failure] - error code: +* +* %-ENODATA - queue is empty. +* %-ENOENT - first empty is NULL. +*/ +int ssdfs_requests_queue_remove_first (struct ssdfs_requests_queue *rq, +struct ssdfs_segment_request **req) +{ +bool is_empty; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!rq || !req); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock (&rq->lock); + is_empty =list_empty_careful (&rq->list); +if (!is_empty) { + *req =list_first_entry_or_null (&rq->list, + struct ssdfs_segment_request, + list); +if (!*req) { +SSDFS_WARN ("first entry is NULL\n" ); + err = -ENOENT ; + }else +list_del (&(*req)->list); + } +spin_unlock (&rq->lock); + +if (is_empty) { +SSDFS_WARN ("requests queue is empty\n" ); +return -ENODATA ; + }else if (err) +return err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!is_request_command_valid ((*req)->private.class, + (*req)->private.cmd)); +BUG_ON ((*req)->private.type >=SSDFS_REQ_TYPE_MAX ); + +SSDFS_DBG ("seg_id %llu, class %#x, cmd %#x\n" , + (*req)->place.start.seg_id, + (*req)->private.class, + (*req)->private.cmd); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_requests_queue_remove_all() - remove all requests from queue +* @rq: requests queue +* @err: error code +* +* This function removes all requests from the queue. +*/ +void ssdfs_requests_queue_remove_all (struct ssdfs_requests_queue *rq, +int err) +{ +bool is_empty; +LIST_HEAD (tmp_list); +struct list_head *this, *next; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!rq); +#endif /* CONFIG_SSDFS_DEBUG */ + +spin_lock (&rq->lock); + is_empty =list_empty_careful (&rq->list); +if (!is_empty) +list_replace_init (&rq->list, &tmp_list); +spin_unlock (&rq->lock); + +if (is_empty) +return ; + +list_for_each_safe (this, next, &tmp_list) { +struct ssdfs_segment_request *req; +unsigned int i; + + req = list_entry(this, struct ssdfs_segment_request, list); + +if (!req) { +SSDFS_WARN ("empty request ptr\n" ); +continue ; + } + +list_del (&req->list); + +SSDFS_WARN ("delete request: " +"class %#x, cmd %#x, type %#x, refs_count %u, " +"seg %llu, extent (start %u, len %u)\n" , + req->private.class, req->private.cmd, + req->private.type, +atomic_read (&req->private.refs_count), + req->place.start.seg_id, + req->place.start.blk_index, + req->place.len); + +atomic_set (&req->result.state,SSDFS_REQ_FAILED ); + +switch (req->private.type) { +case SSDFS_REQ_SYNC : + req->result.err = err; +complete (&req->result.wait); +wake_up_all (&req->private.wait_queue); +break ; + +case SSDFS_REQ_ASYNC : +complete (&req->result.wait); +wake_up_all (&req->private.wait_queue); + +for (i =0 ; i <pagevec_count (&req->result.pvec); i++) { +struct page *page = req->result.pvec.pages[i]; + +if (!page) { +SSDFS_WARN ("empty page ptr: index %u\n" , i); +continue ; + } + +#ifdef CONFIG_SSDFS_DEBUG +WARN_ON (!PageLocked (page)); +#endif /* CONFIG_SSDFS_DEBUG */ + +ClearPageUptodate (page); +ssdfs_clear_page_private (page,0 ); +ClearPageMappedToDisk (page); +ssdfs_clear_dirty_page (page); +ssdfs_unlock_page (page); +end_page_writeback (page); + } + +ssdfs_put_request (req); +ssdfs_request_free (req); +break ; + +case SSDFS_REQ_ASYNC_NO_FREE : +complete (&req->result.wait); +wake_up_all (&req->private.wait_queue); + +for (i =0 ; i <pagevec_count (&req->result.pvec); i++) { +struct page *page = req->result.pvec.pages[i]; + +if (!page) { +SSDFS_WARN ("empty page ptr: index %u\n" , i); +continue ; + } + +#ifdef CONFIG_SSDFS_DEBUG +WARN_ON (!PageLocked (page)); +#endif /* CONFIG_SSDFS_DEBUG */ + +ClearPageUptodate (page); +ssdfs_clear_page_private (page,0 ); +ClearPageMappedToDisk (page); +ssdfs_clear_dirty_page (page); +ssdfs_unlock_page (page); +end_page_writeback (page); + } + +ssdfs_put_request (req); +break ; + +default : +BUG (); + }; + } +} + +/* +* ssdfs_request_alloc() - allocate memory for segment request object +*/ +struct ssdfs_segment_request *ssdfs_request_alloc (void ) +{ +struct ssdfs_segment_request *ptr; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ssdfs_seg_req_obj_cachep); +#endif /* CONFIG_SSDFS_DEBUG */ + + ptr =kmem_cache_alloc (ssdfs_seg_req_obj_cachep,GFP_KERNEL ); +if (!ptr) { +SSDFS_ERR ("fail to allocate memory for request\n" ); +return ERR_PTR (-ENOMEM ); + } + +ssdfs_req_queue_cache_leaks_increment (ptr); + +return ptr; +} + +/* +* ssdfs_request_free() - free memory for segment request object +*/ +void ssdfs_request_free (struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ssdfs_seg_req_obj_cachep); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!req) +return ; + +ssdfs_req_queue_cache_leaks_decrement (req); +kmem_cache_free (ssdfs_seg_req_obj_cachep, req); +} + +/* +* ssdfs_request_init() - common request initialization +* @req: request [out] +*/ +void ssdfs_request_init (struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + +memset (req,0 ,sizeof (struct ssdfs_segment_request )); + +INIT_LIST_HEAD (&req->list); +atomic_set (&req->private.refs_count,0 ); +init_waitqueue_head (&req->private.wait_queue); +pagevec_init (&req->result.pvec); +pagevec_init (&req->result.diffs); +atomic_set (&req->result.state,SSDFS_REQ_CREATED ); +init_completion (&req->result.wait); + req->result.err =0 ; +} + +/* +* ssdfs_get_request() - increment reference counter +* @req: request +*/ +void ssdfs_get_request (struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + +WARN_ON (atomic_inc_return (&req->private.refs_count) <=0 ); +} + +/* +* ssdfs_put_request() - decrement reference counter +* @req: request +*/ +void ssdfs_put_request (struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (atomic_dec_return (&req->private.refs_count) <0 ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("request's reference count %d\n" , +atomic_read (&req->private.refs_count)); +#endif /* CONFIG_SSDFS_DEBUG */ + } +} + +/* +* ssdfs_request_add_page() - add memory page into segment request +* @page: memory page +* @req: segment request [out] +*/ +int ssdfs_request_add_page (struct page *page, +struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!page || !req); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (pagevec_space (&req->result.pvec) ==0 ) { +SSDFS_WARN ("request's pagevec is full\n" ); +return -E2BIG ; + } + +pagevec_add (&req->result.pvec, page); +return 0 ; +} + +/* +* ssdfs_request_add_diff_page() - add diff page into segment request +* @page: memory page +* @req: segment request [out] +*/ +int ssdfs_request_add_diff_page (struct page *page, +struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!page || !req); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (pagevec_space (&req->result.diffs) ==0 ) { +SSDFS_WARN ("request's pagevec is full\n" ); +return -E2BIG ; + } + +pagevec_add (&req->result.diffs, page); +return 0 ; +} + +/* +* ssdfs_request_allocate_and_add_page() - allocate and add page into request +* @req: segment request [out] +*/ +struct page * +ssdfs_request_allocate_and_add_page (struct ssdfs_segment_request *req) +{ +struct page *page; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); + +SSDFS_DBG ("pagevec count %d\n" , +pagevec_count (&req->result.pvec)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (pagevec_space (&req->result.pvec) ==0 ) { +SSDFS_WARN ("request's pagevec is full\n" ); +return ERR_PTR (-E2BIG ); + } + + page =ssdfs_req_queue_alloc_page (GFP_KERNEL | __GFP_ZERO); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("unable to allocate memory page\n" ); +return ERR_PTR (err); + } + +pagevec_add (&req->result.pvec, page); +return page; +} + +/* +* ssdfs_request_allocate_and_add_diff_page() - allocate and add diff page +* @req: segment request [out] +*/ +struct page * +ssdfs_request_allocate_and_add_diff_page (struct ssdfs_segment_request *req) +{ +struct page *page; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); + +SSDFS_DBG ("pagevec count %d\n" , +pagevec_count (&req->result.diffs)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (pagevec_space (&req->result.diffs) ==0 ) { +SSDFS_WARN ("request's pagevec is full\n" ); +return ERR_PTR (-E2BIG ); + } + + page =ssdfs_req_queue_alloc_page (GFP_KERNEL | __GFP_ZERO); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("unable to allocate memory page\n" ); +return ERR_PTR (err); + } + +pagevec_add (&req->result.diffs, page); +return page; +} + +/* +* ssdfs_request_allocate_and_add_old_state_page() - allocate+add old state page +* @req: segment request [out] +*/ +struct page * +ssdfs_request_allocate_and_add_old_state_page (struct ssdfs_segment_request *req) +{ +struct page *page; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); + +SSDFS_DBG ("pagevec count %d\n" , +pagevec_count (&req->result.old_state)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (pagevec_space (&req->result.old_state) ==0 ) { +SSDFS_WARN ("request's pagevec is full\n" ); +return ERR_PTR (-E2BIG ); + } + + page =ssdfs_req_queue_alloc_page (GFP_KERNEL | __GFP_ZERO); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("unable to allocate memory page\n" ); +return ERR_PTR (err); + } + +pagevec_add (&req->result.old_state, page); +return page; +} + +/* +* ssdfs_request_allocate_locked_page() - allocate and add locked page +* @req: segment request [out] +* @page_index: index of the page +*/ +struct page * +ssdfs_request_allocate_locked_page (struct ssdfs_segment_request *req, +int page_index) +{ +struct page *page; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); + +SSDFS_DBG ("pagevec count %d\n" , +pagevec_count (&req->result.pvec)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (pagevec_space (&req->result.pvec) ==0 ) { +SSDFS_WARN ("request's pagevec is full\n" ); +return ERR_PTR (-E2BIG ); + } + +if (page_index >=PAGEVEC_SIZE ) { +SSDFS_ERR ("invalid page index %d\n" , + page_index); +return ERR_PTR (-EINVAL ); + } + + page = req->result.pvec.pages[page_index]; + +if (page) { +SSDFS_ERR ("page already exists: index %d\n" , + page_index); +return ERR_PTR (-EINVAL ); + } + + page =ssdfs_req_queue_alloc_page (GFP_KERNEL | __GFP_ZERO); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("unable to allocate memory page\n" ); +return ERR_PTR (err); + } + + req->result.pvec.pages[page_index] = page; + +if ((page_index +1 ) > req->result.pvec.nr) + req->result.pvec.nr = page_index +1 ; + +ssdfs_lock_page (page); + +return page; +} + +/* +* ssdfs_request_allocate_locked_diff_page() - allocate locked diff page +* @req: segment request [out] +* @page_index: index of the page +*/ +struct page * +ssdfs_request_allocate_locked_diff_page (struct ssdfs_segment_request *req, +int page_index) +{ +struct page *page; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); + +SSDFS_DBG ("pagevec count %d\n" , +pagevec_count (&req->result.diffs)); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (pagevec_space (&req->result.diffs) ==0 ) { +SSDFS_WARN ("request's pagevec is full\n" ); +return ERR_PTR (-E2BIG ); + } + +if (page_index >=PAGEVEC_SIZE ) { +SSDFS_ERR ("invalid page index %d\n" , + page_index); +return ERR_PTR (-EINVAL ); + } + + page = req->result.diffs.pages[page_index]; + +if (page) { +SSDFS_ERR ("page already exists: index %d\n" , + page_index); +return ERR_PTR (-EINVAL ); + } + + page =ssdfs_req_queue_alloc_page (GFP_KERNEL | __GFP_ZERO); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("unable to allocate memory page\n" ); +return ERR_PTR (err); + } + + req->result.diffs.pages[page_index] = page; + +if ((page_index +1 ) > req->result.diffs.nr) + req->result.diffs.nr = page_index +1 ; + +ssdfs_lock_page (page); + +return page; +} + +/* +* ssdfs_request_add_allocated_page_locked() - allocate, add and lock page +* @req: segment request [out] +*/ +int ssdfs_request_add_allocated_page_locked (struct ssdfs_segment_request *req) +{ +struct page *page; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + + page =ssdfs_request_allocate_and_add_page (req); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("fail to allocate page: err %d\n" , + err); +return err; + } + +ssdfs_lock_page (page); +return 0 ; +} + +/* +* ssdfs_request_add_allocated_diff_locked() - allocate, add and lock page +* @req: segment request [out] +*/ +int ssdfs_request_add_allocated_diff_locked (struct ssdfs_segment_request *req) +{ +struct page *page; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + + page =ssdfs_request_allocate_and_add_diff_page (req); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("fail to allocate page: err %d\n" , + err); +return err; + } + +ssdfs_lock_page (page); +return 0 ; +} + +/* +* ssdfs_request_add_old_state_page_locked() - allocate, add and lock page +* @req: segment request [out] +*/ +int ssdfs_request_add_old_state_page_locked (struct ssdfs_segment_request *req) +{ +struct page *page; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + + page =ssdfs_request_allocate_and_add_old_state_page (req); +if (IS_ERR_OR_NULL (page)) { + err = (page ==NULL ? -ENOMEM :PTR_ERR (page)); +SSDFS_ERR ("fail to allocate page: err %d\n" , + err); +return err; + } + +ssdfs_lock_page (page); +return 0 ; +} + +/* +* ssdfs_request_unlock_and_remove_pages() - unlock and remove pages +* @req: segment request [out] +*/ +void ssdfs_request_unlock_and_remove_pages (struct ssdfs_segment_request *req) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_request_unlock_and_remove_old_state (req); +ssdfs_request_unlock_and_remove_update (req); +ssdfs_request_unlock_and_remove_diffs (req); +} + +/* +* ssdfs_request_unlock_and_remove_update() - unlock and remove update pages +* @req: segment request [out] +*/ +void ssdfs_request_unlock_and_remove_update (struct ssdfs_segment_request *req) +{ +unsigned count; +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + + count =pagevec_count (&req->result.pvec); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("result: pages count %u\n" , + count); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i < count; i++) { +struct page *page = req->result.pvec.pages[i]; + +if (!page) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page %d is NULL\n" , i); +#endif /* CONFIG_SSDFS_DEBUG */ +continue ; + } + +ssdfs_unlock_page (page); + } + +ssdfs_req_queue_pagevec_release (&req->result.pvec); +} + +/* +* ssdfs_request_unlock_and_remove_diffs() - unlock and remove diffs +* @req: segment request [out] +*/ +void ssdfs_request_unlock_and_remove_diffs (struct ssdfs_segment_request *req) +{ +unsigned count; +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + + count =pagevec_count (&req->result.diffs); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("diff: pages count %u\n" , + count); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i < count; i++) { +struct page *page = req->result.diffs.pages[i]; + +if (!page) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page %d is NULL\n" , i); +#endif /* CONFIG_SSDFS_DEBUG */ +continue ; + } + +ssdfs_unlock_page (page); + } + +ssdfs_req_queue_pagevec_release (&req->result.diffs); +} + +/* +* ssdfs_request_unlock_and_remove_old_state() - unlock and remove old state +* @req: segment request [out] +*/ +void ssdfs_request_unlock_and_remove_old_state (struct ssdfs_segment_request *req) +{ +unsigned count; +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + + count =pagevec_count (&req->result.old_state); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("old_state: pages count %u\n" , + count); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i < count; i++) { +struct page *page = req->result.old_state.pages[i]; + +if (!page) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page %d is NULL\n" , i); +#endif /* CONFIG_SSDFS_DEBUG */ +continue ; + } + +ssdfs_unlock_page (page); + } + +ssdfs_req_queue_pagevec_release (&req->result.old_state); +} + +/* +* ssdfs_request_switch_update_on_diff() - switch block update on diff page +* @fsi: shared file system info object +* @diff_page: page with prepared delta +* @req: segment request [out] +*/ +int ssdfs_request_switch_update_on_diff (struct ssdfs_fs_info *fsi, +struct page *diff_page, +struct ssdfs_segment_request *req) +{ +struct page *page; +u32 mem_pages_per_block; +int page_index; +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi || !req); +#endif /* CONFIG_SSDFS_DEBUG */ + + mem_pages_per_block = fsi->pagesize /PAGE_SIZE ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (mem_pages_per_block ==0 ); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_request_unlock_and_remove_old_state (req); + + page_index = req->result.processed_blks * mem_pages_per_block; + +for (i =0 ; i < mem_pages_per_block; i++) { + page_index += i; + +if (page_index >=pagevec_count (&req->result.pvec)) { +SSDFS_ERR ("page_index %d >= pvec_size %u\n" , + page_index, +pagevec_count (&req->result.pvec)); +return -ERANGE ; + } + + page = req->result.pvec.pages[page_index]; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!page); +#endif /* CONFIG_SSDFS_DEBUG */ + +clear_page_new (page); +SetPageUptodate (page); +ssdfs_clear_dirty_page (page); + +ssdfs_unlock_page (page); +end_page_writeback (page); + +if (!(req->private.flags &SSDFS_REQ_DONT_FREE_PAGES )) +ssdfs_req_queue_forget_page (page); + + req->result.pvec.pages[page_index] =NULL ; + } + + page_index = req->result.processed_blks * mem_pages_per_block; +set_page_new (diff_page); + req->result.pvec.pages[page_index] = diff_page; + req->result.diffs.pages[0 ] =NULL ; + +if (pagevec_count (&req->result.diffs) >1 ) { +SSDFS_WARN ("diff pagevec contains several pages %u\n" , +pagevec_count (&req->result.diffs)); +ssdfs_req_queue_pagevec_release (&req->result.diffs); + }else +pagevec_reinit (&req->result.diffs); + +return 0 ; +} + +/* +* ssdfs_request_unlock_and_remove_page() - unlock and remove page +* @req: segment request [in|out] +* @page_index: page index +*/ +void ssdfs_request_unlock_and_remove_page (struct ssdfs_segment_request *req, +int page_index) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (page_index >=pagevec_count (&req->result.pvec)) { +SSDFS_ERR ("page_index %d >= pagevec_count %u\n" , + page_index, +pagevec_count (&req->result.pvec)); +return ; + } + +if (!req->result.pvec.pages[page_index]) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page %d is NULL\n" , page_index); +#endif /* CONFIG_SSDFS_DEBUG */ +return ; + } + +ssdfs_unlock_page (req->result.pvec.pages[page_index]); +ssdfs_req_queue_forget_page (req->result.pvec.pages[page_index]); + req->result.pvec.pages[page_index] =NULL ; +} + +/* +* ssdfs_free_flush_request_pages() - unlock and remove flush request's pages +* @req: segment request [out] +*/ +void ssdfs_free_flush_request_pages (struct ssdfs_segment_request *req) +{ +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!req); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i <pagevec_count (&req->result.pvec); i++) { +struct page *page = req->result.pvec.pages[i]; +bool need_free_page = false; + +if (!page) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("page %d is NULL\n" , i); +#endif /* CONFIG_SSDFS_DEBUG */ +continue ; + } + +if (need_add_block (page)) { +clear_page_new (page); + +if (req->private.flags &SSDFS_REQ_PREPARE_DIFF ) + need_free_page = true; + } + +if (PageWriteback (page)) +end_page_writeback (page); +else { +SSDFS_WARN ("page %d is not under writeback: " +"cmd %#x, type %#x\n" , + i, req->private.cmd, + req->private.type); + } + +if (PageLocked (page)) +ssdfs_unlock_page (page); +else { +SSDFS_WARN ("page %d is not locked: " +"cmd %#x, type %#x\n" , + i, req->private.cmd, + req->private.type); + } + + req->result.pvec.pages[i] =NULL ; + +if (need_free_page) +ssdfs_req_queue_free_page (page); +else if (!(req->private.flags &SSDFS_REQ_DONT_FREE_PAGES )) +ssdfs_req_queue_free_page (page); + } + +if (req->private.flags &SSDFS_REQ_DONT_FREE_PAGES ) { +/* +* Do nothing +*/ + }else +pagevec_reinit (&req->result.pvec); +} + +/* +* ssdfs_peb_extent_length() - determine extent length in pagevec +* @si: segment object +* @pvec: page vector +*/ +u8 ssdfs_peb_extent_length (struct ssdfs_segment_info *si, +struct pagevec *pvec) +{ +u32 len; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!si || !si->fsi || !pvec); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (si->fsi->pagesize <PAGE_SIZE ) { +BUG_ON (PAGE_SIZE % si->fsi->pagesize); + len =PAGE_SIZE / si->fsi->pagesize; + len *=pagevec_count (pvec); +BUG_ON (len ==0 ); + }else { + len =pagevec_count (pvec) *PAGE_SIZE ; +BUG_ON (len ==0 ); +BUG_ON (len % si->fsi->pagesize); + len = si->fsi->pagesize / len; +BUG_ON (len ==0 ); + } + +BUG_ON (len >=U8_MAX ); +return (u8 )len; +}
fs/ssdfs/request_queue.h new file +417 -0
417 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 17/76 ssdfs: introduce offset translation table
In reply to Viacheslav DubeykoOne of the key goal of SSDFS is to decrease the write amplification factor. Logical extent concept is the fundamental technique to achieve the goal. Logical extent describes any volume extent on the basis of segment ID, logical block ID, and length. Migration scheme guarantee that segment ID will be always the same for any logical extent. Offset translation table provides the way to convert logical block ID into offset inside of a log of particular "Physical" Erase Block (PEB). As a result, extents b-tree never needs to be updated because logical extent will never change until intentional movement from one segment into another one.
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.
--- fs/ssdfs/offset_translation_table.c | 2914 +++++++++++++++++++++++++++ fs/ssdfs/offset_translation_table.h | 446 ++++ 2 files changed, 3360 insertions(+) create mode 100644 fs/ssdfs/offset_translation_table.c create mode 100644 fs/ssdfs/offset_translation_table.h
fs/ssdfs/offset_translation_table.c new file +2914 -0
@@ -0,0 +1,2914 @@ +// SPDX-License-Identifier: BSD-3-Clause-Clear +/* +* SSDFS -- SSD-oriented File System. +* +* fs/ssdfs/offset_translation_table.c - offset translation table 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/bitmap.h> +#include <linux/slab.h> +#include <linux/pagevec.h> + +#include "peb_mapping_queue.h" +#include "peb_mapping_table_cache.h" +#include "ssdfs.h" +#include "offset_translation_table.h" +#include "page_array.h" +#include "page_vector.h" +#include "peb.h" +#include "peb_container.h" +#include "segment_bitmap.h" +#include "segment.h" + +#include <trace/events/ssdfs.h> + +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_t ssdfs_blk2off_page_leaks; +atomic64_t ssdfs_blk2off_memory_leaks; +atomic64_t ssdfs_blk2off_cache_leaks; +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ + +/* +* void ssdfs_blk2off_cache_leaks_increment(void *kaddr) +* void ssdfs_blk2off_cache_leaks_decrement(void *kaddr) +* void *ssdfs_blk2off_kmalloc(size_t size, gfp_t flags) +* void *ssdfs_blk2off_kzalloc(size_t size, gfp_t flags) +* void *ssdfs_blk2off_kvzalloc(size_t size, gfp_t flags) +* void *ssdfs_blk2off_kcalloc(size_t n, size_t size, gfp_t flags) +* void ssdfs_blk2off_kfree(void *kaddr) +* void ssdfs_blk2off_kvfree(void *kaddr) +* struct page *ssdfs_blk2off_alloc_page(gfp_t gfp_mask) +* struct page *ssdfs_blk2off_add_pagevec_page(struct pagevec *pvec) +* void ssdfs_blk2off_free_page(struct page *page) +* void ssdfs_blk2off_pagevec_release(struct pagevec *pvec) +*/ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING + SSDFS_MEMORY_LEAKS_CHECKER_FNS(blk2off) +#else + SSDFS_MEMORY_ALLOCATOR_FNS(blk2off) +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ + +void ssdfs_blk2off_memory_leaks_init (void ) +{ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +atomic64_set (&ssdfs_blk2off_page_leaks,0 ); +atomic64_set (&ssdfs_blk2off_memory_leaks,0 ); +atomic64_set (&ssdfs_blk2off_cache_leaks,0 ); +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +} + +void ssdfs_blk2off_check_memory_leaks (void ) +{ +#ifdef CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING +if (atomic64_read (&ssdfs_blk2off_page_leaks) !=0 ) { +SSDFS_ERR ("BLK2OFF TABLE: " +"memory leaks include %lld pages\n" , +atomic64_read (&ssdfs_blk2off_page_leaks)); + } + +if (atomic64_read (&ssdfs_blk2off_memory_leaks) !=0 ) { +SSDFS_ERR ("BLK2OFF TABLE: " +"memory allocator suffers from %lld leaks\n" , +atomic64_read (&ssdfs_blk2off_memory_leaks)); + } + +if (atomic64_read (&ssdfs_blk2off_cache_leaks) !=0 ) { +SSDFS_ERR ("BLK2OFF TABLE: " +"caches suffers from %lld leaks\n" , +atomic64_read (&ssdfs_blk2off_cache_leaks)); + } +#endif /* CONFIG_SSDFS_MEMORY_LEAKS_ACCOUNTING */ +} + +/****************************************************************************** +* BLK2OFF TABLE CACHE * +******************************************************************************/ + +static struct kmem_cache *ssdfs_blk2off_frag_obj_cachep; + +void ssdfs_zero_blk2off_frag_obj_cache_ptr (void ) +{ + ssdfs_blk2off_frag_obj_cachep =NULL ; +} + +static void ssdfs_init_blk2off_frag_object_once (void *obj) +{ +struct ssdfs_phys_offset_table_fragment *frag_obj = obj; + +memset (frag_obj,0 ,sizeof (struct ssdfs_phys_offset_table_fragment )); +} + +void ssdfs_shrink_blk2off_frag_obj_cache (void ) +{ +if (ssdfs_blk2off_frag_obj_cachep) +kmem_cache_shrink (ssdfs_blk2off_frag_obj_cachep); +} + +void ssdfs_destroy_blk2off_frag_obj_cache (void ) +{ +if (ssdfs_blk2off_frag_obj_cachep) +kmem_cache_destroy (ssdfs_blk2off_frag_obj_cachep); +} + +int ssdfs_init_blk2off_frag_obj_cache (void ) +{ +size_t obj_size =sizeof (struct ssdfs_phys_offset_table_fragment ); + + ssdfs_blk2off_frag_obj_cachep = +kmem_cache_create ("ssdfs_blk2off_frag_obj_cache" , + obj_size,0 , +SLAB_RECLAIM_ACCOUNT | +SLAB_MEM_SPREAD | +SLAB_ACCOUNT , + ssdfs_init_blk2off_frag_object_once); +if (!ssdfs_blk2off_frag_obj_cachep) { +SSDFS_ERR ("unable to create blk2off fragments cache\n" ); +return -ENOMEM ; + } + +return 0 ; +} + +/* +* ssdfs_blk2off_frag_alloc() - allocate memory for blk2off fragment +*/ +static +struct ssdfs_phys_offset_table_fragment *ssdfs_blk2off_frag_alloc (void ) +{ +struct ssdfs_phys_offset_table_fragment *ptr; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ssdfs_blk2off_frag_obj_cachep); +#endif /* CONFIG_SSDFS_DEBUG */ + + ptr =kmem_cache_alloc (ssdfs_blk2off_frag_obj_cachep,GFP_KERNEL ); +if (!ptr) { +SSDFS_ERR ("fail to allocate memory for blk2off fragment\n" ); +return ERR_PTR (-ENOMEM ); + } + +ssdfs_blk2off_cache_leaks_increment (ptr); + +return ptr; +} + +/* +* ssdfs_blk2off_frag_free() - free memory for blk2off fragment +*/ +static +void ssdfs_blk2off_frag_free (void *ptr) +{ +struct ssdfs_phys_offset_table_fragment *frag; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!ssdfs_blk2off_frag_obj_cachep); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!ptr) +return ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("ptr %p\n" , ptr); +#endif /* CONFIG_SSDFS_DEBUG */ + + frag = (struct ssdfs_phys_offset_table_fragment *)ptr; + +WARN_ON (atomic_read (&frag->state) ==SSDFS_BLK2OFF_FRAG_DIRTY ); + +if (frag->buf) { +ssdfs_blk2off_kfree (frag->buf); + frag->buf =NULL ; + } + +ssdfs_blk2off_cache_leaks_decrement (frag); +kmem_cache_free (ssdfs_blk2off_frag_obj_cachep, frag); +} + +/****************************************************************************** +* BLK2OFF TABLE OBJECT FUNCTIONALITY * +******************************************************************************/ + +/* +* struct ssdfs_blk2off_init - initialization environment +* @table: pointer on translation table object +* @blk2off_pvec: blk2off table fragment +* @blk_desc_pvec: blk desc table fragment +* @peb_index: PEB's index +* @cno: checkpoint +* @fragments_count: count of fragments in portion +* @capacity: maximum amount of items +* @tbl_hdr: portion header +* @tbl_hdr_off: portion header's offset +* @pot_hdr: fragment header +* @pot_hdr_off: fragment header's offset +* @bmap: temporary bitmap +* @bmap_bytes: bytes in temporaray bitmap +* @extent_array: translation extents temporary array +* @extents_count: count of extents in array +*/ +struct ssdfs_blk2off_init { +struct ssdfs_blk2off_table *table; +struct pagevec *blk2off_pvec; +struct pagevec *blk_desc_pvec; +u16 peb_index; +u64 cno; +u32 fragments_count; +u16 capacity; + +struct ssdfs_blk2off_table_header tbl_hdr; +u32 tbl_hdr_off; +struct ssdfs_phys_offset_table_header pot_hdr; +u32 pot_hdr_off; + +unsigned long *bmap; +u32 bmap_bytes; + +struct ssdfs_translation_extent *extent_array; +u16 extents_count; +}; + +static +void ssdfs_debug_blk2off_table_object (struct ssdfs_blk2off_table *tbl); + +/* +* ssdfs_blk2off_table_init_fragment() - init PEB's fragment +* @ptr: fragment pointer +* @sequence_id: fragment's sequence ID +* @start_id: fragment's start ID +* @pages_per_peb: PEB's pages count +* @state: fragment state after initialization +* @buf_size: pointer on buffer size +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ENOMEM - fail to allocate memory. +*/ +static int +ssdfs_blk2off_table_init_fragment (struct ssdfs_phys_offset_table_fragment *ptr, +u16 sequence_id,u16 start_id, +u32 pages_per_peb,int state, +size_t *buf_size) +{ +size_t blk2off_tbl_hdr_size =sizeof (struct ssdfs_blk2off_table_header ); +size_t hdr_size =sizeof (struct ssdfs_phys_offset_table_header ); +size_t off_size =sizeof (struct ssdfs_phys_offset_descriptor ); +size_t fragment_size =0 ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("ptr %p, sequence_id %u, start_id %u, " +"pages_per_peb %u, state %#x, buf_size %p\n" , + ptr, sequence_id, start_id, pages_per_peb, + state, buf_size); + +BUG_ON (!ptr); +BUG_ON (sequence_id >SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD ); +BUG_ON (state <SSDFS_BLK2OFF_FRAG_CREATED || + state >=SSDFS_BLK2OFF_FRAG_STATE_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + +init_rwsem (&ptr->lock); + +down_write (&ptr->lock); + +if (buf_size) { + fragment_size =min_t (size_t, *buf_size,PAGE_SIZE ); + }else { + fragment_size += blk2off_tbl_hdr_size; + fragment_size += hdr_size + (off_size * pages_per_peb); + fragment_size =min_t (size_t, fragment_size,PAGE_SIZE ); + } + + ptr->buf_size = fragment_size; + ptr->buf =ssdfs_blk2off_kzalloc (ptr->buf_size,GFP_KERNEL ); +if (!ptr->buf) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate table buffer\n" ); + goto finish_fragment_init; + } + + ptr->start_id = start_id; + ptr->sequence_id = sequence_id; +atomic_set (&ptr->id_count,0 ); + + ptr->hdr =SSDFS_POFFTH (ptr->buf); + ptr->phys_offs =SSDFS_PHYSOFFD (ptr->buf + hdr_size); + +atomic_set (&ptr->state, state); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("FRAGMENT: sequence_id %u, start_id %u, id_count %d\n" , + sequence_id, start_id,atomic_read (&ptr->id_count)); +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_fragment_init: +up_write (&ptr->lock); +return err; +} + +/* +* ssdfs_get_migrating_block() - get pointer on migrating block +* @table: pointer on translation table object +* @logical_blk: logical block ID +* @need_allocate: should descriptor being allocated? +* +* This method tries to return pointer on migrating block's +* descriptor. In the case of necessity the descriptor +* will be allocated (if @need_allocate is true). +* +* RETURN: +* [success] - pointer on migrating block's descriptor. +* [failure] - error code: +* +* %-EINVAL - invalid value. +* %-ENOMEM - fail to allocate memory. +*/ +static +struct ssdfs_migrating_block * +ssdfs_get_migrating_block (struct ssdfs_blk2off_table *table, +u16 logical_blk, +bool need_allocate) +{ +struct ssdfs_migrating_block *migrating_blk =NULL ; +void *kaddr; +size_t blk_desc_size =sizeof (struct ssdfs_migrating_block ); +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table); +BUG_ON (logical_blk >= table->lblk2off_capacity); + +SSDFS_DBG ("logical_blk %u, need_allocate %#x\n" , + logical_blk, need_allocate); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (need_allocate) { + migrating_blk =ssdfs_blk2off_kzalloc (blk_desc_size, +GFP_KERNEL ); +if (!migrating_blk) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate migrating block desc\n" ); + goto fail_get_migrating_blk; + } + + err =ssdfs_dynamic_array_set (&table->migrating_blks, + logical_blk, &migrating_blk); +if (unlikely (err)) { +ssdfs_blk2off_kfree (migrating_blk); +SSDFS_ERR ("fail to store migrating block in array: " +"logical_blk %u, err %d\n" , + logical_blk, err); + goto fail_get_migrating_blk; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("logical_blk %u descriptor has been allocated\n" , + logical_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + }else { + kaddr =ssdfs_dynamic_array_get_locked (&table->migrating_blks, + logical_blk); +if (IS_ERR_OR_NULL (kaddr)) { + err = (kaddr ==NULL ? -ENOENT :PTR_ERR (kaddr)); +SSDFS_ERR ("fail to get migrating block: " +"logical_blk %u, err %d\n" , + logical_blk, err); + goto fail_get_migrating_blk; + } + + migrating_blk =SSDFS_MIGRATING_BLK (*(u8 **)kaddr); + + err =ssdfs_dynamic_array_release (&table->migrating_blks, + logical_blk, kaddr); +if (unlikely (err)) { +SSDFS_ERR ("fail to release: " +"logical_blk %u, err %d\n" , + logical_blk, err); + goto fail_get_migrating_blk; + } + } + +if (migrating_blk) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("logical_blk %u, state %#x\n" , + logical_blk, migrating_blk->state); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +return migrating_blk; + +fail_get_migrating_blk: +return ERR_PTR (err); +} + +/* +* ssdfs_destroy_migrating_blocks_array() - destroy descriptors array +* @table: pointer on translation table object +* +* This method tries to free memory of migrating block +* descriptors array. +*/ +static +void ssdfs_destroy_migrating_blocks_array (struct ssdfs_blk2off_table *table) +{ +struct ssdfs_migrating_block *migrating_blk =NULL ; +void *kaddr; +u32 items_count; +u32 i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table); +#endif /* CONFIG_SSDFS_DEBUG */ + + items_count = table->last_allocated_blk +1 ; + +for (i =0 ; i < items_count; i++) { + kaddr =ssdfs_dynamic_array_get_locked (&table->migrating_blks, + i); +if (IS_ERR_OR_NULL (kaddr)) +continue ; + + migrating_blk =SSDFS_MIGRATING_BLK (*(u8 **)kaddr); + +if (migrating_blk) +ssdfs_blk2off_kfree (migrating_blk); + +ssdfs_dynamic_array_release (&table->migrating_blks, + i, kaddr); + } + +ssdfs_dynamic_array_destroy (&table->migrating_blks); +} + +/* +* ssdfs_blk2off_table_create() - create translation table object +* @fsi: pointer on shared file system object +* @items_count: table's capacity +* @type: table's type +* @state: initial state of object +* +* This method tries to create translation table object. +* +* RETURN: +* [success] - pointer on created object. +* [failure] - error code: +* +* %-EINVAL - invalid value. +* %-ENOMEM - fail to allocate memory. +*/ +struct ssdfs_blk2off_table * +ssdfs_blk2off_table_create (struct ssdfs_fs_info *fsi, +u16 items_count,u8 type, +int state) +{ +struct ssdfs_blk2off_table *ptr; +size_t table_size =sizeof (struct ssdfs_blk2off_table ); +size_t off_pos_size =sizeof (struct ssdfs_offset_position ); +size_t ptr_size =sizeof (struct ssdfs_migrating_block *); +u32 bytes; +u32 bits_count; +int i; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!fsi); +BUG_ON (state <=SSDFS_BLK2OFF_OBJECT_UNKNOWN || + state >=SSDFS_BLK2OFF_OBJECT_STATE_MAX ); +BUG_ON (items_count > (2 * fsi->pages_per_seg)); +BUG_ON (type <=SSDFS_UNKNOWN_OFF_TABLE_TYPE || + type >=SSDFS_OFF_TABLE_MAX_TYPE ); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("fsi %p, items_count %u, type %u, state %#x\n" , + fsi, items_count, type, state); +#else +SSDFS_DBG ("fsi %p, items_count %u, type %u, state %#x\n" , + fsi, items_count, type, state); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + + ptr = (struct ssdfs_blk2off_table *)ssdfs_blk2off_kzalloc (table_size, +GFP_KERNEL ); +if (!ptr) { +SSDFS_ERR ("fail to allocate translation table\n" ); +return ERR_PTR (-ENOMEM ); + } + + ptr->fsi = fsi; + +atomic_set (&ptr->flags,0 ); +atomic_set (&ptr->state,SSDFS_BLK2OFF_OBJECT_UNKNOWN ); + + ptr->pages_per_peb = fsi->pages_per_peb; + ptr->pages_per_seg = fsi->pages_per_seg; + ptr->type = type; + +init_rwsem (&ptr->translation_lock); +init_waitqueue_head (&ptr->wait_queue); + + ptr->init_cno =U64_MAX ; + ptr->used_logical_blks =0 ; + ptr->free_logical_blks = items_count; + ptr->last_allocated_blk =U16_MAX ; + + bytes =ssdfs_blk2off_table_bmap_bytes (items_count); + bytes =min_t (u32, bytes,PAGE_SIZE ); + bits_count = bytes *BITS_PER_BYTE ; + + ptr->lbmap.bits_count = bits_count; + ptr->lbmap.bytes_count = bytes; + +for (i =0 ; i <SSDFS_LBMAP_ARRAY_MAX ; i++) { + ptr->lbmap.array[i] = + (unsigned long *)ssdfs_blk2off_kvzalloc (bytes, +GFP_KERNEL ); +if (!ptr->lbmap.array[i]) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate bitmaps\n" ); + goto free_bmap; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("init_bmap %lx, state_bmap %lx, modification_bmap %lx\n" , + *ptr->lbmap.array[SSDFS_LBMAP_INIT_INDEX ], + *ptr->lbmap.array[SSDFS_LBMAP_STATE_INDEX ], + *ptr->lbmap.array[SSDFS_LBMAP_MODIFICATION_INDEX ]); +#endif /* CONFIG_SSDFS_DEBUG */ + + ptr->lblk2off_capacity = items_count; + + err =ssdfs_dynamic_array_create (&ptr->lblk2off, + ptr->lblk2off_capacity, + off_pos_size, +0xFF ); +if (unlikely (err)) { +SSDFS_ERR ("fail to create translation array: " +"off_pos_size %zu, items_count %u\n" , + off_pos_size, + ptr->lblk2off_capacity); + goto free_bmap; + } + + err =ssdfs_dynamic_array_create (&ptr->migrating_blks, + ptr->lblk2off_capacity, + ptr_size, +0 ); +if (unlikely (err)) { +SSDFS_ERR ("fail to create migrating blocks array: " +"ptr_size %zu, items_count %u\n" , + ptr_size, + ptr->lblk2off_capacity); + goto free_bmap; + } + + ptr->pebs_count = fsi->pebs_per_seg; + + ptr->peb =ssdfs_blk2off_kcalloc (ptr->pebs_count, +sizeof (struct ssdfs_phys_offset_table_array ), +GFP_KERNEL ); +if (!ptr->peb) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate phys offsets array\n" ); + goto free_translation_array; + } + +for (i =0 ; i < ptr->pebs_count; i++) { +struct ssdfs_phys_offset_table_array *table = &ptr->peb[i]; +struct ssdfs_sequence_array *seq_ptr =NULL ; +u32 threshold =SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD ; + + seq_ptr =ssdfs_create_sequence_array (threshold); +if (IS_ERR_OR_NULL (seq_ptr)) { + err = (seq_ptr ==NULL ? -ENOMEM :PTR_ERR (seq_ptr)); +SSDFS_ERR ("fail to allocate sequence: " +"err %d\n" , err); + goto free_phys_offs_array; + }else + table->sequence = seq_ptr; + +if (state ==SSDFS_BLK2OFF_OBJECT_COMPLETE_INIT ) { +struct ssdfs_phys_offset_table_fragment *fragment; +u16 start_id = i * fsi->pages_per_peb; +u32 pages_per_peb = fsi->pages_per_peb; +int fragment_state =SSDFS_BLK2OFF_FRAG_INITIALIZED ; + +atomic_set (&table->fragment_count,1 ); + + fragment =ssdfs_blk2off_frag_alloc (); +if (IS_ERR_OR_NULL (fragment)) { + err = (fragment ==NULL ? -ENOMEM : +PTR_ERR (fragment)); +SSDFS_ERR ("fail to allocate fragment: " +"err %d\n" , err); + goto free_phys_offs_array; + } + + err =ssdfs_sequence_array_init_item (table->sequence, +0 , fragment); +if (unlikely (err)) { +ssdfs_blk2off_frag_free (fragment); +SSDFS_ERR ("fail to init fragment: " +"err %d\n" , err); + goto free_phys_offs_array; + } + + err =ssdfs_blk2off_table_init_fragment (fragment,0 , + start_id, + pages_per_peb, + fragment_state, +NULL ); +if (unlikely (err)) { +SSDFS_ERR ("fail to init fragment: " +"fragment_index %d, err %d\n" , + i, err); + goto free_phys_offs_array; + } + +atomic_set (&table->state, +SSDFS_BLK2OFF_TABLE_COMPLETE_INIT ); + }else if (state ==SSDFS_BLK2OFF_OBJECT_CREATED ) { +atomic_set (&table->fragment_count,0 ); +atomic_set (&table->state, +SSDFS_BLK2OFF_TABLE_CREATED ); + }else +BUG (); + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("init_bmap %lx, state_bmap %lx, modification_bmap %lx\n" , + *ptr->lbmap.array[SSDFS_LBMAP_INIT_INDEX ], + *ptr->lbmap.array[SSDFS_LBMAP_STATE_INDEX ], + *ptr->lbmap.array[SSDFS_LBMAP_MODIFICATION_INDEX ]); +#endif /* CONFIG_SSDFS_DEBUG */ + +init_completion (&ptr->partial_init_end); +init_completion (&ptr->full_init_end); + +atomic_set (&ptr->state, state); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +return ptr; + +free_phys_offs_array: +for (i =0 ; i < ptr->pebs_count; i++) { +struct ssdfs_sequence_array *sequence; + + sequence = ptr->peb[i].sequence; +ssdfs_destroy_sequence_array (sequence, ssdfs_blk2off_frag_free); + ptr->peb[i].sequence =NULL ; + } + +ssdfs_blk2off_kfree (ptr->peb); + +free_translation_array: +ssdfs_dynamic_array_destroy (&ptr->lblk2off); + +free_bmap: +for (i =0 ; i <SSDFS_LBMAP_ARRAY_MAX ; i++) { +ssdfs_blk2off_kvfree (ptr->lbmap.array[i]); + ptr->lbmap.array[i] =NULL ; + } + + ptr->lbmap.bits_count =0 ; + ptr->lbmap.bytes_count =0 ; + +ssdfs_blk2off_kfree (ptr); + +return ERR_PTR (err); +} + +/* +* ssdfs_blk2off_table_destroy() - destroy translation table object +* @table: pointer on translation table object +*/ +void ssdfs_blk2off_table_destroy (struct ssdfs_blk2off_table *table) +{ +#ifdef CONFIG_SSDFS_DEBUG +int migrating_blks =-1 ; +#endif /* CONFIG_SSDFS_DEBUG */ +int state; +int i; + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("table %p\n" , table); +#else +SSDFS_DBG ("table %p\n" , table); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +if (!table) { +WARN_ON (!table); +return ; + } + +if (table->peb) { +for (i =0 ; i < table->pebs_count; i++) { +struct ssdfs_sequence_array *sequence; + + sequence = table->peb[i].sequence; +ssdfs_destroy_sequence_array (sequence, + ssdfs_blk2off_frag_free); + table->peb[i].sequence =NULL ; + + state =atomic_read (&table->peb[i].state); + +switch (state) { +case SSDFS_BLK2OFF_TABLE_DIRTY : +case SSDFS_BLK2OFF_TABLE_DIRTY_PARTIAL_INIT : +SSDFS_WARN ("unexpected table state %#x\n" , + state); +break ; + +default : +/* do nothing */ +break ; + } + } + +ssdfs_blk2off_kfree (table->peb); + table->peb =NULL ; + } + +#ifdef CONFIG_SSDFS_DEBUG +if (table->last_allocated_blk >=U16_MAX ) + migrating_blks =0 ; +else + migrating_blks = table->last_allocated_blk +1 ; + +for (i =0 ; i < migrating_blks; i++) { +struct ssdfs_migrating_block *blk = +ssdfs_get_migrating_block (table, i, false); + +if (IS_ERR_OR_NULL (blk)) +continue ; + +switch (blk->state) { +case SSDFS_LBLOCK_UNDER_MIGRATION : +case SSDFS_LBLOCK_UNDER_COMMIT : +SSDFS_ERR ("logical blk %d is under migration\n" , i); +ssdfs_blk2off_pagevec_release (&blk->pvec); +break ; + } + } +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_dynamic_array_destroy (&table->lblk2off); + +ssdfs_destroy_migrating_blocks_array (table); + +for (i =0 ; i <SSDFS_LBMAP_ARRAY_MAX ; i++) { +ssdfs_blk2off_kvfree (table->lbmap.array[i]); + table->lbmap.array[i] =NULL ; + } + + table->lbmap.bits_count =0 ; + table->lbmap.bytes_count =0 ; + +ssdfs_blk2off_kfree (table); + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("finished\n" ); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ +} + +/* +* ssdfs_blk2off_table_resize_bitmap_array() - resize bitmap array +* @lbmap: bitmap pointer +* @logical_blk: new threshold +*/ +static inline +int ssdfs_blk2off_table_resize_bitmap_array (struct ssdfs_bitmap_array *lbmap, +u16 logical_blk) +{ +unsigned long *bmap_ptr; +u32 new_bits_count; +u32 new_bytes_count; +u32 bits_per_page; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!lbmap); + +SSDFS_DBG ("lbmap %p, logical_blk %u\n" , + lbmap, logical_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + bits_per_page =PAGE_SIZE *BITS_PER_BYTE ; + + new_bits_count = logical_blk + bits_per_page -1 ; + new_bits_count /= bits_per_page; + new_bits_count *= bits_per_page; + + new_bytes_count =ssdfs_blk2off_table_bmap_bytes (new_bits_count); + +for (i =0 ; i <SSDFS_LBMAP_ARRAY_MAX ; i++) { + bmap_ptr =kvrealloc (lbmap->array[i], + lbmap->bytes_count, + new_bytes_count, +GFP_KERNEL | __GFP_ZERO); +if (!bmap_ptr) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate bitmaps\n" ); + goto finish_bitmap_array_resize; + }else + lbmap->array[i] = (unsigned long *)bmap_ptr; + } + + lbmap->bits_count = new_bits_count; + lbmap->bytes_count = new_bytes_count; + +finish_bitmap_array_resize: +return err; +} + +/* +* ssdfs_blk2off_table_bmap_set() - set bit for logical block +* @lbmap: bitmap pointer +* @bitmap_index: index of bitmap +* @logical_blk: logical block number +*/ +static inline +int ssdfs_blk2off_table_bmap_set (struct ssdfs_bitmap_array *lbmap, +int bitmap_index,u16 logical_blk) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!lbmap); + +SSDFS_DBG ("lbmap %p, bitmap_index %d, logical_blk %u\n" , + lbmap, bitmap_index, logical_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (bitmap_index >=SSDFS_LBMAP_ARRAY_MAX ) { +SSDFS_ERR ("invalid bitmap index %d\n" , + bitmap_index); +return -EINVAL ; + } + +if (logical_blk >= lbmap->bits_count) { + err =ssdfs_blk2off_table_resize_bitmap_array (lbmap, + logical_blk); +if (unlikely (err)) { +SSDFS_ERR ("fail to realloc bitmap array: " +"logical_blk %u, err %d\n" , + logical_blk, err); +return err; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!lbmap->array[bitmap_index]); +#endif /* CONFIG_SSDFS_DEBUG */ + +bitmap_set (lbmap->array[bitmap_index], logical_blk,1 ); + +return 0 ; +} + +/* +* ssdfs_blk2off_table_bmap_clear() - clear bit for logical block +* @lbmap: bitmap pointer +* @bitmap_index: index of bitmap +* @logical_blk: logical block number +*/ +static inline +int ssdfs_blk2off_table_bmap_clear (struct ssdfs_bitmap_array *lbmap, +int bitmap_index,u16 logical_blk) +{ +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!lbmap); + +SSDFS_DBG ("lbmap %p, bitmap_index %d, logical_blk %u\n" , + lbmap, bitmap_index, logical_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (bitmap_index >=SSDFS_LBMAP_ARRAY_MAX ) { +SSDFS_ERR ("invalid bitmap index %d\n" , + bitmap_index); +return -EINVAL ; + } + +if (logical_blk >= lbmap->bits_count) { + err =ssdfs_blk2off_table_resize_bitmap_array (lbmap, + logical_blk); +if (unlikely (err)) { +SSDFS_ERR ("fail to realloc bitmap array: " +"logical_blk %u, err %d\n" , + logical_blk, err); +return err; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!lbmap->array[bitmap_index]); +#endif /* CONFIG_SSDFS_DEBUG */ + +bitmap_clear (lbmap->array[bitmap_index], logical_blk,1 ); + +return 0 ; +} + +/* +* ssdfs_blk2off_table_bmap_vacant() - check bit for logical block +* @lbmap: bitmap pointer +* @bitmap_index: index of bitmap +* @lbmap_bits: count of bits in bitmap +* @logical_blk: logical block number +*/ +static inline +bool ssdfs_blk2off_table_bmap_vacant (struct ssdfs_bitmap_array *lbmap, +int bitmap_index, +u16 lbmap_bits, +u16 logical_blk) +{ +unsigned long found; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!lbmap); + +SSDFS_DBG ("lbmap %p, bitmap_index %d, " +"lbmap_bits %u, logical_blk %u\n" , + lbmap, bitmap_index, + lbmap_bits, logical_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (bitmap_index >=SSDFS_LBMAP_ARRAY_MAX ) { +SSDFS_ERR ("invalid bitmap index %d\n" , + bitmap_index); +return false; + } + +if (logical_blk >= lbmap->bits_count) +return true; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!lbmap->array[bitmap_index]); +#endif /* CONFIG_SSDFS_DEBUG */ + + found =find_next_zero_bit (lbmap->array[bitmap_index], + lbmap_bits, logical_blk); + +return found == logical_blk; +} + +/* +* ssdfs_blk2off_table_extent_vacant() - check extent vacancy +* @lbmap: bitmap pointer +* @bitmap_index: index of bitmap +* @lbmap_bits: count of bits in bitmap +* @extent: pointer on extent +*/ +static inline +bool ssdfs_blk2off_table_extent_vacant (struct ssdfs_bitmap_array *lbmap, +int bitmap_index, +u16 lbmap_bits, +struct ssdfs_blk2off_range *extent) +{ +unsigned long start, end; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!lbmap || !extent); + +SSDFS_DBG ("lbmap %p, bitmap_index %d, " +"lbmap_bits %u, extent (start %u, len %u)\n" , + lbmap, bitmap_index, lbmap_bits, + extent->start_lblk, extent->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (bitmap_index >=SSDFS_LBMAP_ARRAY_MAX ) { +SSDFS_ERR ("invalid bitmap index %d\n" , + bitmap_index); +return false; + } + +if (extent->start_lblk >= lbmap_bits) { +SSDFS_ERR ("invalid extent start %u\n" , + extent->start_lblk); +return false; + } + +if (extent->len ==0 || extent->len >=U16_MAX ) { +SSDFS_ERR ("invalid extent length\n" ); +return false; + } + +if (extent->start_lblk >= lbmap->bits_count) +return true; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!lbmap->array[bitmap_index]); +#endif /* CONFIG_SSDFS_DEBUG */ + + start =find_next_zero_bit (lbmap->array[bitmap_index], + lbmap_bits, extent->start_lblk); + +if (start != extent->start_lblk) +return false; +else if (extent->len ==1 ) +return true; + + end =find_next_bit (lbmap->array[bitmap_index], lbmap_bits, start); + +if ((end - start) == extent->len) +return true; + +return false; +} + +/* +* is_ssdfs_table_header_magic_valid() - check segment header's magic +* @hdr: table header +*/ +bool is_ssdfs_table_header_magic_valid (struct ssdfs_blk2off_table_header *hdr) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!hdr); +#endif /* CONFIG_SSDFS_DEBUG */ + +return le16_to_cpu (hdr->magic.key) ==SSDFS_BLK2OFF_TABLE_HDR_MAGIC ; +} + +/* +* ssdfs_check_table_header() - check table header +* @hdr: table header +* @size: size of header +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - header is invalid. +*/ +static +int ssdfs_check_table_header (struct ssdfs_blk2off_table_header *hdr, +size_t size) +{ +u16 extents_off = offsetof(struct ssdfs_blk2off_table_header , + sequence); +size_t extent_size =sizeof (struct ssdfs_translation_extent ); +size_t extent_area; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!hdr); + +SSDFS_DBG ("hdr %p, size %zu\n" , hdr, size); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!is_ssdfs_magic_valid (&hdr->magic) || + !is_ssdfs_table_header_magic_valid (hdr)) { +SSDFS_ERR ("invalid table magic\n" ); +return -EIO ; + } + +if (!is_csum_valid (&hdr->check, hdr, size)) { +SSDFS_ERR ("invalid checksum\n" ); +return -EIO ; + } + +if (extents_off !=le16_to_cpu (hdr->extents_off)) { +SSDFS_ERR ("invalid extents offset %u\n" , +le16_to_cpu (hdr->extents_off)); +return -EIO ; + } + + extent_area = extent_size *le16_to_cpu (hdr->extents_count); +if (le16_to_cpu (hdr->offset_table_off) != (extents_off + extent_area)) { +SSDFS_ERR ("invalid table offset: extents_off %u, " +"extents_count %u, offset_table_off %u\n" , +le16_to_cpu (hdr->extents_off), +le16_to_cpu (hdr->extents_count), +le16_to_cpu (hdr->offset_table_off)); +return -EIO ; + } + +return 0 ; +} + +/* +* ssdfs_check_fragment() - check table's fragment +* @table: pointer on table object +* @peb_index: PEB's index +* @hdr: fragment's header +* @fragment_size: size of fragment in bytes +* +* Method tries to check fragment validity. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - corrupted fragment. +* %-ERANGE - internal error. +*/ +static +int ssdfs_check_fragment (struct ssdfs_blk2off_table *table, +u16 peb_index, +struct ssdfs_phys_offset_table_header *hdr, +size_t fragment_size) +{ +u16 start_id; +u16 sequence_id; +u16 id_count; +u32 byte_size; +u32 items_size; +__le32 csum1, csum2; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table || !hdr); +BUG_ON (peb_index >= table->pebs_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + start_id =le16_to_cpu (hdr->start_id); + id_count =le16_to_cpu (hdr->id_count); + byte_size =le32_to_cpu (hdr->byte_size); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("table %p, peb_index %u, start_id %u, " +"id_count %u, byte_size %u, " +"fragment_id %u\n" , + table, peb_index, + start_id, id_count, byte_size, + hdr->sequence_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (le32_to_cpu (hdr->magic) !=SSDFS_PHYS_OFF_TABLE_MAGIC ) { +SSDFS_ERR ("invalid magic %#x\n" , +le32_to_cpu (hdr->magic)); +return -EIO ; + } + +if (byte_size > fragment_size) { +SSDFS_ERR ("byte_size %u > fragment_size %zu\n" , + byte_size, fragment_size); +return -ERANGE ; + } + + csum1 = hdr->checksum; + hdr->checksum =0 ; + csum2 =ssdfs_crc32_le (hdr, byte_size); + hdr->checksum = csum1; + +if (csum1 != csum2) { +SSDFS_ERR ("csum1 %#x != csum2 %#x\n" , +le32_to_cpu (csum1), +le32_to_cpu (csum2)); +return -EIO ; + } + +if (le16_to_cpu (hdr->peb_index) != peb_index) { +SSDFS_ERR ("invalid peb_index %u\n" , +le16_to_cpu (hdr->peb_index)); +return -EIO ; + } + +if (start_id ==U16_MAX ) { +SSDFS_ERR ("invalid start_id %u for peb_index %u\n" , + start_id, peb_index); +return -EIO ; + } + +if (id_count ==0 || id_count > table->pages_per_peb) { +SSDFS_ERR ("invalid id_count %u for peb_index %u\n" , +le16_to_cpu (hdr->id_count), + peb_index); +return -EIO ; + } + + items_size = (u32 )id_count * +sizeof (struct ssdfs_phys_offset_descriptor ); + +if (byte_size < items_size) { +SSDFS_ERR ("invalid byte_size %u for peb_index %u\n" , +le32_to_cpu (hdr->byte_size), + peb_index); +return -EIO ; + } + + sequence_id =le16_to_cpu (hdr->sequence_id); +if (sequence_id >SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD ) { +SSDFS_ERR ("invalid sequence_id %u for peb_index %u\n" , + sequence_id, peb_index); +return -EIO ; + } + +if (le16_to_cpu (hdr->type) ==SSDFS_UNKNOWN_OFF_TABLE_TYPE || +le16_to_cpu (hdr->type) >=SSDFS_OFF_TABLE_MAX_TYPE ) { +SSDFS_ERR ("invalid type %#x for peb_index %u\n" , +le16_to_cpu (hdr->type), peb_index); +return -EIO ; + } + +if (le16_to_cpu (hdr->flags) & ~SSDFS_OFF_TABLE_FLAGS_MASK ) { +SSDFS_ERR ("invalid flags set %#x for peb_index %u\n" , +le16_to_cpu (hdr->flags), peb_index); +return -EIO ; + } + +return 0 ; +} + +/* +* ssdfs_get_checked_table_header() - get and check table header +* @portion: pointer on portion init environment [out] +*/ +static +int ssdfs_get_checked_table_header (struct ssdfs_blk2off_init *portion) +{ +size_t hdr_size =sizeof (struct ssdfs_blk2off_table_header ); +struct page *page; +int page_index; +u32 page_off; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!portion || !portion->blk2off_pvec); + +SSDFS_DBG ("source %p, offset %u\n" , + portion->blk2off_pvec, portion->tbl_hdr_off); +#endif /* CONFIG_SSDFS_DEBUG */ + + page_index = portion->tbl_hdr_off >>PAGE_SHIFT ; +if (portion->tbl_hdr_off >=PAGE_SIZE ) + page_off = portion->tbl_hdr_off %PAGE_SIZE ; +else + page_off = portion->tbl_hdr_off; + +if (page_index >=pagevec_count (portion->blk2off_pvec)) { +SSDFS_ERR ("invalid page index %d: " +"offset %u, pagevec_count %u\n" , + page_index, portion->tbl_hdr_off, +pagevec_count (portion->blk2off_pvec)); +return -EINVAL ; + } + + page = portion->blk2off_pvec->pages[page_index]; + +ssdfs_lock_page (page); + err =ssdfs_memcpy_from_page (&portion->tbl_hdr,0 , hdr_size, + page, page_off,PAGE_SIZE , + hdr_size); +ssdfs_unlock_page (page); + +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: " +"page_off %u, hdr_size %zu\n" , + page_off, hdr_size); +return err; + } + + err =ssdfs_check_table_header (&portion->tbl_hdr, hdr_size); +if (err) { +SSDFS_ERR ("invalid table header\n" ); +return err; + } + + portion->fragments_count = +le16_to_cpu (portion->tbl_hdr.fragments_count); + +return 0 ; +} + +/* +* ssdfs_blk2off_prepare_temp_bmap() - prepare temporary bitmap +* @portion: initialization environment [in | out] +*/ +static inline +int ssdfs_blk2off_prepare_temp_bmap (struct ssdfs_blk2off_init *portion) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!portion || portion->bmap); +#endif /* CONFIG_SSDFS_DEBUG */ + + portion->bmap_bytes =ssdfs_blk2off_table_bmap_bytes (portion->capacity); + portion->bmap =ssdfs_blk2off_kvzalloc (portion->bmap_bytes, +GFP_KERNEL ); +if (unlikely (!portion->bmap)) { +SSDFS_ERR ("fail to allocate memory\n" ); +return -ENOMEM ; + } + +return 0 ; +} + +/* +* ssdfs_blk2off_prepare_extent_array() - prepare extents array +* @portion: initialization environment [in | out] +*/ +static +int ssdfs_blk2off_prepare_extent_array (struct ssdfs_blk2off_init *portion) +{ +size_t extent_size =sizeof (struct ssdfs_translation_extent ); +u32 extents_off, table_off; +size_t ext_array_size; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!portion || !portion->blk2off_pvec || portion->extent_array); +#endif /* CONFIG_SSDFS_DEBUG */ + + extents_off = offsetof(struct ssdfs_blk2off_table_header , sequence); +if (extents_off !=le16_to_cpu (portion->tbl_hdr.extents_off)) { +SSDFS_ERR ("invalid extents offset %u\n" , +le16_to_cpu (portion->tbl_hdr.extents_off)); +return -EIO ; + } + + portion->extents_count =le16_to_cpu (portion->tbl_hdr.extents_count); + ext_array_size = extent_size * portion->extents_count; + table_off =le16_to_cpu (portion->tbl_hdr.offset_table_off); + +if (ext_array_size ==0 || + (extents_off + ext_array_size) != table_off) { +SSDFS_ERR ("invalid table header: " +"extents_off %u, extents_count %u, " +"offset_table_off %u\n" , + extents_off, portion->extents_count, table_off); +return -EIO ; + } + +if (ext_array_size >0 ) { +u32 array_size = ext_array_size; +u32 read_bytes =0 ; +int page_index; +u32 page_off; +#ifdef CONFIG_SSDFS_DEBUG +int i; +#endif /* CONFIG_SSDFS_DEBUG */ + + portion->extent_array =ssdfs_blk2off_kzalloc (ext_array_size, +GFP_KERNEL ); +if (unlikely (!portion->extent_array)) { +SSDFS_ERR ("fail to allocate memory\n" ); +return -ENOMEM ; + } + + extents_off = offsetof(struct ssdfs_blk2off_table_header , + sequence); + page_index = extents_off >>PAGE_SHIFT ; + page_off = extents_off %PAGE_SIZE ; + +while (array_size >0 ) { +u32 size; +struct page *page; + +if (page_index >=pagevec_count (portion->blk2off_pvec)) { +SSDFS_ERR ("invalid request: " +"page_index %d, pagevec_size %u\n" , + page_index, +pagevec_count (portion->blk2off_pvec)); +return -ERANGE ; + } + + size =min_t (u32,PAGE_SIZE - page_off, + array_size); + page = portion->blk2off_pvec->pages[page_index]; + +ssdfs_lock_page (page); + err =ssdfs_memcpy_from_page (portion->extent_array, + read_bytes, ext_array_size, + page, + page_off,PAGE_SIZE , + size); +ssdfs_unlock_page (page); + +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , + err); +return err; + } + + read_bytes += size; + array_size -= size; + extents_off += size; + + page_index = extents_off >>PAGE_SHIFT ; + page_off = extents_off %PAGE_SIZE ; + }; + +#ifdef CONFIG_SSDFS_DEBUG +for (i =0 ; i < portion->extents_count; i++) { +struct ssdfs_translation_extent *extent; + extent = &portion->extent_array[i]; + +SSDFS_DBG ("index %d, logical_blk %u, offset_id %u, " +"len %u, sequence_id %u, state %u\n" , + i, +le16_to_cpu (extent->logical_blk), +le16_to_cpu (extent->offset_id), +le16_to_cpu (extent->len), + extent->sequence_id, + extent->state); + } +#endif /* CONFIG_SSDFS_DEBUG */ + } + +return 0 ; +} + +/* +* ssdfs_get_fragment_header() - get fragment header +* @portion: initialization environment [in | out] +* @offset: header offset in bytes +*/ +static +int ssdfs_get_fragment_header (struct ssdfs_blk2off_init *portion, +u32 offset) +{ +size_t hdr_size =sizeof (struct ssdfs_phys_offset_table_header ); +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!portion || !portion->blk2off_pvec); + +SSDFS_DBG ("source %p, offset %u\n" , + portion->blk2off_pvec, offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_unaligned_read_pagevec (portion->blk2off_pvec, + offset, + hdr_size, + &portion->pot_hdr); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); +return err; + } + +return 0 ; +} + +/* +* ssdfs_get_checked_fragment() - get checked table's fragment +* @portion: initialization environment [in | out] +* +* This method tries to get and to check fragment validity. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-EIO - corrupted fragment. +* %-EEXIST - has been initialized already. +*/ +static +int ssdfs_get_checked_fragment (struct ssdfs_blk2off_init *portion) +{ +struct ssdfs_phys_offset_table_array *phys_off_table; +struct ssdfs_phys_offset_table_fragment *fragment; +struct page *page; +void *kaddr; +int page_index; +u32 page_off; +size_t fragment_size; +u16 start_id; +u16 sequence_id; +int state; +u32 read_bytes; +#ifdef CONFIG_SSDFS_DEBUG +int i; +#endif /* CONFIG_SSDFS_DEBUG */ +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!portion || !portion->table || !portion->blk2off_pvec); + +SSDFS_DBG ("table %p, peb_index %u, source %p, offset %u\n" , + portion->table, portion->peb_index, + portion->blk2off_pvec, portion->pot_hdr_off); +#endif /* CONFIG_SSDFS_DEBUG */ + + fragment_size =le32_to_cpu (portion->pot_hdr.byte_size); + start_id =le16_to_cpu (portion->pot_hdr.start_id); + sequence_id =le16_to_cpu (portion->pot_hdr.sequence_id); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sequence_id %u\n" , sequence_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (fragment_size >PAGE_SIZE ) { +SSDFS_ERR ("invalid fragment_size %zu\n" , + fragment_size); + +#ifdef CONFIG_SSDFS_DEBUG +for (i =0 ; i <pagevec_count (portion->blk2off_pvec); i++) { + page = portion->blk2off_pvec->pages[i]; + + kaddr =kmap_local_page (page); +SSDFS_DBG ("PAGE DUMP: index %d\n" , + i); +print_hex_dump_bytes ("" ,DUMP_PREFIX_OFFSET , + kaddr, +PAGE_SIZE ); +SSDFS_DBG ("\n" ); +kunmap_local (kaddr); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +return -EIO ; + } + +if (sequence_id >SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD ) { +SSDFS_ERR ("invalid sequence_id %u\n" , + sequence_id); +return -EIO ; + } + + phys_off_table = &portion->table->peb[portion->peb_index]; + + kaddr =ssdfs_sequence_array_get_item (phys_off_table->sequence, + sequence_id); +if (IS_ERR_OR_NULL (kaddr)) { +/* expected state -> continue logic */ +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("fragment %u is absent\n" , + sequence_id); +#endif /* CONFIG_SSDFS_DEBUG */ + }else { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("fragment %u has been initialized already\n" , + sequence_id); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EEXIST ; + } + + fragment =ssdfs_blk2off_frag_alloc (); +if (IS_ERR_OR_NULL (fragment)) { + err = (fragment ==NULL ? -ENOMEM :PTR_ERR (fragment)); +SSDFS_ERR ("fail to allocate fragment: " +"err %d\n" , err); +return err; + } + + err =ssdfs_sequence_array_init_item (phys_off_table->sequence, + sequence_id, + fragment); +if (unlikely (err)) { +ssdfs_blk2off_frag_free (fragment); +SSDFS_ERR ("fail to init fragment: " +"err %d\n" , err); +return err; + } + + state =SSDFS_BLK2OFF_FRAG_CREATED ; + err =ssdfs_blk2off_table_init_fragment (fragment, + sequence_id, + start_id, + portion->table->pages_per_peb, + state, + &fragment_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to initialize fragment: err %d\n" , + err); +return err; + } + + page_index = portion->pot_hdr_off >>PAGE_SHIFT ; +if (portion->pot_hdr_off >=PAGE_SIZE ) + page_off = portion->pot_hdr_off %PAGE_SIZE ; +else + page_off = portion->pot_hdr_off; + +down_write (&fragment->lock); + + read_bytes =0 ; +while (fragment_size >0 ) { +u32 size; + + size =min_t (u32,PAGE_SIZE - page_off, fragment_size); + +if (page_index >=pagevec_count (portion->blk2off_pvec)) { + err = -ERANGE ; +SSDFS_ERR ("invalid request: " +"page_index %d, pvec_size %u\n" , + page_index, +pagevec_count (portion->blk2off_pvec)); + goto finish_fragment_read; + } + + page = portion->blk2off_pvec->pages[page_index]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("read_bytes %u, fragment->buf_size %zu, " +"page_off %u, size %u\n" , + read_bytes, fragment->buf_size, page_off, page_off); +#endif /* CONFIG_SSDFS_DEBUG */ + +ssdfs_lock_page (page); + err =ssdfs_memcpy_from_page (fragment->buf, + read_bytes, fragment->buf_size, + page, page_off,PAGE_SIZE , + size); +ssdfs_unlock_page (page); + +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); + goto finish_fragment_read; + } + + read_bytes += size; + fragment_size -= size; + portion->pot_hdr_off += size; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("read_bytes %u, fragment_size %zu, " +"pot_hdr_off %u\n" , + read_bytes, fragment_size, + portion->pot_hdr_off); +#endif /* CONFIG_SSDFS_DEBUG */ + + page_index = portion->pot_hdr_off >>PAGE_SHIFT ; +if (portion->pot_hdr_off >=PAGE_SIZE ) + page_off = portion->pot_hdr_off %PAGE_SIZE ; +else + page_off = portion->pot_hdr_off; + }; + + err =ssdfs_check_fragment (portion->table, portion->peb_index, + fragment->hdr, + fragment->buf_size); +if (err) + goto finish_fragment_read; + + fragment->start_id = start_id; +atomic_set (&fragment->id_count, +le16_to_cpu (fragment->hdr->id_count)); +atomic_set (&fragment->state,SSDFS_BLK2OFF_FRAG_INITIALIZED ); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("FRAGMENT: sequence_id %u, start_id %u, id_count %d\n" , + sequence_id, start_id,atomic_read (&fragment->id_count)); +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_fragment_read: +up_write (&fragment->lock); + +if (err) { +SSDFS_ERR ("corrupted fragment: err %d\n" , + err); +return err; + } + +return 0 ; +} + +/* +* is_ssdfs_offset_position_older() - is position checkpoint older? +* @pos: position offset +* @cno: checkpoint number for comparison +*/ +static inline +bool is_ssdfs_offset_position_older (struct ssdfs_offset_position *pos, +u64 cno) +{ +if (pos->cno !=SSDFS_INVALID_CNO ) +return pos->cno >= cno; + +return false; +} + +/* +* ssdfs_check_translation_extent() - check translation extent +* @extent: pointer on translation extent +* @capacity: logical blocks capacity +* @sequence_id: extent's sequence id +* +* This method tries to check extent's validity. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EIO - corrupted translation extent. +*/ +static +int ssdfs_check_translation_extent (struct ssdfs_translation_extent *extent, +u16 capacity,u8 sequence_id) +{ +u16 logical_blk; +u16 offset_id; +u16 len; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!extent); +#endif /* CONFIG_SSDFS_DEBUG */ + + logical_blk =le16_to_cpu (extent->logical_blk); + offset_id =le16_to_cpu (extent->offset_id); + len =le16_to_cpu (extent->len); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("logical_blk %u, offset_id %u, len %u, " +"sequence_id %u, state %#x\n" , + logical_blk, offset_id, len, + extent->sequence_id, extent->state); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (extent->state <=SSDFS_LOGICAL_BLK_UNKNOWN_STATE || + extent->state >=SSDFS_LOGICAL_BLK_STATE_MAX ) { +SSDFS_ERR ("invalid translation extent: " +"unknown state %#x\n" , + extent->state); +return -EIO ; + } + +if (logical_blk > (U16_MAX - len) || + (logical_blk + len) > capacity) { +SSDFS_ERR ("invalid translation extent: " +"logical_blk %u, len %u, capacity %u\n" , + logical_blk, len, capacity); +return -EIO ; + } + +if (extent->state !=SSDFS_LOGICAL_BLK_FREE ) { +if (offset_id > (U16_MAX - len)) { +SSDFS_ERR ("invalid translation extent: " +"offset_id %u, len %u\n" , + offset_id, len); +return -EIO ; + } + } + +if (sequence_id != extent->sequence_id) { +SSDFS_ERR ("invalid translation extent: " +"sequence_id %u != extent->sequence_id %u\n" , + sequence_id, extent->sequence_id); +return -EIO ; + } + +return 0 ; +} + +/* +* ssdfs_process_used_translation_extent() - process used translation extent +* @portion: pointer on portion init environment [in | out] +* @extent_index: index of extent +* +* This method checks translation extent, to set bitmap for +* logical blocks in the extent and to fill portion of +* offset position array by physical offsets id. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-EIO - corrupted translation extent. +* %-EAGAIN - extent is partially processed in the fragment. +*/ +static +int ssdfs_process_used_translation_extent (struct ssdfs_blk2off_init *portion, +int *extent_index) +{ +struct ssdfs_sequence_array *sequence =NULL ; +struct ssdfs_phys_offset_table_fragment *frag =NULL ; +struct ssdfs_phys_offset_descriptor *phys_off =NULL ; +struct ssdfs_translation_extent *extent =NULL ; +struct ssdfs_dynamic_array *lblk2off; +void *ptr; +u16 peb_index; +u16 sequence_id; +u16 pos_array_items; +u16 start_id; +u16 id_count; +u16 id_diff; +u32 logical_blk; +u16 offset_id; +u16 len; +int phys_off_index; +bool is_partially_processed = false; +struct ssdfs_blk_state_offset *state_off; +int i, j; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!portion || !extent_index); +BUG_ON (!portion->bmap || !portion->extent_array); +BUG_ON (portion->cno ==SSDFS_INVALID_CNO ); +BUG_ON (*extent_index >= portion->extents_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + lblk2off = &portion->table->lblk2off; + + peb_index = portion->peb_index; + sequence_id =le16_to_cpu (portion->pot_hdr.sequence_id); + + sequence = portion->table->peb[peb_index].sequence; + ptr =ssdfs_sequence_array_get_item (sequence, sequence_id); +if (IS_ERR_OR_NULL (ptr)) { + err = (ptr ==NULL ? -ENOENT :PTR_ERR (ptr)); +SSDFS_ERR ("fail to get fragment: " +"sequence_id %u, err %d\n" , + sequence_id, err); +return err; + } + frag = (struct ssdfs_phys_offset_table_fragment *)ptr; + + start_id =le16_to_cpu (portion->pot_hdr.start_id); + id_count =le16_to_cpu (portion->pot_hdr.id_count); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start_id %u, id_count %u\n" , + start_id, id_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + extent = &portion->extent_array[*extent_index]; + + err =ssdfs_check_translation_extent (extent, portion->capacity, + *extent_index); +if (err) { +SSDFS_ERR ("invalid translation extent: " +"index %u, err %d\n" , + *extent_index, err); +return err; + } + +if (*extent_index ==0 && extent->state !=SSDFS_LOGICAL_BLK_USED ) { +SSDFS_ERR ("invalid translation extent state %#x\n" , + extent->state); +return -EIO ; + } + + logical_blk =le16_to_cpu (extent->logical_blk); + offset_id =le16_to_cpu (extent->offset_id); + len =le16_to_cpu (extent->len); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("logical_blk %u, offset_id %u, len %u, " +"sequence_id %u, state %#x\n" , + logical_blk, offset_id, len, + extent->sequence_id, extent->state); +#endif /* CONFIG_SSDFS_DEBUG */ + +if ((start_id + id_count) < offset_id) { +SSDFS_ERR ("start_id %u + id_count %u < offset_id %u\n" , + start_id, id_count, offset_id); +return -EIO ; + } + +if ((offset_id + len) <= start_id) { +SSDFS_ERR ("offset_id %u + len %u <= start_id %u\n" , + offset_id, len, start_id); +return -EIO ; + } + +if (offset_id < start_id) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset_id %u, len %u, " +"start_id %u,id_count %u\n" , + offset_id, len, + start_id, id_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + id_diff = start_id - offset_id; + offset_id += id_diff; + logical_blk += id_diff; + len -= id_diff; + } + +if ((offset_id + len) > (start_id + id_count)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset_id %u, len %u, " +"start_id %u,id_count %u\n" , + offset_id, len, + start_id, id_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + is_partially_processed = true; + +/* correct lenght */ + len = (start_id + id_count) - offset_id; + } + + pos_array_items = portion->capacity - logical_blk; + +if (pos_array_items < len) { +SSDFS_ERR ("array_items %u < len %u\n" , + pos_array_items, len); +return -EINVAL ; + } + +if (id_count >atomic_read (&frag->id_count)) { +SSDFS_ERR ("id_count %u > frag->id_count %d\n" , + id_count, +atomic_read (&frag->id_count)); +return -EIO ; + } + + phys_off_index = offset_id - start_id; + +if ((phys_off_index + len) > id_count) { +SSDFS_ERR ("phys_off_index %d, len %u, id_count %u\n" , + phys_off_index, len, id_count); +return -EIO ; + } + +bitmap_clear (portion->bmap,0 , portion->capacity); + +down_read (&frag->lock); + +#ifdef CONFIG_SSDFS_DEBUG +for (j =0 ; j <pagevec_count (portion->blk_desc_pvec); j++) { +void *kaddr; +struct page *page = portion->blk_desc_pvec->pages[j]; + + kaddr =kmap_local_page (page); +SSDFS_DBG ("PAGE DUMP: index %d\n" , + j); +print_hex_dump_bytes ("" ,DUMP_PREFIX_OFFSET , + kaddr, +PAGE_SIZE ); +SSDFS_DBG ("\n" ); +kunmap_local (kaddr); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i < len; i++) { +size_t area_tbl_size =sizeof (struct ssdfs_area_block_table ); +size_t desc_size =sizeof (struct ssdfs_block_descriptor ); +struct ssdfs_offset_position *pos; +u16 id = offset_id + i; +u16 cur_blk; +u32 byte_offset; +bool is_invalid = false; + + phys_off = &frag->phys_offs[phys_off_index + i]; + + cur_blk =le16_to_cpu (phys_off->page_desc.logical_blk); + byte_offset =le32_to_cpu (phys_off->blk_state.byte_offset); + +if (byte_offset < area_tbl_size) { + err = -EIO ; +SSDFS_ERR ("corrupted phys offset: " +"byte_offset %u, area_tbl_size %zu\n" , + byte_offset, area_tbl_size); + goto finish_process_fragment; + } + + byte_offset -= area_tbl_size; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_blk %u, byte_offset %u\n" , + cur_blk, byte_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (cur_blk >= portion->capacity) { + err = -EIO ; +SSDFS_ERR ("logical_blk %u >= portion->capacity %u\n" , + cur_blk, portion->capacity); + goto finish_process_fragment; + } + +if (cur_blk < logical_blk || cur_blk >= (logical_blk + len)) { + err = -EIO ; +SSDFS_ERR ("cur_blk %u, logical_blk %u, len %u\n" , + cur_blk, logical_blk, len); + goto finish_process_fragment; + } + + pos =SSDFS_OFF_POS (ssdfs_dynamic_array_get_locked (lblk2off, + cur_blk)); +if (IS_ERR_OR_NULL (pos)) { + err = (pos ==NULL ? -ENOENT :PTR_ERR (pos)); +SSDFS_ERR ("fail to get logical block: " +"cur_blk %u, err %d\n" , + cur_blk, err); + goto finish_process_fragment; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("portion->cno %#llx, " +"pos (cno %#llx, id %u, peb_index %u, " +"sequence_id %u, offset_index %u)\n" , + portion->cno, + pos->cno, pos->id, pos->peb_index, + pos->sequence_id, pos->offset_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (is_ssdfs_offset_position_older (pos, portion->cno)) { +/* logical block has been initialized already */ +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("logical block %u has been initialized already\n" , + cur_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + err =ssdfs_dynamic_array_release (lblk2off, + cur_blk, pos); +if (unlikely (err)) { +SSDFS_ERR ("fail to release: " +"cur_blk %u, err %d\n" , + cur_blk, err); + goto finish_process_fragment; + }else +continue ; + } + + peb_index = portion->peb_index; + +bitmap_set (portion->bmap, cur_blk,1 ); + + pos->cno = portion->cno; + pos->id = id; + pos->peb_index = peb_index; + pos->sequence_id = sequence_id; + pos->offset_index = phys_off_index + i; + + err =ssdfs_unaligned_read_pagevec (portion->blk_desc_pvec, + byte_offset, + desc_size, + &pos->blk_desc.buf); +if (err == -E2BIG ) { + err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable init block descriptor: " +"logical block %u\n" , + cur_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + pos->blk_desc.status =SSDFS_BLK_DESC_BUF_UNKNOWN_STATE ; +memset (&pos->blk_desc.buf,0xFF , +sizeof (desc_size)); + }else if (unlikely (err)) { +SSDFS_ERR ("fail to read block descriptor: " +"cur_blk %u, err %d\n" , + cur_blk, err); +ssdfs_dynamic_array_release (lblk2off, + cur_blk, pos); + goto finish_process_fragment; + }else + pos->blk_desc.status =SSDFS_BLK_DESC_BUF_INITIALIZED ; + + state_off = &pos->blk_desc.buf.state[0 ]; + +switch (pos->blk_desc.status) { +case SSDFS_BLK_DESC_BUF_INITIALIZED : + is_invalid = +IS_SSDFS_BLK_STATE_OFFSET_INVALID (state_off); +break ; + +default : + is_invalid = false; +break ; + } + +if (is_invalid) { + err = -ERANGE ; +SSDFS_ERR ("block state offset invalid\n" ); + +SSDFS_ERR ("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_ERR ("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); + } + +ssdfs_dynamic_array_release (lblk2off, cur_blk, pos); + +#ifdef CONFIG_SSDFS_DEBUG +BUG (); +#endif /* CONFIG_SSDFS_DEBUG */ + + goto finish_process_fragment; + } + +#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); + } + +SSDFS_DBG ("set init bitmap: cur_blk %u\n" , + cur_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_dynamic_array_release (lblk2off, cur_blk, pos); +if (unlikely (err)) { +SSDFS_ERR ("fail to release: " +"cur_blk %u, err %d\n" , + cur_blk, err); + goto finish_process_fragment; + } + + err =ssdfs_blk2off_table_bmap_set (&portion->table->lbmap, +SSDFS_LBMAP_INIT_INDEX , + cur_blk); +if (unlikely (err)) { +SSDFS_ERR ("fail to set init bitmap: " +"logical_blk %u, err %d\n" , + cur_blk, err); + goto finish_process_fragment; + } + } + +finish_process_fragment: +up_read (&frag->lock); + +if (unlikely (err)) +return err; + +if (bitmap_intersects (portion->bmap, + portion->table->lbmap.array[SSDFS_LBMAP_STATE_INDEX ], + portion->table->lbmap.bits_count)) { +SSDFS_ERR ("invalid translation extent: " +"logical_blk %u, offset_id %u, len %u\n" , + logical_blk, offset_id, len); +return -EIO ; + } + +bitmap_or (portion->table->lbmap.array[SSDFS_LBMAP_STATE_INDEX ], + portion->bmap, + portion->table->lbmap.array[SSDFS_LBMAP_STATE_INDEX ], + portion->table->lbmap.bits_count); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("init_bmap %lx, state_bmap %lx, modification_bmap %lx\n" , + *portion->table->lbmap.array[SSDFS_LBMAP_INIT_INDEX ], + *portion->table->lbmap.array[SSDFS_LBMAP_STATE_INDEX ], + *portion->table->lbmap.array[SSDFS_LBMAP_MODIFICATION_INDEX ]); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (is_partially_processed) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("extent has been processed partially: " +"index %u\n" , *extent_index); +#endif /* CONFIG_SSDFS_DEBUG */ +return -EAGAIN ; + } + +return 0 ; +} + +/* +* ssdfs_process_free_translation_extent() - process free translation extent +* @portion: pointer on portion init environment [in | out] +* @extent_index: index of extent +* +* This method checks translation extent, to set bitmap for +* logical blocks in the extent and to fill portion of +* offset position array by physical offsets id. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-EIO - corrupted translation extent. +*/ +static +int ssdfs_process_free_translation_extent (struct ssdfs_blk2off_init *portion, +int *extent_index) +{ +struct ssdfs_sequence_array *sequence =NULL ; +struct ssdfs_phys_offset_table_fragment *frag =NULL ; +struct ssdfs_translation_extent *extent =NULL ; +struct ssdfs_dynamic_array *lblk2off; +void *ptr; +u16 peb_index; +u16 sequence_id; +u16 pos_array_items; +size_t pos_size =sizeof (struct ssdfs_offset_position ); +u32 logical_blk; +u16 len; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!portion || !extent_index); +BUG_ON (!portion->extent_array); +BUG_ON (portion->cno ==SSDFS_INVALID_CNO ); +BUG_ON (*extent_index >= portion->extents_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + lblk2off = &portion->table->lblk2off; + + peb_index = portion->peb_index; + sequence_id =le16_to_cpu (portion->pot_hdr.sequence_id); + + sequence = portion->table->peb[peb_index].sequence; + ptr =ssdfs_sequence_array_get_item (sequence, sequence_id); +if (IS_ERR_OR_NULL (ptr)) { + err = (ptr ==NULL ? -ENOENT :PTR_ERR (ptr)); +SSDFS_ERR ("fail to get fragment: " +"sequence_id %u, err %d\n" , + sequence_id, err); +return err; + } + frag = (struct ssdfs_phys_offset_table_fragment *)ptr; + + extent = &portion->extent_array[*extent_index]; + logical_blk =le16_to_cpu (extent->logical_blk); + len =le16_to_cpu (extent->len); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("logical_blk %u, len %u, " +"sequence_id %u, state %#x\n" , + logical_blk, len, + extent->sequence_id, extent->state); +#endif /* CONFIG_SSDFS_DEBUG */ + + pos_array_items = portion->capacity - logical_blk; + +if (pos_array_items < len) { +SSDFS_ERR ("array_items %u < len %u\n" , + pos_array_items, len); +return -EINVAL ; + } + + err =ssdfs_check_translation_extent (extent, portion->capacity, + *extent_index); +if (err) { +SSDFS_ERR ("invalid translation extent: " +"sequence_id %u, err %d\n" , + *extent_index, err); +return err; + } + +down_read (&frag->lock); + +for (i =0 ; i < len; i++) { +struct ssdfs_offset_position *pos; +u32 cur_blk = logical_blk + i; + + pos =SSDFS_OFF_POS (ssdfs_dynamic_array_get_locked (lblk2off, + cur_blk)); +if (IS_ERR_OR_NULL (pos)) { + err = (pos ==NULL ? -ENOENT :PTR_ERR (pos)); +SSDFS_ERR ("fail to get logical block: " +"cur_blk %u, err %d\n" , + cur_blk, err); + goto finish_process_fragment; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("portion->cno %#llx, " +"pos (cno %#llx, id %u, peb_index %u, " +"sequence_id %u, offset_index %u)\n" , + portion->cno, + pos->cno, pos->id, pos->peb_index, + pos->sequence_id, pos->offset_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (is_ssdfs_offset_position_older (pos, portion->cno)) { +/* logical block has been initialized already */ +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("logical block %u has been initialized already\n" , + cur_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + err =ssdfs_dynamic_array_release (lblk2off, + cur_blk, pos); +if (unlikely (err)) { +SSDFS_ERR ("fail to release: " +"cur_blk %u, err %d\n" , + cur_blk, err); + goto finish_process_fragment; + }else +continue ; + } + + err =ssdfs_blk2off_table_bmap_clear (&portion->table->lbmap, +SSDFS_LBMAP_STATE_INDEX , + cur_blk); +if (unlikely (err)) { +SSDFS_ERR ("fail to clear state bitmap: " +"logical_blk %u, err %d\n" , + cur_blk, err); + goto finish_process_fragment; + } + +memset (pos,0xFF , pos_size); + + pos->cno = portion->cno; + pos->peb_index = portion->peb_index; + + err =ssdfs_dynamic_array_release (lblk2off, cur_blk, pos); +if (unlikely (err)) { +SSDFS_ERR ("fail to release: " +"cur_blk %u, err %d\n" , + cur_blk, err); + goto finish_process_fragment; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("set init bitmap: cur_blk %u\n" , + cur_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_blk2off_table_bmap_set (&portion->table->lbmap, +SSDFS_LBMAP_INIT_INDEX , + cur_blk); +if (unlikely (err)) { +SSDFS_ERR ("fail to set init bitmap: " +"logical_blk %u, err %d\n" , + cur_blk, err); + goto finish_process_fragment; + } + +/* +* Free block needs to be marked as modified +* with the goal not to lose the information +* about free blocks in the case of PEB migration. +* Because, offsets translation table's snapshot +* needs to contain information about free blocks. +*/ + err =ssdfs_blk2off_table_bmap_set (&portion->table->lbmap, +SSDFS_LBMAP_MODIFICATION_INDEX , + cur_blk); +if (unlikely (err)) { +SSDFS_ERR ("fail to set modification bitmap: " +"logical_blk %u, err %d\n" , + cur_blk, err); + goto finish_process_fragment; + } + } + +finish_process_fragment: +up_read (&frag->lock); + +return err; +} + +/* +* ssdfs_blk2off_fragment_init() - initialize portion's fragment +* @portion: pointer on portion init environment [in | out] +* @fragment_index: index of fragment +* @extent_index: pointer on extent index [in | out] +*/ +static +int ssdfs_blk2off_fragment_init (struct ssdfs_blk2off_init *portion, +int fragment_index, +int *extent_index) +{ +struct ssdfs_sequence_array *sequence =NULL ; +struct ssdfs_translation_extent *extent =NULL ; +u16 logical_blk; +u16 offset_id; +u16 len; +u16 start_id; +u16 id_count; +u16 processed_offset_ids =0 ; +int state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!portion || !portion->table || !portion->blk2off_pvec); +BUG_ON (!portion->bmap || !portion->extent_array); +BUG_ON (!extent_index); +BUG_ON (portion->peb_index >= portion->table->pebs_count); + +SSDFS_DBG ("peb_index %u, fragment_index %d, " +"extent_index %u, extents_count %u\n" , + portion->peb_index, fragment_index, + *extent_index, portion->extents_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (fragment_index ==0 ) { + portion->pot_hdr_off = portion->tbl_hdr_off + +le16_to_cpu (portion->tbl_hdr.offset_table_off); + err =ssdfs_get_fragment_header (portion, portion->pot_hdr_off); + }else { + portion->pot_hdr_off = portion->tbl_hdr_off + +le16_to_cpu (portion->pot_hdr.next_fragment_off); + err =ssdfs_get_fragment_header (portion, portion->pot_hdr_off); + } + +if (err) { +SSDFS_ERR ("fail to get fragment header: err %d\n" , + err); +return err; + } + + err =ssdfs_get_checked_fragment (portion); +if (err == -EEXIST ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("fragment has been initialized already: " +"peb_index %u, offset %u\n" , + portion->peb_index, + portion->pot_hdr_off); +#endif /* CONFIG_SSDFS_DEBUG */ +return err; + }else if (err) { +SSDFS_ERR ("fail to get checked fragment: " +"peb_index %u, offset %u, err %d\n" , + portion->peb_index, + portion->pot_hdr_off, err); +return err; + } + +if (*extent_index >= portion->extents_count) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("extent_index %u >= extents_count %u\n" , + *extent_index, portion->extents_count); +#endif /* CONFIG_SSDFS_DEBUG */ + } + + start_id =le16_to_cpu (portion->pot_hdr.start_id); + id_count =le16_to_cpu (portion->pot_hdr.id_count); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start_id %u, id_count %u\n" , + start_id, id_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +while (*extent_index < portion->extents_count) { + extent = &portion->extent_array[*extent_index]; + logical_blk =le16_to_cpu (extent->logical_blk); + offset_id =le16_to_cpu (extent->offset_id); + len =le16_to_cpu (extent->len); + state = extent->state; + +if (processed_offset_ids > id_count) { +SSDFS_ERR ("processed_offset_ids %u > id_count %u\n" , + processed_offset_ids, id_count); +return -ERANGE ; + }else if (processed_offset_ids == id_count) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("fragment has been processed: " +"processed_offset_ids %u == id_count %u\n" , + processed_offset_ids, id_count); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_fragment_processing; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("logical_blk %u, len %u, " +"state %#x, extent_index %d\n" , + logical_blk, len, state, *extent_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (logical_blk >= portion->capacity) { + err = -ERANGE ; +SSDFS_ERR ("logical_blk %u >= capacity %u\n" , + logical_blk, portion->capacity); +return err; + } + +if (state !=SSDFS_LOGICAL_BLK_FREE ) { +if (offset_id >= (start_id + id_count)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset_id %u, start_id %u, " +"id_count %u\n" , + offset_id, start_id, id_count); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_fragment_processing; + } + } + +if (state ==SSDFS_LOGICAL_BLK_USED ) { + err =ssdfs_process_used_translation_extent (portion, + extent_index); +if (err == -EAGAIN ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("extent has been processed partially: " +"sequence_id %u, err %d\n" , + *extent_index, err); +#endif /* CONFIG_SSDFS_DEBUG */ + }else if (unlikely (err)) { +SSDFS_ERR ("invalid translation extent: " +"sequence_id %u, err %d\n" , + *extent_index, err); +return err; + } + }else if (state ==SSDFS_LOGICAL_BLK_FREE ) { + err =ssdfs_process_free_translation_extent (portion, + extent_index); +if (err) { +SSDFS_ERR ("invalid translation extent: " +"sequence_id %u, err %d\n" , + *extent_index, err); +return err; + } + }else +BUG (); + +if (err == -EAGAIN ) { +SSDFS_DBG ("don't increment extent index\n" ); + goto finish_fragment_processing; + }else + ++*extent_index; + +if (state !=SSDFS_LOGICAL_BLK_FREE ) + processed_offset_ids += len; + }; + +finish_fragment_processing: +if (portion->table->init_cno ==U64_MAX || + portion->cno >= portion->table->init_cno) { +u16 peb_index = portion->peb_index; +u16 sequence_id =le16_to_cpu (portion->pot_hdr.sequence_id); + + sequence = portion->table->peb[peb_index].sequence; + +if (is_ssdfs_sequence_array_last_id_invalid (sequence) || +ssdfs_sequence_array_last_id (sequence) <= sequence_id) { + portion->table->init_cno = portion->cno; + portion->table->used_logical_blks = +le16_to_cpu (portion->pot_hdr.used_logical_blks); + portion->table->free_logical_blks = +le16_to_cpu (portion->pot_hdr.free_logical_blks); + portion->table->last_allocated_blk = +le16_to_cpu (portion->pot_hdr.last_allocated_blk); + +ssdfs_sequence_array_set_last_id (sequence, sequence_id); + } + } + +atomic_inc (&portion->table->peb[portion->peb_index].fragment_count); + +return err; +} + +/* +* ssdfs_define_peb_table_state() - define PEB's table state +* @table: pointer on translation table object +* @peb_index: PEB's index +*/ +static inline +int ssdfs_define_peb_table_state (struct ssdfs_blk2off_table *table, +u16 peb_index) +{ +int state; +u16 last_allocated_blk; +u16 allocated_blks; +int init_bits; +int count; +unsigned long last_id; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table); +BUG_ON (peb_index >= table->pebs_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + count =atomic_read (&table->peb[peb_index].fragment_count); + last_id =ssdfs_sequence_array_last_id (table->peb[peb_index].sequence); + last_allocated_blk = table->last_allocated_blk; + +if (last_allocated_blk >=U16_MAX ) + allocated_blks =0 ; +else + allocated_blks = last_allocated_blk +1 ; + + init_bits =bitmap_weight (table->lbmap.array[SSDFS_LBMAP_INIT_INDEX ], + allocated_blks); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("table %p, peb_index %u, count %d, last_id %lu, " +"last_allocated_blk %u, init_bits %d, " +"allocated_blks %u\n" , + table, peb_index, count, last_id, + last_allocated_blk, init_bits, + allocated_blks); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (init_bits <0 ) { +SSDFS_ERR ("invalid init bmap: weight %d\n" , + init_bits); +return -ERANGE ; + } + +if (count ==0 ) { +SSDFS_ERR ("fragment_count == 0\n" ); +return -ERANGE ; + } + + state =atomic_cmpxchg (&table->peb[peb_index].state, +SSDFS_BLK2OFF_TABLE_CREATED , +SSDFS_BLK2OFF_TABLE_PARTIAL_INIT ); +if (state <=SSDFS_BLK2OFF_TABLE_UNDEFINED || + state >SSDFS_BLK2OFF_TABLE_DIRTY_PARTIAL_INIT ) { +SSDFS_WARN ("unexpected state %#x\n" , + state); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("state %#x\n" , state); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (init_bits >0 ) { +if (init_bits >= allocated_blks) { + state =atomic_cmpxchg (&table->peb[peb_index].state, +SSDFS_BLK2OFF_TABLE_PARTIAL_INIT , +SSDFS_BLK2OFF_TABLE_COMPLETE_INIT ); +if (state ==SSDFS_BLK2OFF_TABLE_PARTIAL_INIT ) { +/* table is completely initialized */ + goto finish_define_peb_table_state; + } + + state =atomic_cmpxchg (&table->peb[peb_index].state, +SSDFS_BLK2OFF_TABLE_DIRTY_PARTIAL_INIT , +SSDFS_BLK2OFF_TABLE_DIRTY ); +if (state ==SSDFS_BLK2OFF_TABLE_DIRTY_PARTIAL_INIT ) { +/* table is dirty already */ + goto finish_define_peb_table_state; + } + +if (state <SSDFS_BLK2OFF_TABLE_PARTIAL_INIT || + state >SSDFS_BLK2OFF_TABLE_COMPLETE_INIT ) { +SSDFS_WARN ("unexpected state %#x\n" , + state); +return -ERANGE ; + } + } + }else { +SSDFS_WARN ("init_bits == 0\n" ); +return -ERANGE ; + } + +finish_define_peb_table_state: +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("state %#x\n" ,atomic_read (&table->peb[peb_index].state)); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_define_blk2off_table_object_state() - define table object state +* @table: pointer on translation table object +*/ +static inline +int ssdfs_define_blk2off_table_object_state (struct ssdfs_blk2off_table *table) +{ +int state; +int i; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table); +#endif /* CONFIG_SSDFS_DEBUG */ + + state =SSDFS_BLK2OFF_TABLE_STATE_MAX ; +for (i =0 ; i < table->pebs_count; i++) { +int peb_tbl_state =atomic_read (&table->peb[i].state); + +if (peb_tbl_state < state) + state = peb_tbl_state; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("table %p, state %#x\n" , table, state); +#endif /* CONFIG_SSDFS_DEBUG */ + +switch (state) { +case SSDFS_BLK2OFF_TABLE_CREATED : + state =atomic_read (&table->state); +if (state !=SSDFS_BLK2OFF_OBJECT_CREATED ) { +SSDFS_WARN ("unexpected state %#x\n" , + state); +return -ERANGE ; + } +break ; + +case SSDFS_BLK2OFF_TABLE_PARTIAL_INIT : +case SSDFS_BLK2OFF_TABLE_DIRTY_PARTIAL_INIT : + state =atomic_cmpxchg (&table->state, +SSDFS_BLK2OFF_OBJECT_CREATED , +SSDFS_BLK2OFF_OBJECT_PARTIAL_INIT ); +complete_all (&table->partial_init_end); + +if (state <=SSDFS_BLK2OFF_OBJECT_UNKNOWN || + state >SSDFS_BLK2OFF_OBJECT_PARTIAL_INIT ) { +SSDFS_WARN ("unexpected state %#x\n" , + state); +return -ERANGE ; + } +break ; + +case SSDFS_BLK2OFF_TABLE_COMPLETE_INIT : +case SSDFS_BLK2OFF_TABLE_DIRTY : + state =atomic_cmpxchg (&table->state, +SSDFS_BLK2OFF_OBJECT_PARTIAL_INIT , +SSDFS_BLK2OFF_OBJECT_COMPLETE_INIT ); +if (state ==SSDFS_BLK2OFF_OBJECT_CREATED ) { + state =atomic_cmpxchg (&table->state, +SSDFS_BLK2OFF_OBJECT_CREATED , +SSDFS_BLK2OFF_OBJECT_COMPLETE_INIT ); + } +complete_all (&table->partial_init_end); +complete_all (&table->full_init_end); + +if (state <SSDFS_BLK2OFF_OBJECT_CREATED || + state >SSDFS_BLK2OFF_OBJECT_COMPLETE_INIT ) { +SSDFS_WARN ("unexpected state %#x\n" , + state); +return -ERANGE ; + } +break ; + +default : +SSDFS_WARN ("unexpected state %#x\n" , state); +return -ERANGE ; + }; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("state %#x\n" ,atomic_read (&table->state)); +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +/* +* ssdfs_blk2off_table_partial_init() - initialize PEB's table fragment +* @table: pointer on translation table object +* @blk2off_pvec: blk2off fragment +* @blk_desc_pvec: blk desc fragment +* @peb_index: PEB's index +* @cno: fragment's checkpoint (log's checkpoint) +* +* This method tries to initialize PEB's table fragment. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-EIO - corrupted translation extent. +*/ +int ssdfs_blk2off_table_partial_init (struct ssdfs_blk2off_table *table, +struct pagevec *blk2off_pvec, +struct pagevec *blk_desc_pvec, +u16 peb_index, +u64 cno) +{ +struct ssdfs_blk2off_init portion; +int extent_index =0 ; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table || !blk2off_pvec || !blk_desc_pvec); +BUG_ON (peb_index >= table->pebs_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("table %p, peb_index %u\n" , + table, peb_index); +#else +SSDFS_DBG ("table %p, peb_index %u\n" , + table, peb_index); +#endif /* CONFIG_SSDFS_TRACK_API_CALL */ + +memset (&portion,0 ,sizeof (struct ssdfs_blk2off_init )); + +if (pagevec_count (blk2off_pvec) ==0 ) { +SSDFS_ERR ("fail to init because of empty pagevec\n" ); +return -EINVAL ; + } + +if (ssdfs_blk2off_table_initialized (table, peb_index)) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("PEB's table has been initialized already: " +"peb_index %u\n" , + peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ +return 0 ; + } + + portion.table = table; + portion.blk2off_pvec = blk2off_pvec; + portion.blk_desc_pvec = blk_desc_pvec; + portion.peb_index = peb_index; + portion.cno = cno; + + portion.tbl_hdr_off =0 ; + err =ssdfs_get_checked_table_header (&portion); +if (err) { +SSDFS_ERR ("invalid table header\n" ); +return err; + } + +down_write (&table->translation_lock); + + portion.capacity = table->lblk2off_capacity; + + err =ssdfs_blk2off_prepare_temp_bmap (&portion); +if (unlikely (err)) { +SSDFS_ERR ("fail to allocate memory\n" ); + goto unlock_translation_table; + } + + err =ssdfs_blk2off_prepare_extent_array (&portion); +if (unlikely (err)) { +SSDFS_ERR ("fail to allocate memory\n" ); + goto unlock_translation_table; + } + + portion.pot_hdr_off = portion.tbl_hdr_off + +le16_to_cpu (portion.tbl_hdr.offset_table_off); + +for (i =0 ; i < portion.fragments_count; i++) { + err =ssdfs_blk2off_fragment_init (&portion, + i, + &extent_index); +if (err == -EAGAIN ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("continue to process extent: " +"fragment %d, extent_index %d\n" , + i, extent_index); +#endif /* CONFIG_SSDFS_DEBUG */ +continue ; + }else if (err == -EEXIST ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("fragment has been initiliazed already: " +"fragment_index %d, extent_index %d\n" , + i, extent_index); +#endif /* CONFIG_SSDFS_DEBUG */ +continue ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to initialize fragment: " +"fragment_index %d, extent_index %d, " +"err %d\n" , + i, extent_index, err); + goto unlock_translation_table; + } + } + + err =ssdfs_define_peb_table_state (table, peb_index); +if (err) { +SSDFS_ERR ("fail to define PEB's table state: " +"peb_index %u, err %d\n" , + peb_index, err); + goto unlock_translation_table; + } + + err =ssdfs_define_blk2off_table_object_state (table); +if (err) { +SSDFS_ERR ("fail to define table object state: " +"err %d\n" , + err); + goto unlock_translation_table; + } + +unlock_translation_table: +up_write (&table->translation_lock); + +ssdfs_blk2off_kvfree (portion.bmap); + portion.bmap =NULL ; +ssdfs_blk2off_kfree (portion.extent_array); + portion.extent_array =NULL ; + +#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; +}
fs/ssdfs/offset_translation_table.h new file +446 -0
86 lines not on this page · 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.
RFC PATCH 18/76 ssdfs: flush offset translation table
In reply to Viacheslav DubeykoOffset 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. Flush logic identifies dirty fragments and stores these fragments as part of log's metadata during log commit operation.
--- fs/ssdfs/offset_translation_table.c | 2250 +++++++++++++++++++++++++++ 1 file changed, 2250 insertions(+)
fs/ssdfs/offset_translation_table.c +2250 -0
@@ -2912,3 +2912,2253 @@ int ssdfs_blk2off_table_partial_init(struct ssdfs_blk2off_table *table,return err; } + +const u16 last_used_blk[U8_MAX +1 ] = { +/* 00 - 0x00 */ U16_MAX ,0 ,1 ,1 , +/* 01 - 0x04 */ 2 ,2 ,2 ,2 , +/* 02 - 0x08 */ 3 ,3 ,3 ,3 , +/* 03 - 0x0C */ 3 ,3 ,3 ,3 , +/* 04 - 0x10 */ 4 ,4 ,4 ,4 , +/* 05 - 0x14 */ 4 ,4 ,4 ,4 , +/* 06 - 0x18 */ 4 ,4 ,4 ,4 , +/* 07 - 0x1C */ 4 ,4 ,4 ,4 , +/* 08 - 0x20 */ 5 ,5 ,5 ,5 , +/* 09 - 0x24 */ 5 ,5 ,5 ,5 , +/* 10 - 0x28 */ 5 ,5 ,5 ,5 , +/* 11 - 0x2C */ 5 ,5 ,5 ,5 , +/* 12 - 0x30 */ 5 ,5 ,5 ,5 , +/* 13 - 0x34 */ 5 ,5 ,5 ,5 , +/* 14 - 0x38 */ 5 ,5 ,5 ,5 , +/* 15 - 0x3C */ 5 ,5 ,5 ,5 , +/* 16 - 0x40 */ 6 ,6 ,6 ,6 , +/* 17 - 0x44 */ 6 ,6 ,6 ,6 , +/* 18 - 0x48 */ 6 ,6 ,6 ,6 , +/* 19 - 0x4C */ 6 ,6 ,6 ,6 , +/* 20 - 0x50 */ 6 ,6 ,6 ,6 , +/* 21 - 0x54 */ 6 ,6 ,6 ,6 , +/* 22 - 0x58 */ 6 ,6 ,6 ,6 , +/* 23 - 0x5C */ 6 ,6 ,6 ,6 , +/* 24 - 0x60 */ 6 ,6 ,6 ,6 , +/* 25 - 0x64 */ 6 ,6 ,6 ,6 , +/* 26 - 0x68 */ 6 ,6 ,6 ,6 , +/* 27 - 0x6C */ 6 ,6 ,6 ,6 , +/* 28 - 0x70 */ 6 ,6 ,6 ,6 , +/* 29 - 0x74 */ 6 ,6 ,6 ,6 , +/* 30 - 0x78 */ 6 ,6 ,6 ,6 , +/* 31 - 0x7C */ 6 ,6 ,6 ,6 , +/* 32 - 0x80 */ 7 ,7 ,7 ,7 , +/* 33 - 0x84 */ 7 ,7 ,7 ,7 , +/* 34 - 0x88 */ 7 ,7 ,7 ,7 , +/* 35 - 0x8C */ 7 ,7 ,7 ,7 , +/* 36 - 0x90 */ 7 ,7 ,7 ,7 , +/* 37 - 0x94 */ 7 ,7 ,7 ,7 , +/* 38 - 0x98 */ 7 ,7 ,7 ,7 , +/* 39 - 0x9C */ 7 ,7 ,7 ,7 , +/* 40 - 0xA0 */ 7 ,7 ,7 ,7 , +/* 41 - 0xA4 */ 7 ,7 ,7 ,7 , +/* 42 - 0xA8 */ 7 ,7 ,7 ,7 , +/* 43 - 0xAC */ 7 ,7 ,7 ,7 , +/* 44 - 0xB0 */ 7 ,7 ,7 ,7 , +/* 45 - 0xB4 */ 7 ,7 ,7 ,7 , +/* 46 - 0xB8 */ 7 ,7 ,7 ,7 , +/* 47 - 0xBC */ 7 ,7 ,7 ,7 , +/* 48 - 0xC0 */ 7 ,7 ,7 ,7 , +/* 49 - 0xC4 */ 7 ,7 ,7 ,7 , +/* 50 - 0xC8 */ 7 ,7 ,7 ,7 , +/* 51 - 0xCC */ 7 ,7 ,7 ,7 , +/* 52 - 0xD0 */ 7 ,7 ,7 ,7 , +/* 53 - 0xD4 */ 7 ,7 ,7 ,7 , +/* 54 - 0xD8 */ 7 ,7 ,7 ,7 , +/* 55 - 0xDC */ 7 ,7 ,7 ,7 , +/* 56 - 0xE0 */ 7 ,7 ,7 ,7 , +/* 57 - 0xE4 */ 7 ,7 ,7 ,7 , +/* 58 - 0xE8 */ 7 ,7 ,7 ,7 , +/* 59 - 0xEC */ 7 ,7 ,7 ,7 , +/* 60 - 0xF0 */ 7 ,7 ,7 ,7 , +/* 61 - 0xF4 */ 7 ,7 ,7 ,7 , +/* 62 - 0xF8 */ 7 ,7 ,7 ,7 , +/* 63 - 0xFC */ 7 ,7 ,7 ,7 +}; + +/* +* ssdfs_blk2off_table_find_last_valid_block() - find last valid block +* @table: pointer on translation table object +* +* RETURN: +* [success] - last valid logical block number. +* [failure] - U16_MAX. +*/ +static +u16 ssdfs_blk2off_table_find_last_valid_block (struct ssdfs_blk2off_table *table) +{ +u16 logical_blk; +unsigned long *lbmap; +unsigned char *byte; +int long_count, byte_count; +int i, j; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table); +BUG_ON (!rwsem_is_locked (&table->translation_lock)); +#endif /* CONFIG_SSDFS_DEBUG */ + + logical_blk =U16_MAX ; + long_count =BITS_TO_LONGS (table->lbmap.bits_count); + lbmap = table->lbmap.array[SSDFS_LBMAP_STATE_INDEX ]; + +for (i = long_count -1 ; i >=0 ; i--) { +if (lbmap[i] !=0 ) { + byte_count =sizeof (unsigned long ); +for (j = byte_count -1 ; j >=0 ; j--) { + byte = (unsigned char *)lbmap[i] + j; + logical_blk = last_used_blk[*byte]; +if (logical_blk !=U16_MAX ) +break ; + } + goto calculate_logical_blk; + } + } + +calculate_logical_blk: +if (logical_blk !=U16_MAX ) + logical_blk += i *BITS_PER_LONG ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("table %p, logical_blk %u\n" , + table, logical_blk); +#endif /* CONFIG_SSDFS_DEBUG */ + +return logical_blk; +} + +/* +* ssdfs_blk2off_table_resize() - resize table +* @table: pointer on translation table object +* @new_items_count: new table size +* +* This method tries to grow or to shrink table. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - unable to shrink table. +* %-ENOMEM - unable to realloc table. +*/ +int ssdfs_blk2off_table_resize (struct ssdfs_blk2off_table *table, +u16 new_items_count) +{ +u16 last_blk; +int diff; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table); + +SSDFS_DBG ("table %p, lblk2off_capacity %u, new_items_count %u\n" , + table, table->lblk2off_capacity, new_items_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&table->translation_lock); + +if (new_items_count == table->lblk2off_capacity) { +SSDFS_WARN ("new_items_count %u == lblk2off_capacity %u\n" , + new_items_count, table->lblk2off_capacity); + goto finish_table_resize; + }else if (new_items_count < table->lblk2off_capacity) { + last_blk =ssdfs_blk2off_table_find_last_valid_block (table); + +if (last_blk !=U16_MAX && last_blk >= new_items_count) { + err = -ERANGE ; +SSDFS_ERR ("unable to shrink bitmap: " +"last_blk %u >= new_items_count %u\n" , + last_blk, new_items_count); + goto finish_table_resize; + } + } + + diff = (int )new_items_count - table->lblk2off_capacity; + + table->lblk2off_capacity = new_items_count; + table->free_logical_blks += diff; + +finish_table_resize: +up_write (&table->translation_lock); + +return err; +} + +/* +* ssdfs_blk2off_table_dirtied() - check that PEB's table is dirty +* @table: pointer on translation table object +* @peb_index: PEB's index +*/ +bool ssdfs_blk2off_table_dirtied (struct ssdfs_blk2off_table *table, +u16 peb_index) +{ +bool is_dirty = false; +struct ssdfs_phys_offset_table_array *phys_off_table; +struct ssdfs_sequence_array *sequence; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table); +BUG_ON (!table->peb); +BUG_ON (peb_index >= table->pebs_count); + +SSDFS_DBG ("table %p, peb_index %u\n" , + table, peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + + phys_off_table = &table->peb[peb_index]; + sequence = phys_off_table->sequence; + is_dirty =has_ssdfs_sequence_array_state (sequence, +SSDFS_SEQUENCE_ITEM_DIRTY_TAG ); + +switch (atomic_read (&phys_off_table->state)) { +case SSDFS_BLK2OFF_TABLE_DIRTY : +case SSDFS_BLK2OFF_TABLE_DIRTY_PARTIAL_INIT : +if (!is_dirty) { +/* table is dirty without dirty fragments */ +SSDFS_WARN ("table is marked as dirty!\n" ); + } +break ; + +default : +if (is_dirty) { +/* there are dirty fragments but table is clean */ +SSDFS_WARN ("table is not dirty\n" ); + } +break ; + } + +return is_dirty; +} + +/* +* ssdfs_blk2off_table_initialized() - check that PEB's table is initialized +* @table: pointer on translation table object +* @peb_index: PEB's index +*/ +bool ssdfs_blk2off_table_initialized (struct ssdfs_blk2off_table *table, +u16 peb_index) +{ +int state; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table); +BUG_ON (peb_index >= table->pebs_count); + +SSDFS_DBG ("table %p, peb_index %u\n" , + table, peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +BUG_ON (!table->peb); + + state =atomic_read (&table->peb[peb_index].state); + +return state >=SSDFS_BLK2OFF_TABLE_COMPLETE_INIT && + state <SSDFS_BLK2OFF_TABLE_STATE_MAX ; +} + +static +int ssdfs_change_fragment_state (void *item,int old_state,int new_state) +{ +struct ssdfs_phys_offset_table_fragment *fragment = + (struct ssdfs_phys_offset_table_fragment *)item; +int state; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("old_state %#x, new_state %#x\n" , + old_state, new_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (!fragment) { +SSDFS_ERR ("pointer is NULL\n" ); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sequence_id %u, state %#x\n" , + fragment->sequence_id, +atomic_read (&fragment->state)); +#endif /* CONFIG_SSDFS_DEBUG */ + + state =atomic_cmpxchg (&fragment->state, old_state, new_state); + +switch (new_state) { +case SSDFS_BLK2OFF_FRAG_DIRTY : +switch (state) { +case SSDFS_BLK2OFF_FRAG_CREATED : +case SSDFS_BLK2OFF_FRAG_INITIALIZED : +case SSDFS_BLK2OFF_FRAG_DIRTY : +/* expected old state */ +break ; + +default : +SSDFS_ERR ("invalid old_state %#x\n" , + old_state); +return -ERANGE ; + } +break ; + +default : +if (state != old_state) { +SSDFS_ERR ("state %#x != old_state %#x\n" , + state, old_state); +return -ERANGE ; + } +break ; + } + +return 0 ; +} + +static inline +int ssdfs_calculate_start_sequence_id (u16 last_sequence_id, +u16 dirty_fragments, +u16 *start_sequence_id) +{ +u16 upper_bound; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!start_sequence_id); + +SSDFS_DBG ("last_sequence_id %u, dirty_fragments %u\n" , + last_sequence_id, dirty_fragments); +#endif /* CONFIG_SSDFS_DEBUG */ + + *start_sequence_id =U16_MAX ; + +if (last_sequence_id >SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD ) { +SSDFS_ERR ("invalid last_sequence_id %u\n" , + last_sequence_id); +return -ERANGE ; + } + +if (dirty_fragments >SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD ) { +SSDFS_ERR ("invalid dirty_fragments %u\n" , + dirty_fragments); +return -ERANGE ; + } + + upper_bound = last_sequence_id +1 ; + +if (upper_bound >= dirty_fragments) + *start_sequence_id = upper_bound - dirty_fragments; +else { + *start_sequence_id =SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD - + (dirty_fragments - upper_bound); + } + +return 0 ; +} + +/* +* ssdfs_blk2off_table_snapshot() - get table's snapshot +* @table: pointer on translation table object +* @peb_index: PEB's index +* @snapshot: pointer on table's snapshot object +* +* This method tries to get table's snapshot. The @bmap_copy +* and @tbl_copy fields of snapshot object are allocated during +* getting snapshot by this method. Freeing of allocated +* memory SHOULD BE MADE by caller. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal logic error. +* %-ENOMEM - fail to allocate memory. +* %-ENODATA - PEB hasn't dirty fragments. +*/ +int ssdfs_blk2off_table_snapshot (struct ssdfs_blk2off_table *table, +u16 peb_index, +struct ssdfs_blk2off_table_snapshot *snapshot) +{ +struct ssdfs_phys_offset_table_array *pot_table; +struct ssdfs_sequence_array *sequence; +u32 capacity; +size_t bmap_bytes, tbl_bytes; +u16 last_sequence_id; +unsigned long dirty_fragments; +int state; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table || !snapshot); +BUG_ON (peb_index >= table->pebs_count); + +SSDFS_DBG ("table %p, peb_index %u, snapshot %p\n" , + table, peb_index, snapshot); +#endif /* CONFIG_SSDFS_DEBUG */ + +memset (snapshot,0 ,sizeof (struct ssdfs_blk2off_table_snapshot )); + snapshot->bmap_copy =NULL ; + snapshot->tbl_copy =NULL ; + +down_write (&table->translation_lock); + +if (!ssdfs_blk2off_table_dirtied (table, peb_index)) { + err = -ENODATA ; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("table isn't dirty for peb_index %u\n" , + peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ + goto finish_snapshoting; + } + + capacity =ssdfs_dynamic_array_items_count (&table->lblk2off); +if (capacity ==0 ) { + err = -ERANGE ; +SSDFS_ERR ("invalid capacity %u\n" , capacity); + goto finish_snapshoting; + } + + bmap_bytes =ssdfs_blk2off_table_bmap_bytes (table->lbmap.bits_count); + snapshot->bmap_copy =ssdfs_blk2off_kvzalloc (bmap_bytes,GFP_KERNEL ); +if (!snapshot->bmap_copy) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocated bytes %zu\n" , + bmap_bytes); + goto finish_snapshoting; + } + + tbl_bytes =ssdfs_dynamic_array_allocated_bytes (&table->lblk2off); +if (tbl_bytes ==0 ) { + err = -ERANGE ; +SSDFS_ERR ("invalid bytes count %zu\n" , tbl_bytes); + goto finish_snapshoting; + } + + snapshot->tbl_copy =ssdfs_blk2off_kvzalloc (tbl_bytes,GFP_KERNEL ); +if (!snapshot->tbl_copy) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocated bytes %zu\n" , + tbl_bytes); + goto finish_snapshoting; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("capacity %u, bits_count %u, " +"bmap_bytes %zu, tbl_bytes %zu, " +"last_allocated_blk %u\n" , + capacity, table->lbmap.bits_count, + bmap_bytes, tbl_bytes, + table->last_allocated_blk); +SSDFS_DBG ("init_bmap %lx, state_bmap %lx, bmap_copy %lx\n" , + *table->lbmap.array[SSDFS_LBMAP_INIT_INDEX ], + *table->lbmap.array[SSDFS_LBMAP_STATE_INDEX ], + *snapshot->bmap_copy); +#endif /* CONFIG_SSDFS_DEBUG */ + +bitmap_or (snapshot->bmap_copy, + snapshot->bmap_copy, + table->lbmap.array[SSDFS_LBMAP_MODIFICATION_INDEX ], + table->lbmap.bits_count); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("modification_bmap %lx, bmap_copy %lx\n" , + *table->lbmap.array[SSDFS_LBMAP_MODIFICATION_INDEX ], + *snapshot->bmap_copy); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_dynamic_array_copy_content (&table->lblk2off, + snapshot->tbl_copy, + tbl_bytes); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy position array: " +"err %d\n" , err); + goto finish_snapshoting; + } + + snapshot->capacity = capacity; + + snapshot->used_logical_blks = table->used_logical_blks; + snapshot->free_logical_blks = table->free_logical_blks; + snapshot->last_allocated_blk = table->last_allocated_blk; + + snapshot->peb_index = peb_index; + snapshot->start_sequence_id =SSDFS_INVALID_FRAG_ID ; + + sequence = table->peb[peb_index].sequence; + err =ssdfs_sequence_array_change_all_states (sequence, +SSDFS_SEQUENCE_ITEM_DIRTY_TAG , +SSDFS_SEQUENCE_ITEM_UNDER_COMMIT_TAG , + ssdfs_change_fragment_state, +SSDFS_BLK2OFF_FRAG_DIRTY , +SSDFS_BLK2OFF_FRAG_UNDER_COMMIT , + &dirty_fragments); +if (unlikely (err)) { +SSDFS_ERR ("fail to change from dirty to under_commit: " +"err %d\n" , err); + goto finish_snapshoting; + }else if (dirty_fragments >=U16_MAX ) { + err = -ERANGE ; +SSDFS_ERR ("invalid dirty_fragments %lu\n" , + dirty_fragments); + goto finish_snapshoting; + } + +#ifdef CONFIG_SSDFS_SAVE_WHOLE_BLK2OFF_TBL_IN_EVERY_LOG + snapshot->start_sequence_id =0 ; + snapshot->dirty_fragments = dirty_fragments; + snapshot->fragments_count = +atomic_read (&table->peb[peb_index].fragment_count); +#else + snapshot->dirty_fragments = dirty_fragments; + + last_sequence_id = +ssdfs_sequence_array_last_id (table->peb[peb_index].sequence); + err =ssdfs_calculate_start_sequence_id (last_sequence_id, + snapshot->dirty_fragments, + &snapshot->start_sequence_id); +if (unlikely (err)) { +SSDFS_ERR ("fail to calculate start sequence ID: " +"err %d\n" , err); + goto finish_snapshoting; + } + + snapshot->fragments_count = +atomic_read (&table->peb[peb_index].fragment_count); +#endif /* CONFIG_SSDFS_SAVE_WHOLE_BLK2OFF_TBL_IN_EVERY_LOG */ + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("start_sequence_id %u, dirty_fragments %u\n" , + snapshot->start_sequence_id, + snapshot->dirty_fragments); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (snapshot->dirty_fragments ==0 ) { + err = -ERANGE ; +SSDFS_ERR ("PEB hasn't dirty fragments\n" ); + goto finish_snapshoting; + } + + snapshot->cno =ssdfs_current_cno (table->fsi->sb); + + pot_table = &table->peb[peb_index]; + state =atomic_cmpxchg (&pot_table->state, +SSDFS_BLK2OFF_TABLE_DIRTY_PARTIAL_INIT , +SSDFS_BLK2OFF_TABLE_PARTIAL_INIT ); +if (state !=SSDFS_BLK2OFF_TABLE_DIRTY_PARTIAL_INIT ) { + state =atomic_cmpxchg (&pot_table->state, +SSDFS_BLK2OFF_TABLE_DIRTY , +SSDFS_BLK2OFF_TABLE_COMPLETE_INIT ); +if (state !=SSDFS_BLK2OFF_TABLE_DIRTY ) { + err = -ERANGE ; +SSDFS_ERR ("table isn't dirty: " +"state %#x\n" , + state); + goto finish_snapshoting; + } + } + +finish_snapshoting: +up_write (&table->translation_lock); + +if (err) { +if (snapshot->bmap_copy) { +ssdfs_blk2off_kvfree (snapshot->bmap_copy); + snapshot->bmap_copy =NULL ; + } + +if (snapshot->tbl_copy) { +ssdfs_blk2off_kvfree (snapshot->tbl_copy); + snapshot->tbl_copy =NULL ; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("finished\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_blk2off_table_free_snapshot() - free snapshot's resources +* @sp: pointer on tabls's snapshot +*/ +void ssdfs_blk2off_table_free_snapshot (struct ssdfs_blk2off_table_snapshot *sp) +{ +if (!sp) +return ; + +if (sp->bmap_copy) { +ssdfs_blk2off_kvfree (sp->bmap_copy); + sp->bmap_copy =NULL ; + } + +if (sp->tbl_copy) { +ssdfs_blk2off_kvfree (sp->tbl_copy); + sp->tbl_copy =NULL ; + } + +memset (sp,0 ,sizeof (struct ssdfs_blk2off_table_snapshot )); +} + +/* +* ssdfs_find_changed_area() - find changed area +* @sp: table's snapshot +* @start: starting bit for search +* @found: found range of set bits +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal logic error. +* %-ENODATA - nothing was found. +*/ +static inline +int ssdfs_find_changed_area (struct ssdfs_blk2off_table_snapshot *sp, +unsigned long start, +struct ssdfs_blk2off_range *found) +{ +unsigned long modified_bits; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sp || !found); +#endif /* CONFIG_SSDFS_DEBUG */ + + modified_bits =bitmap_weight (sp->bmap_copy, sp->capacity); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("snapshot %p, peb_index %u, start %lu, found %p\n" , + sp, sp->peb_index, start, found); +SSDFS_DBG ("modified_bits %lu, capacity %u\n" , + modified_bits, sp->capacity); +#endif /* CONFIG_SSDFS_DEBUG */ + + start =find_next_bit (sp->bmap_copy, sp->capacity, start); +if (start >= sp->capacity) { +SSDFS_DBG ("nothing found\n" ); +return -ENODATA ; + } + + found->start_lblk = (u16 )start; + + start =find_next_zero_bit (sp->bmap_copy, sp->capacity, start); + start = (unsigned long )min_t (u16, (u16 )start, sp->capacity); + + found->len = (u16)(start - found->start_lblk); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("found_start %lu, found_end %lu, len %lu\n" , + (unsigned long )found->start_lblk, + start, + (unsigned long )found->len); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (found->len ==0 ) { +SSDFS_ERR ("found empty extent\n" ); +return -ERANGE ; + } + +return 0 ; +} + +/* +* struct ssdfs_blk2off_found_range - found range +* @range: range descriptor +* @start_id: starting offset ID +* @state: state of logical blocks in extent (used, free and so on) +*/ +struct ssdfs_blk2off_found_range { +struct ssdfs_blk2off_range range; +u16 start_id; +u8 state; +}; + +/* +* ssdfs_translation_extent_init() - init translation extent +* @found: range of changed logical blocks +* @sequence_id: sequence ID of extent +* @extent: pointer on initialized extent [out] +*/ +static inline +void ssdfs_translation_extent_init (struct ssdfs_blk2off_found_range *found, +u8 sequence_id, +struct ssdfs_translation_extent *extent) +{ +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!found || !extent); +BUG_ON (found->state <=SSDFS_LOGICAL_BLK_UNKNOWN_STATE || + found->state >=SSDFS_LOGICAL_BLK_STATE_MAX ); + +SSDFS_DBG ("start %u, len %u, id %u, sequence_id %u, state %#x\n" , + found->range.start_lblk, found->range.len, + found->start_id, sequence_id, found->state); +#endif /* CONFIG_SSDFS_DEBUG */ + + extent->logical_blk =cpu_to_le16 (found->range.start_lblk); + extent->offset_id =cpu_to_le16 (found->start_id); + extent->len =cpu_to_le16 (found->range.len); + extent->sequence_id = sequence_id; + extent->state = found->state; +} + +/* +* can_translation_extent_be_merged() - check opportunity to merge extents +* @extent: extent for checking +* @found: range of changed logical blocks +*/ +static inline +bool can_translation_extent_be_merged (struct ssdfs_translation_extent *extent, +struct ssdfs_blk2off_found_range *found) +{ +u16 logical_blk; +u16 offset_id; +u16 len; +u16 found_blk; +u16 found_len; +u16 found_id; +u8 found_state; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!extent || !found); +BUG_ON (found->start_id ==SSDFS_BLK2OFF_TABLE_INVALID_ID ); +BUG_ON (found->state <=SSDFS_LOGICAL_BLK_UNKNOWN_STATE || + found->state >=SSDFS_LOGICAL_BLK_STATE_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + logical_blk =le16_to_cpu (extent->logical_blk); + offset_id =le16_to_cpu (extent->offset_id); + len =le16_to_cpu (extent->len); + + found_blk = found->range.start_lblk; + found_len = found->range.len; + found_id = found->start_id; + found_state = found->state; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("EXTENT: logical_blk %u, offset_id %u, len %u, " +"sequence_id %u, state %#x; " +"FOUND: logical_blk %u, start_id %u, " +"len %u, state %#x\n" , + logical_blk, offset_id, len, + extent->sequence_id, extent->state, + found->range.start_lblk, found->start_id, + found->range.len, found->state); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (extent->state != found->state) +return false; + +if (found_id == offset_id) { +SSDFS_ERR ("start_id %u == offset_id %u\n" , + found_id, offset_id); +return false; + }else if (found_id > offset_id && + (offset_id + len) == found_id) { +if ((logical_blk + len) == found_blk) +return true; +else if ((found_blk + found_len) == logical_blk) +return true; + }else if (found_id < offset_id && + (found_id + found_len) == offset_id) { +if ((logical_blk + len) == found_blk) +return true; +else if ((found_blk + found_len) == logical_blk) +return true; + } + +return false; +} + +/* +* ssdfs_merge_translation_extent() - merge translation extents +* @extent: extent for checking +* @found: range of changed logical blocks +*/ +static inline +int ssdfs_merge_translation_extent (struct ssdfs_translation_extent *extent, +struct ssdfs_blk2off_found_range *found) +{ +u16 logical_blk; +u16 offset_id; +u16 len; +u16 found_blk; +u16 found_len; +u16 found_id; +u8 found_state; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!extent || !found); +BUG_ON (found->start_id ==SSDFS_BLK2OFF_TABLE_INVALID_ID ); +BUG_ON (found->state <=SSDFS_LOGICAL_BLK_UNKNOWN_STATE || + found->state >=SSDFS_LOGICAL_BLK_STATE_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + logical_blk =le16_to_cpu (extent->logical_blk); + offset_id =le16_to_cpu (extent->offset_id); + len =le16_to_cpu (extent->len); + + found_blk = found->range.start_lblk; + found_len = found->range.len; + found_id = found->start_id; + found_state = found->state; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("EXTENT: logical_blk %u, offset_id %u, len %u, " +"sequence_id %u, state %#x; " +"FOUND: logical_blk %u, start_id %u, " +"len %u, state %#x\n" , + logical_blk, offset_id, len, + extent->sequence_id, extent->state, + found_blk, found_id, found_len, + found_state); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (extent->state != found_state) { +SSDFS_ERR ("extent->state %#x != state %#x\n" , + extent->state, found_state); +return -EINVAL ; + } + +if (found_id == offset_id) { +SSDFS_ERR ("start_id %u == offset_id %u\n" , + found_id, offset_id); +return -ERANGE ; + } + +if (found_id > offset_id && + (offset_id + len) == found_id) { +if ((logical_blk + len) == found_blk) { + extent->len =cpu_to_le16 (len + found_len); + }else if ((found_blk + found_len) == logical_blk) { + extent->logical_blk =cpu_to_le16 (found_blk); + extent->len =cpu_to_le16 (len + found_len); + } + }else if (found_id < offset_id && + (found_id + found_len) == offset_id) { +if ((logical_blk + len) == found_blk) { + extent->offset_id =cpu_to_le16 (found_id); + extent->len =cpu_to_le16 (len + found_len); + }else if ((found_blk + found_len) == logical_blk) { + extent->logical_blk =cpu_to_le16 (found_blk); + extent->offset_id =cpu_to_le16 (found_id); + extent->len =cpu_to_le16 (len + found_len); + } + }else { +SSDFS_ERR ("fail to merge the translation extent\n" ); +return -ERANGE ; + } + +return 0 ; +} + +/* +* ssdfs_insert_translation_extent() - insert translation extent into the queue +* @found: range of changed logical blocks +* @array: extents array [in|out] +* @capacity: capacity of extents array +* @extent_count: pointer on extents count value [out] +*/ +static inline +int ssdfs_insert_translation_extent (struct ssdfs_blk2off_found_range *found, +struct ssdfs_translation_extent *array, +u16 capacity,u16 *extent_count) +{ +struct ssdfs_translation_extent *extent; +size_t extent_size =sizeof (struct ssdfs_translation_extent ); +size_t array_bytes = extent_size * capacity; +u16 logical_blk; +u16 offset_id; +u16 len; +int i, j; +int err; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!found || !extent_count); +BUG_ON (found->state <=SSDFS_LOGICAL_BLK_UNKNOWN_STATE || + found->state >=SSDFS_LOGICAL_BLK_STATE_MAX ); + +SSDFS_DBG ("start_id %u, state %#x, extent_count %u\n" , + found->start_id, found->state, *extent_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +BUG_ON (*extent_count >= capacity); + +if (found->start_id ==SSDFS_BLK2OFF_TABLE_INVALID_ID ) { + extent = &array[*extent_count]; +ssdfs_translation_extent_init (found, *extent_count, extent); + (*extent_count)++; + +return 0 ; + } + +for (i =0 ; i < *extent_count; i++) { + extent = &array[i]; + + logical_blk =le16_to_cpu (extent->logical_blk); + offset_id =le16_to_cpu (extent->offset_id); + len =le16_to_cpu (extent->len); + +if (offset_id >=SSDFS_BLK2OFF_TABLE_INVALID_ID ) +continue ; + +if (found->start_id == offset_id) { +SSDFS_ERR ("start_id %u == offset_id %u\n" , + found->start_id, offset_id); +return -ERANGE ; + }else if (found->start_id > offset_id && +can_translation_extent_be_merged (extent, found)) { + err =ssdfs_merge_translation_extent (extent, found); +if (unlikely (err)) { +SSDFS_ERR ("fail to merge extent: " +"err %d\n" , err); +return err; + }else +return 0 ; + }else if (found->start_id < offset_id) { +if (can_translation_extent_be_merged (extent, found)) { + err =ssdfs_merge_translation_extent (extent, + found); +if (unlikely (err)) { +SSDFS_ERR ("fail to merge extent: " +"err %d\n" , err); +return err; + }else +return 0 ; + }else { + i++; +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("unable to merge: index %d\n" , i); +#endif /* CONFIG_SSDFS_DEBUG */ +break ; + } + } + } + +if (i < *extent_count) { +#ifdef CONFIG_SSDFS_DEBUG +if (((i +1 ) + (*extent_count - i)) > capacity) { +SSDFS_WARN ("value is out capacity\n" ); +return -ERANGE ; + } + +SSDFS_DBG ("extent_count %u, index %d, extent_size %zu\n" , + *extent_count, i, extent_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_memmove (array, (i +1 ) * extent_size, array_bytes, + array, i * extent_size, array_bytes, + (*extent_count - i) * extent_size); +if (unlikely (err)) { +SSDFS_ERR ("fail to move: err %d\n" , err); +return err; + } + +for (j = i +1 ; j <= *extent_count; j++) { + extent = &array[j]; + extent->sequence_id = j; + } + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("extent_count %u, index %d, extent_size %zu\n" , + *extent_count, i, extent_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + extent = &array[i]; +ssdfs_translation_extent_init (found, i, extent); + + (*extent_count)++; + +#ifdef CONFIG_SSDFS_DEBUG +for (i =0 ; i < *extent_count; i++) { + extent = &array[i]; + +SSDFS_DBG ("index %d, logical_blk %u, offset_id %u, " +"len %u, sequence_id %u, state %u\n" , + i, +le16_to_cpu (extent->logical_blk), +le16_to_cpu (extent->offset_id), +le16_to_cpu (extent->len), + extent->sequence_id, + extent->state); + } +#endif /* CONFIG_SSDFS_DEBUG */ + +return 0 ; +} + +static inline +bool is_found_logical_block_free (struct ssdfs_blk2off_table_snapshot *sp, +u16 blk) +{ +struct ssdfs_offset_position *pos; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sp); + +SSDFS_DBG ("blk %u\n" , blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + pos = &sp->tbl_copy[blk]; + +return pos->id ==SSDFS_BLK2OFF_TABLE_INVALID_ID && + pos->offset_index >=U16_MAX ; +} + +static inline +bool is_found_extent_ended (struct ssdfs_blk2off_table_snapshot *sp, +u16 blk, +struct ssdfs_blk2off_found_range *found) +{ +struct ssdfs_offset_position *pos; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sp || !found); + +SSDFS_DBG ("blk %u\n" , blk); +#endif /* CONFIG_SSDFS_DEBUG */ + + pos = &sp->tbl_copy[blk]; + +if (pos->peb_index != sp->peb_index) { +/* changes of another PEB */ +return true; + }else if (pos->id !=SSDFS_BLK2OFF_TABLE_INVALID_ID ) { +if (found->start_id ==SSDFS_BLK2OFF_TABLE_INVALID_ID ) + found->start_id = pos->id; +else if ((found->start_id + found->range.len) != pos->id) +return true; + }else if (pos->id ==SSDFS_BLK2OFF_TABLE_INVALID_ID && + found->state !=SSDFS_LOGICAL_BLK_FREE ) { +if (found->range.start_lblk !=U16_MAX ) { +/* state is changed */ +return true; + } + } + +return false; +} + +/* +* ssdfs_blk2off_table_extract_extents() - extract changed extents +* @sp: table's snapshot +* @array: extents array [in|out] +* @capacity: capacity of extents array +* @extent_count: pointer on extents count value [out] +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal logic error. +*/ +int ssdfs_blk2off_table_extract_extents (struct ssdfs_blk2off_table_snapshot *sp, +struct ssdfs_translation_extent *array, +u16 capacity,u16 *extent_count) +{ +unsigned long start =0 ; +int i; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!sp || !array || !extent_count); +BUG_ON (capacity ==0 ); + +SSDFS_DBG ("snapshot %p, peb_index %u, extents %p, " +"capacity %u, extent_count %p\n" , + sp, sp->peb_index, array, + capacity, extent_count); +#endif /* CONFIG_SSDFS_DEBUG */ + + *extent_count =0 ; + +do { +struct ssdfs_blk2off_range changed_area = {0 }; +struct ssdfs_blk2off_found_range found = { + .range.start_lblk =U16_MAX , + .range.len =0 , + .start_id =SSDFS_BLK2OFF_TABLE_INVALID_ID , + .state =SSDFS_LOGICAL_BLK_UNKNOWN_STATE , + }; +struct ssdfs_offset_position *pos; + + err =ssdfs_find_changed_area (sp, start, &changed_area); +if (err == -ENODATA ) { + err =0 ; +SSDFS_DBG ("nothing found\n" ); + goto finish_extract_extents; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to find changed area: err %d\n" , + err); +return err; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("changed area: start %u, len %u\n" , + changed_area.start_lblk, changed_area.len); +#endif /* CONFIG_SSDFS_DEBUG */ + +for (i =0 ; i < changed_area.len; i++) { +u16 blk = changed_area.start_lblk + i; +bool is_extent_ended = false; + + pos = &sp->tbl_copy[blk]; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cno %llx, id %u, peb_index %u, " +"sequence_id %u, offset_index %u\n" , + pos->cno, pos->id, pos->peb_index, + pos->sequence_id, pos->offset_index); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (pos->peb_index ==U16_MAX ) { +SSDFS_WARN ("invalid peb_index: " +"logical_blk %u\n" , + blk); +return -ERANGE ; + } + +if (is_found_logical_block_free (sp, blk)) { +/* free block */ + +switch (found.state) { +case SSDFS_LOGICAL_BLK_UNKNOWN_STATE : + found.range.start_lblk = blk; + found.range.len =1 ; + found.state =SSDFS_LOGICAL_BLK_FREE ; +break ; + +case SSDFS_LOGICAL_BLK_FREE : + found.range.len++; +break ; + +case SSDFS_LOGICAL_BLK_USED : + is_extent_ended = true; +break ; + +default : +SSDFS_ERR ("unexpected blk state %#x\n" , + found.state); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("free block: start_lblk %u, " +"len %u, state %#x, " +"is_extent_ended %#x\n" , + found.range.start_lblk, + found.range.len, + found.state, + is_extent_ended); +#endif /* CONFIG_SSDFS_DEBUG */ + }else { +/* used block */ + +switch (found.state) { +case SSDFS_LOGICAL_BLK_UNKNOWN_STATE : + found.range.start_lblk = blk; + found.range.len =1 ; + found.start_id = pos->id; + found.state =SSDFS_LOGICAL_BLK_USED ; +break ; + +case SSDFS_LOGICAL_BLK_USED : + is_extent_ended = +is_found_extent_ended (sp, blk, + &found); +if (!is_extent_ended) + found.range.len++; +break ; + +case SSDFS_LOGICAL_BLK_FREE : + is_extent_ended = true; +break ; + +default : +SSDFS_ERR ("unexpected blk state %#x\n" , + found.state); +return -ERANGE ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("used block: start_lblk %u, " +"len %u, state %#x, " +"is_extent_ended %#x\n" , + found.range.start_lblk, + found.range.len, + found.state, + is_extent_ended); +#endif /* CONFIG_SSDFS_DEBUG */ + } + +if (is_extent_ended) { +if (found.range.start_lblk ==U16_MAX ) { +SSDFS_ERR ("invalid start_lblk %u\n" , + found.range.start_lblk); +return -ERANGE ; + } + + err =ssdfs_insert_translation_extent (&found, + array, + capacity, + extent_count); +if (unlikely (err)) { +SSDFS_ERR ("fail to insert extent: " +"start_id %u, state %#x, " +"err %d\n" , + found.start_id, found.state, + err); +return err; + } + + pos = &sp->tbl_copy[blk]; + +if (pos->id ==SSDFS_BLK2OFF_TABLE_INVALID_ID ) + found.state =SSDFS_LOGICAL_BLK_FREE ; +else + found.state =SSDFS_LOGICAL_BLK_USED ; + + found.range.start_lblk = blk; + found.range.len =1 ; + found.start_id = pos->id; + } + } + +if (found.range.start_lblk !=U16_MAX ) { + err =ssdfs_insert_translation_extent (&found, + array, + capacity, + extent_count); +if (unlikely (err)) { +SSDFS_ERR ("fail to insert extent: " +"start_id %u, state %#x, " +"err %d\n" , + found.start_id, found.state, err); +return err; + } + + start = found.range.start_lblk + found.range.len; + + found.range.start_lblk =U16_MAX ; + found.range.len =0 ; + found.state =SSDFS_LOGICAL_BLK_UNKNOWN_STATE ; + }else + start = changed_area.start_lblk + changed_area.len; + }while (start < sp->capacity); + +finish_extract_extents: +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("extents_count %u\n" , *extent_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (*extent_count ==0 ) { + err = -ERANGE ; +SSDFS_ERR ("invalid state of change bitmap\n" ); +return err; + } + +return 0 ; +} + +/* +* ssdfs_blk2off_table_prepare_for_commit() - prepare fragment for commit +* @table: pointer on table object +* @peb_index: PEB's index +* @sequence_id: fragment's sequence ID +* @offset_table_off: pointer on current offset to offset table header [in|out] +* @sp: pointer on snapshot +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ERANGE - internal logic error. +*/ +int +ssdfs_blk2off_table_prepare_for_commit (struct ssdfs_blk2off_table *table, +u16 peb_index,u16 sequence_id, +u32 *offset_table_off, +struct ssdfs_blk2off_table_snapshot *sp) +{ +struct ssdfs_fs_info *fsi; +struct ssdfs_phys_offset_table_array *pot_table; +struct ssdfs_sequence_array *sequence; +struct ssdfs_phys_offset_table_fragment *fragment; +void *ptr; +u16 id_count; +u32 byte_size; +u16 flags =0 ; +int last_sequence_id; +bool has_next_fragment = false; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table || !sp || !table->fsi || !offset_table_off); +BUG_ON (peb_index >= table->pebs_count); +BUG_ON (peb_index != sp->peb_index); + +SSDFS_DBG ("table %p, peb_index %u, sequence_id %u, " +"offset_table_off %p, sp %p\n" , + table, peb_index, sequence_id, + offset_table_off, sp); +#endif /* CONFIG_SSDFS_DEBUG */ + + fsi = table->fsi; + +down_read (&table->translation_lock); + + pot_table = &table->peb[peb_index]; + + sequence = pot_table->sequence; + ptr =ssdfs_sequence_array_get_item (sequence, sequence_id); +if (IS_ERR_OR_NULL (ptr)) { + err = (ptr ==NULL ? -ENOENT :PTR_ERR (ptr)); +SSDFS_ERR ("fail to get fragment: " +"sequence_id %u, err %d\n" , + sequence_id, err); + goto finish_prepare_for_commit; + } + fragment = (struct ssdfs_phys_offset_table_fragment *)ptr; + +if (atomic_read (&fragment->state) !=SSDFS_BLK2OFF_FRAG_UNDER_COMMIT ) { + err = -ERANGE ; +SSDFS_ERR ("fragment isn't under commit: " +"state %#x\n" , +atomic_read (&fragment->state)); + goto finish_prepare_for_commit; + } + +down_write (&fragment->lock); + + fragment->hdr->magic =cpu_to_le32 (SSDFS_PHYS_OFF_TABLE_MAGIC ); + fragment->hdr->checksum =0 ; + + fragment->hdr->start_id =cpu_to_le16 (fragment->start_id); + id_count = (u16 )atomic_read (&fragment->id_count); + fragment->hdr->id_count =cpu_to_le16 (id_count); + byte_size =sizeof (struct ssdfs_phys_offset_table_header ); + byte_size += (u32 )id_count *sizeof (struct ssdfs_phys_offset_descriptor ); + fragment->hdr->byte_size =cpu_to_le32 (byte_size); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("hdr_size %zu, id_count %u, " +"desc_size %zu, byte_size %u\n" , +sizeof (struct ssdfs_phys_offset_table_header ), + id_count, +sizeof (struct ssdfs_phys_offset_descriptor ), + byte_size); +SSDFS_DBG ("fragment: start_id %u, id_count %u\n" , +le16_to_cpu (fragment->hdr->start_id), +le16_to_cpu (fragment->hdr->id_count)); +#endif /* CONFIG_SSDFS_DEBUG */ + + fragment->hdr->peb_index =cpu_to_le16 (peb_index); + fragment->hdr->sequence_id =cpu_to_le16 (fragment->sequence_id); + fragment->hdr->type =cpu_to_le16 (table->type); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sequence_id %u, start_sequence_id %u, " +"dirty_fragments %u, fragment->sequence_id %u\n" , + sequence_id, sp->start_sequence_id, + sp->dirty_fragments, + fragment->sequence_id); +#endif /* CONFIG_SSDFS_DEBUG */ + + last_sequence_id =ssdfs_sequence_array_last_id (pot_table->sequence); + has_next_fragment = sequence_id != last_sequence_id; + + flags |=SSDFS_OFF_TABLE_HAS_CSUM ; +if (has_next_fragment) + flags |=SSDFS_OFF_TABLE_HAS_NEXT_FRAGMENT ; + +switch (fsi->metadata_options.blk2off_tbl.compression) { +case SSDFS_BLK2OFF_TBL_ZLIB_COMPR_TYPE : +case SSDFS_BLK2OFF_TBL_LZO_COMPR_TYPE : + flags |=SSDFS_BLK_DESC_TBL_COMPRESSED ; +break ; +default : +/* do nothing */ +break ; + } + + fragment->hdr->flags =cpu_to_le16 (flags); + + fragment->hdr->used_logical_blks =cpu_to_le16 (sp->used_logical_blks); + fragment->hdr->free_logical_blks =cpu_to_le16 (sp->free_logical_blks); + fragment->hdr->last_allocated_blk =cpu_to_le16 (sp->last_allocated_blk); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (byte_size >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + *offset_table_off += byte_size; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("offset_table_off %u\n" , *offset_table_off); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (has_next_fragment) { + fragment->hdr->next_fragment_off = +cpu_to_le16 ((u16 )byte_size); + }else { + fragment->hdr->next_fragment_off = +cpu_to_le16 (U16_MAX ); + } + + fragment->hdr->checksum =ssdfs_crc32_le (fragment->hdr, byte_size); + +up_write (&fragment->lock); + +finish_prepare_for_commit: +up_read (&table->translation_lock); + +return err; +} + +/* +* ssdfs_blk2off_table_forget_snapshot() - undirty PEB's table +* @table: pointer on table object +* @sp: pointer on snapshot +* @array: extents array +* @extent_count: count of extents in array +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal logic error. +*/ +int +ssdfs_blk2off_table_forget_snapshot (struct ssdfs_blk2off_table *table, +struct ssdfs_blk2off_table_snapshot *sp, +struct ssdfs_translation_extent *array, +u16 extent_count) +{ +struct ssdfs_phys_offset_table_array *pot_table; +struct ssdfs_sequence_array *sequence; +struct ssdfs_offset_position *pos; +u16 last_sequence_id; +unsigned long commited_fragments =0 ; +int i, j; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!table || !sp || !array); +BUG_ON (sp->peb_index >= table->pebs_count); +BUG_ON (extent_count ==0 ); + +SSDFS_DBG ("table %p, peb_index %u, sp %p, " +"extents %p, extents_count %u\n" , + table, sp->peb_index, sp, + array, extent_count); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_write (&table->translation_lock); + + pot_table = &table->peb[sp->peb_index]; + last_sequence_id =ssdfs_sequence_array_last_id (pot_table->sequence); + +if (sp->dirty_fragments ==0 ) { + err = -EINVAL ; +SSDFS_ERR ("dirty_fragments == 0\n" ); + goto finish_forget_snapshot; + } + + sequence = table->peb[sp->peb_index].sequence; + err =ssdfs_sequence_array_change_all_states (sequence, +SSDFS_SEQUENCE_ITEM_UNDER_COMMIT_TAG , +SSDFS_SEQUENCE_ITEM_COMMITED_TAG , + ssdfs_change_fragment_state, +SSDFS_BLK2OFF_FRAG_UNDER_COMMIT , +SSDFS_BLK2OFF_FRAG_COMMITED , + &commited_fragments); +if (unlikely (err)) { +SSDFS_ERR ("fail to set fragments as commited: " +"err %d\n" , err); + goto finish_forget_snapshot; + } + +if (sp->dirty_fragments != commited_fragments) { + err = -ERANGE ; +SSDFS_ERR ("dirty_fragments %u != commited_fragments %lu\n" , + sp->dirty_fragments, commited_fragments); + goto finish_forget_snapshot; + } + +for (i =0 ; i < extent_count; i++) { +u16 start_blk =le16_to_cpu (array[i].logical_blk); +u16 len =le16_to_cpu (array[i].len); + +for (j =0 ; j < len; j++) { +u16 blk = start_blk + j; +u64 cno1, cno2; +void *kaddr; + + kaddr =ssdfs_dynamic_array_get_locked (&table->lblk2off, + blk); +if (IS_ERR_OR_NULL (kaddr)) { + err = (kaddr ==NULL ? -ENOENT :PTR_ERR (kaddr)); +SSDFS_ERR ("fail to get logical block: " +"blk %u, err %d\n" , + blk, err); + goto finish_forget_snapshot; + } + + pos =SSDFS_OFF_POS (kaddr); + cno1 = pos->cno; + cno2 = sp->tbl_copy[blk].cno; + + err =ssdfs_dynamic_array_release (&table->lblk2off, + blk, pos); +if (unlikely (err)) { +SSDFS_ERR ("fail to release: " +"blk %u, err %d\n" , + blk, err); + goto finish_forget_snapshot; + } + +if (cno1 < cno2) { +SSDFS_WARN ("cno1 %llu < cno2 %llu\n" , + cno1, cno2); + }else if (cno1 > cno2) +continue ; + +/* +* Don't clear information about free blocks +* in the modification bitmap. Otherwise, +* this information will be lost during +* the PEBs migration. +*/ +if (array[i].state !=SSDFS_LOGICAL_BLK_FREE ) { + err = +ssdfs_blk2off_table_bmap_clear (&table->lbmap, +SSDFS_LBMAP_MODIFICATION_INDEX , blk); +if (unlikely (err)) { +SSDFS_ERR ("fail to clear bitmap: " +"blk %u, err %d\n" , + blk, err); + goto finish_forget_snapshot; + } + } + + err =ssdfs_blk2off_table_bmap_set (&table->lbmap, +SSDFS_LBMAP_INIT_INDEX , blk); +if (unlikely (err)) { +SSDFS_ERR ("fail to set bitmap: " +"blk %u, err %d\n" , + blk, err); + goto finish_forget_snapshot; + } + } + } + +finish_forget_snapshot: +up_write (&table->translation_lock); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("finished\n" ); +#endif /* CONFIG_SSDFS_DEBUG */ + +return err; +} + +/* +* ssdfs_peb_store_offsets_table_header() - store offsets table header +* @pebi: pointer on PEB object +* @hdr: table header +* @cur_page: pointer on current page value [in|out] +* @write_offset: pointer on write offset value [in|out] +* +* This function tries to store table header into log. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ENOMEM - fail to find memory page. +*/ +int ssdfs_peb_store_offsets_table_header (struct ssdfs_peb_info *pebi, +struct ssdfs_blk2off_table_header *hdr, +pgoff_t *cur_page, +u32 *write_offset) +{ +size_t hdr_sz =sizeof (struct ssdfs_blk2off_table_header ); +struct page *page; +u32 page_off, cur_offset; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebi); +BUG_ON (!hdr || !cur_page || !write_offset); + +SSDFS_DBG ("peb %llu, current_log.start_page %u, " +"hdr %p, cur_page %lu, write_offset %u\n" , + pebi->peb_id, + pebi->current_log.start_page, + hdr, *cur_page, *write_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + page_off = *write_offset %PAGE_SIZE ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON ((PAGE_SIZE - page_off) < hdr_sz); +#endif /* CONFIG_SSDFS_DEBUG */ + + 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 ; + } + + err =ssdfs_memcpy_to_page (page, page_off,PAGE_SIZE , + hdr,0 , hdr_sz, + hdr_sz); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); + goto finish_copy; + } + +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 %lu as dirty: err %d\n" , + *cur_page, 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 */ + +if (unlikely (err)) +return err; + + *write_offset += hdr_sz; + + cur_offset = (*cur_page <<PAGE_SHIFT ) + page_off + hdr_sz; + *cur_page = cur_offset >>PAGE_SHIFT ; + +return 0 ; +} + +/* +* ssdfs_peb_store_offsets_table_extents() - store translation extents +* @pebi: pointer on PEB object +* @array: translation extents array +* @extent_count: count of extents in the array +* @cur_page: pointer on current page value [in|out] +* @write_offset: pointer on write offset value [in|out] +* +* This function tries to store translation extents into log. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-ENOMEM - fail to find memory page. +*/ +int +ssdfs_peb_store_offsets_table_extents (struct ssdfs_peb_info *pebi, +struct ssdfs_translation_extent *array, +u16 extent_count, +pgoff_t *cur_page, +u32 *write_offset) +{ +struct page *page; +size_t extent_size =sizeof (struct ssdfs_translation_extent ); +size_t array_size = extent_size * extent_count; +u32 rest_bytes, written_bytes =0 ; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebi); +BUG_ON (!array || !cur_page || !write_offset); +BUG_ON (extent_count ==0 || extent_count ==U16_MAX ); + +SSDFS_DBG ("peb %llu, current_log.start_page %u, " +"array %p, extent_count %u, " +"cur_page %lu, write_offset %u\n" , + pebi->peb_id, + pebi->current_log.start_page, + array, extent_count, + *cur_page, *write_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + + rest_bytes = extent_count * extent_size; + +while (rest_bytes >0 ) { +u32 bytes; +u32 cur_off = *write_offset %PAGE_SIZE ; +u32 new_off; + + bytes =min_t (u32, rest_bytes,PAGE_SIZE - cur_off); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (bytes < extent_size); +BUG_ON (written_bytes > (extent_count * extent_size)); +#endif /* CONFIG_SSDFS_DEBUG */ + + 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 ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_off %u, written_bytes %u, bytes %u\n" , + cur_off, written_bytes, bytes); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_memcpy_to_page (page, cur_off,PAGE_SIZE , + array, written_bytes, array_size, + bytes); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); + goto finish_copy; + } + +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 %lu as dirty: err %d\n" , + *cur_page, 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 */ + +if (unlikely (err)) +return err; + + *write_offset += bytes; + + new_off = (*cur_page <<PAGE_SHIFT ) + cur_off + bytes; + *cur_page = new_off >>PAGE_SHIFT ; + + rest_bytes -= bytes; + written_bytes += bytes; + }; + +return 0 ; +} + +/* +* ssdfs_peb_store_offsets_table_fragment() - store fragment of offsets table +* @pebi: pointer on PEB object +* @table: pointer on translation table object +* @peb_index: PEB's index +* @sequence_id: sequence ID of fragment +* @cur_page: pointer on current page value [in|out] +* @write_offset: pointer on write offset value [in|out] +* +* This function tries to store table's fragment into log. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENOMEM - fail to find memory page. +*/ +int ssdfs_peb_store_offsets_table_fragment (struct ssdfs_peb_info *pebi, +struct ssdfs_blk2off_table *table, +u16 peb_index,u16 sequence_id, +pgoff_t *cur_page, +u32 *write_offset) +{ +struct ssdfs_phys_offset_table_array *pot_table; +struct ssdfs_sequence_array *sequence; +struct ssdfs_phys_offset_table_fragment *fragment; +struct ssdfs_phys_offset_table_header *hdr; +size_t hdr_size =sizeof (struct ssdfs_phys_offset_table_header ); +struct page *page; +void *kaddr; +u32 fragment_size; +u16 flags; +u32 next_fragment_off; +u32 rest_bytes, written_bytes =0 ; +u32 cur_off; +u32 new_off; +u32 diff; +int err =0 ; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (!pebi); +BUG_ON (!table || !cur_page || !write_offset); +BUG_ON (peb_index >= table->pebs_count); + +SSDFS_DBG ("peb %llu, current_log.start_page %u, " +"peb_index %u, sequence_id %u, " +"cur_page %lu, write_offset %u\n" , + pebi->peb_id, + pebi->current_log.start_page, + peb_index, sequence_id, + *cur_page, *write_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + +down_read (&table->translation_lock); + + pot_table = &table->peb[peb_index]; + + sequence = pot_table->sequence; + kaddr =ssdfs_sequence_array_get_item (sequence, sequence_id); +if (IS_ERR_OR_NULL (kaddr)) { + err = (kaddr ==NULL ? -ENOENT :PTR_ERR (kaddr)); +SSDFS_ERR ("fail to get fragment: " +"sequence_id %u, err %d\n" , + sequence_id, err); + goto finish_store_fragment; + } + fragment = (struct ssdfs_phys_offset_table_fragment *)kaddr; + +down_write (&fragment->lock); + +if (atomic_read (&fragment->state) !=SSDFS_BLK2OFF_FRAG_UNDER_COMMIT ) { + err = -ERANGE ; +SSDFS_ERR ("invalid fragment state %#x\n" , +atomic_read (&fragment->state)); + goto finish_fragment_copy; + } + + hdr = fragment->hdr; + +if (!hdr) { + err = -ERANGE ; +SSDFS_ERR ("header pointer is NULL\n" ); + goto finish_fragment_copy; + } + + fragment_size =le32_to_cpu (hdr->byte_size); + rest_bytes = fragment_size; + +if (fragment_size < hdr_size || fragment_size > fragment->buf_size) { + err = -ERANGE ; +SSDFS_ERR ("invalid fragment size %u\n" , + fragment_size); + goto finish_fragment_copy; + } + + next_fragment_off =ssdfs_peb_correct_area_write_offset (*write_offset + + fragment_size, + hdr_size); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_page %lu, write_offset %u, fragment_size %u, " +"hdr_size %zu, next_fragment_off %u\n" , + *cur_page, *write_offset, fragment_size, + hdr_size, next_fragment_off); +#endif /* CONFIG_SSDFS_DEBUG */ + + flags =le16_to_cpu (hdr->flags); +if (flags &SSDFS_OFF_TABLE_HAS_NEXT_FRAGMENT ) { + diff = next_fragment_off - *write_offset; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (diff >=U16_MAX ); +#endif /* CONFIG_SSDFS_DEBUG */ + + hdr->next_fragment_off =cpu_to_le16 ((u16 )diff); + hdr->checksum =ssdfs_crc32_le (hdr, +le32_to_cpu (hdr->byte_size)); + } + +while (rest_bytes >0 ) { +u32 bytes; + cur_off = *write_offset %PAGE_SIZE ; + + bytes =min_t (u32, rest_bytes,PAGE_SIZE - cur_off); + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (written_bytes > fragment_size); +#endif /* CONFIG_SSDFS_DEBUG */ + + page =ssdfs_page_array_grab_page (&pebi->cache, + *cur_page); +if (IS_ERR_OR_NULL (page)) { + err = -ENOMEM ; +SSDFS_ERR ("fail to get cache page: index %lu\n" , + *cur_page); + goto finish_fragment_copy; + } + + err =ssdfs_memcpy_to_page (page, cur_off,PAGE_SIZE , + hdr, written_bytes, fragment_size, + bytes); +if (unlikely (err)) { +SSDFS_ERR ("fail to copy: err %d\n" , err); + goto finish_cur_copy; + } + +ssdfs_set_page_private (page,0 ); +SetPageUptodate (page); + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("ssdfs_page_array_set_page_dirty %lu\n" , + *cur_page); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_page_array_set_page_dirty (&pebi->cache, + *cur_page); +if (unlikely (err)) { +SSDFS_ERR ("fail to set page %lu as dirty: err %d\n" , + *cur_page, err); + } + +finish_cur_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 */ + +if (unlikely (err)) + goto finish_fragment_copy; + + *write_offset += bytes; + + new_off = (*cur_page <<PAGE_SHIFT ) + cur_off + bytes; + *cur_page = new_off >>PAGE_SHIFT ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_off %u, bytes %u, new_off %u, cur_page %lu\n" , + cur_off, bytes, new_off, *cur_page); +#endif /* CONFIG_SSDFS_DEBUG */ + + rest_bytes -= bytes; + written_bytes += bytes; + }; + +#ifdef CONFIG_SSDFS_DEBUG +BUG_ON (*write_offset > next_fragment_off); +#endif /* CONFIG_SSDFS_DEBUG */ + + diff = next_fragment_off - *write_offset; + +if (diff >0 ) { + cur_off = *write_offset %PAGE_SIZE ; + *write_offset = next_fragment_off; + + new_off = (*cur_page <<PAGE_SHIFT ) + cur_off + diff; + *cur_page = new_off >>PAGE_SHIFT ; + } + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("cur_page %lu, write_offset %u, " +"next_fragment_off %u\n" , + *cur_page, *write_offset, + next_fragment_off); +#endif /* CONFIG_SSDFS_DEBUG */ + +finish_fragment_copy: +up_write (&fragment->lock); + +finish_store_fragment: +up_read (&table->translation_lock); + +return err; +} + +static inline +u16 ssdfs_next_sequence_id (u16 sequence_id) +{ +u16 next_sequence_id =U16_MAX ; + +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("sequence_id %u\n" , sequence_id); +#endif /* CONFIG_SSDFS_DEBUG */ + +if (sequence_id >SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD ) { +SSDFS_ERR ("invalid sequence_id %u\n" , + sequence_id); +return U16_MAX ; + }else if (sequence_id <SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD ) { +/* increment value */ + next_sequence_id = sequence_id +1 ; + }else + next_sequence_id =0 ; + +return next_sequence_id; +} + +/* +* ssdfs_peb_store_offsets_table() - store offsets table +* @pebi: pointer on PEB object +* @desc: offsets table 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 the offsets table into log. +* +* RETURN: +* [success] +* [failure] - error code: +* +* %-EINVAL - invalid input. +* %-ERANGE - internal error. +* %-ENOMEM - fail to find memory page. +*/ +int ssdfs_peb_store_offsets_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_blk2off_table *table; +struct ssdfs_blk2off_table_snapshot snapshot = {0 }; +struct ssdfs_blk2off_table_header hdr; +struct ssdfs_translation_extent *extents =NULL ; +size_t tbl_hdr_size =sizeof (struct ssdfs_blk2off_table_header ); +u16 extents_off = offsetof(struct ssdfs_blk2off_table_header , sequence); +u16 extent_count =0 ; +u32 offset_table_off; +u16 peb_index; +u32 table_start_offset; +u16 sequence_id; +u32 fragments_count =0 ; +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 (!pebi->pebc->parent_si->blk2off_table); +BUG_ON (!desc || !cur_page || !write_offset); +#endif /* CONFIG_SSDFS_DEBUG */ + +#ifdef CONFIG_SSDFS_TRACK_API_CALL +SSDFS_ERR ("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); +#else +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_TRACK_API_CALL */ + + fsi = pebi->pebc->parent_si->fsi; + peb_index = pebi->peb_index; + table = pebi->pebc->parent_si->blk2off_table; + +memset (desc,0 ,sizeof (struct ssdfs_metadata_descriptor )); +memset (&hdr,0 , tbl_hdr_size); + + err =ssdfs_blk2off_table_snapshot (table, peb_index, &snapshot); +if (err == -ENODATA ) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("table hasn't dirty fragments: peb_index %u\n" , + peb_index); +#endif /* CONFIG_SSDFS_DEBUG */ +return 0 ; + }else if (unlikely (err)) { +SSDFS_ERR ("fail to get snapshot: peb_index %u, err %d\n" , + peb_index, err); +return err; + } + +if (unlikely (peb_index != snapshot.peb_index)) { + err = -ERANGE ; +SSDFS_ERR ("peb_index %u != snapshot.peb_index %u\n" , + peb_index, snapshot.peb_index); + goto fail_store_off_table; + } + +if (unlikely (!snapshot.bmap_copy || !snapshot.tbl_copy)) { + err = -ERANGE ; +SSDFS_ERR ("invalid snapshot: " +"peb_index %u, bmap_copy %p, tbl_copy %p\n" , + peb_index, + snapshot.bmap_copy, + snapshot.tbl_copy); + goto fail_store_off_table; + } + + extents =ssdfs_blk2off_kcalloc (snapshot.capacity, +sizeof (struct ssdfs_translation_extent ), +GFP_KERNEL ); +if (unlikely (!extents)) { + err = -ENOMEM ; +SSDFS_ERR ("fail to allocate extent array\n" ); + goto fail_store_off_table; + } + + hdr.magic.common =cpu_to_le32 (SSDFS_SUPER_MAGIC ); + hdr.magic.key =cpu_to_le16 (SSDFS_BLK2OFF_TABLE_HDR_MAGIC ); + hdr.magic.version.major =SSDFS_MAJOR_REVISION ; + hdr.magic.version.minor =SSDFS_MINOR_REVISION ; + + err =ssdfs_blk2off_table_extract_extents (&snapshot, extents, + snapshot.capacity, + &extent_count); +if (unlikely (err)) { +SSDFS_ERR ("fail to extract the extent array: " +"peb_index %u, err %d\n" , + peb_index, err); + goto fail_store_off_table; + }else if (extent_count ==0 ) { + err = -ERANGE ; +SSDFS_ERR ("invalid extent count\n" ); + goto fail_store_off_table; + } + + hdr.extents_off =cpu_to_le16 (extents_off); + hdr.extents_count =cpu_to_le16 (extent_count); + +#ifdef CONFIG_SSDFS_SAVE_WHOLE_BLK2OFF_TBL_IN_EVERY_LOG + fragments_count = snapshot.fragments_count; +#else + fragments_count = snapshot.dirty_fragments; +#endif /* CONFIG_SSDFS_SAVE_WHOLE_BLK2OFF_TBL_IN_EVERY_LOG */ + + offset_table_off = tbl_hdr_size + + ((extent_count -1 ) * +sizeof (struct ssdfs_translation_extent )); + + hdr.offset_table_off =cpu_to_le16 ((u16 )offset_table_off); + + sequence_id = snapshot.start_sequence_id; +for (i =0 ; i < fragments_count; i++) { +#ifdef CONFIG_SSDFS_DEBUG +SSDFS_DBG ("fragment_index %d, offset_table_off %u\n" , + i, offset_table_off); +#endif /* CONFIG_SSDFS_DEBUG */ + + err =ssdfs_blk2off_table_prepare_for_commit (table, peb_index, + sequence_id, + &offset_table_off, + &snapshot); +if (unlikely (err)) { +SSDFS_ERR ("fail to prepare fragment for commit: " +"peb_index %u, sequence_id %u, err %d\n" , + peb_index, sequence_id, err); + goto fail_store_off_table; + } + + sequence_id =ssdfs_next_sequence_id (sequence_id); +if (sequence_id >SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD ) { + err = -ERANGE ; +SSDFS_ERR ("invalid next sequence_id %u\n" , + sequence_id); + goto fail_store_off_table; + } + } + + hdr.fragments_count =cpu_to_le16 (snapshot.dirty_fragments); + +ssdfs_memcpy (hdr.sequence,0 ,sizeof (struct ssdfs_translation_extent ), + extents,0 ,sizeof (struct ssdfs_translation_extent ), +sizeof (struct ssdfs_translation_extent )); + + hdr.check.bytes =cpu_to_le16 (tbl_hdr_size); + hdr.check.flags =cpu_to_le16 (SSDFS_CRC32 ); + + err =ssdfs_calculate_csum (&hdr.check, &hdr, tbl_hdr_size); +if (unlikely (err)) { +SSDFS_ERR ("unable to calculate checksum: err %d\n" , err); + goto fail_store_off_table; + } + + *write_offset =ssdfs_peb_correct_area_write_offset (*write_offset, + tbl_hdr_size); + table_start_offset = *write_offset; + + desc->offset =cpu_to_le32 (*write_offset + + (pebi->current_log.start_page * fsi->pagesize)); + + err =ssdfs_peb_store_offsets_table_header (pebi, &hdr, + cur_page, write_offset); +if (unlikely (err)) { +SSDFS_ERR ("fail to store offsets table's header: " +"cur_page %lu, write_offset %u, err %d\n" , + *cur_page, *write_offset, err); + goto fail_store_off_table; + } + +if (extent_count >1 ) { + err =ssdfs_peb_store_offsets_table_extents (pebi, &extents[1 ], + extent_count -1 , + cur_page, + write_offset); +if (unlikely (err)) { +SSDFS_ERR ("fail to store offsets table's extents: " +"cur_page %lu, write_offset %u, err %d\n" , + *cur_page, *write_offset, err); + goto fail_store_off_table; + } + } + + sequence_id = snapshot.start_sequence_id; +for (i =0 ; i < fragments_count; i++) { + err =ssdfs_peb_store_offsets_table_fragment (pebi, table, + peb_index, + sequence_id, + cur_page, + write_offset); +if (unlikely (err)) { +SSDFS_ERR ("fail to store offsets table's fragment: " +"sequence_id %u, cur_page %lu, " +"write_offset %u, err %d\n" , + sequence_id, *cur_page, + *write_offset, err); + goto fail_store_off_table; + } + + sequence_id =ssdfs_next_sequence_id (sequence_id); +if (sequence_id >SSDFS_BLK2OFF_TBL_REVERT_THRESHOLD ) { + err = -ERANGE ; +SSDFS_ERR ("invalid next sequence_id %u\n" , + sequence_id); + goto fail_store_off_table; + } + } + + err =ssdfs_blk2off_table_forget_snapshot (table, &snapshot, + extents, extent_count); +if (unlikely (err)) { +SSDFS_ERR ("fail to forget snapshot state: " +"peb_index %u, err %d\n" , + peb_index, err); + goto fail_store_off_table; + } + +BUG_ON (*write_offset <= table_start_offset); + desc->size =cpu_to_le32 (*write_offset - table_start_offset); + + pebi->current_log.seg_flags |=SSDFS_SEG_HDR_HAS_OFFSET_TABLE ; + +fail_store_off_table: +ssdfs_blk2off_table_free_snapshot (&snapshot); + +ssdfs_blk2off_kfree (extents); + +#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; +}
Signature · 1 line
2.34.1