• Qu Wenruo's avatar
    btrfs: rework btrfs_decompress_buf2page() · 1c3dc173
    Qu Wenruo authored
    There are several bugs inside the function btrfs_decompress_buf2page()
    
    - @start_byte doesn't take bvec.bv_offset into consideration
      Thus it can't handle case where the target range is not page aligned.
    
    - Too many helper variables
      There are tons of helper variables, @buf_offset, @current_buf_start,
      @start_byte, @prev_start_byte, @working_bytes, @bytes.
      This hurts anyone who wants to read the function.
    
    - No obvious main cursor for the iteartion
      A new problem caused by previous problem.
    
    - Comments for parameter list makes no sense
      Like @buf_start is the offset to @buf, or offset inside the full
      decompressed extent? (Spoiler alert, the later case)
      And @total_out acts more like @buf_start + @size_of_buf.
    
      The worst is @disk_start.
      The real meaning of it is the file offset of the full decompressed
      extent.
    
    This patch will rework the whole function by:
    
    - Add a proper comment with ASCII art to explain the parameter list
    
    - Rework parameter list
      The old @buf_start is renamed to @decompressed, to show how many bytes
      are already decompressed inside the full decompressed extent.
      The old @total_out is replaced by @buf_len, which is the decompressed
      data size.
      For old @disk_start and @bio, just pass @compressed_bio in.
    
    - Use single main cursor
      The main cursor will be @cur_file_offset, to show what's the current
      file offset.
      Other helper variables will be declared inside the main loop, and only
      minimal amount of helper variables:
      * offset_inside_decompressed_buf:	The only real helper
      * copy_start_file_offset:		File offset we start memcpy
      * bvec_file_offset:			File offset of current bvec
    
    Even with all these extensive comments, the final function is still
    smaller than the original function, which is definitely a win.
    Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    1c3dc173
compression.h 5.97 KB