DM: dm-inplace-compress: inplace compressed DM target
Subsystems (3)
PATCH v5 1/1 DM: dm-inplace-compress: inplace compressed DM target
This patch provides a generic device-mapper compression device.
Originally written by Shaohua Li.
https://www.redhat.com/archives/dm-devel/2013-December/msg00143.html
I have optimized and hardened the code.
I have not received any negative comments till now. Feel confident with the code. Please consider merging the code upstream.
Testing:
-------
This compression block device is tested in the following scenarios
a) backing a ext4/xfs/btrfs filesystem
b) backing swap
Ran 'badblocks' test on the compressed block device. Thoroughly stress tested on PPC64 and x86 system.
I have included a test-script that I used to test the block device.
Version v5:
Modified the parameter list format to use token=value. Fixed a coding issue noted by Julia Lawall.
Fixed data corruption issue when compressed size was same as the original data.
Modified the allowed maximum I/O size to be as large as two pages, without which larger size I/O need larger size buffers to temporarily hold compressed data. This can lead to inability to satisfy memory allocation requests.
Version v4:
fixed kbuild errors; hopefully they are all taken care off.
- no reference to zero_page
- convert all divide and mod operations to bit operations
Version v3:
Fixed sector alignment bugs exposed while testing on x86.
Explicitly set the maximum request size to 128K. Without which
range locking failed, causing I/Os to stamp each other.
Fixed an occasional data corruption caused by wrong size of the compression buffer.
Added a parameter while creation of the block device,
to not sleep during memory allocations. This can be useful
if the device is used as a swap device.
Version v2:
All patches are merged into a single patch.
Major code re-arrangement.
Data and metablocks allocated based on the length of the device map rather than the size of the backing device.
Size of each entry in the bitmap array is explicitly set
to 32bits.
Attempt to reuse the provided bio buffer space instead
of allocating a new one.
Version v1:
Comments from Alasdair have been incorporated. https://www.redhat.com/archives/dm-devel/2013-December/msg00144.html
Ram Pai (1):
From: Shaohua Li <[email protected]>
Documentation/device-mapper/dm-inplace-compress.txt | 174 + drivers/md/Kconfig | 6 drivers/md/Makefile | 2 drivers/md/dm-inplace-compress.c | 2295 ++++++++++++++++++++ drivers/md/dm-inplace-compress.h | 194 + 5 files changed, 2671 insertions(+)
---------- Test script -------------
#!/bin/bash
# a test program to verify the correctness of # the dm-inplace-compression target # - Ram Pai
compdevname="__compdev"usage()
{
echo
echo
echo "$1: -d <device path> [ -h ] [ -c <compdevicename> ]" echo "-d <device path> path to the block device to "
echo " back the compression device"
echo "-c <compdevicename> some unique name of the compress" echo " device name to be used. Defaults to $compdevname" echo "-h help"
echo
echo
return
}
getsize()
{
#the target will spew out the maximum size that
#it can accommodate for the device. So start with
#insane number and let it fail.
insane=999999999999999999990009
dmsetup create $2 --table \
"0 $insane inplacecompress device=$1, \
writethrough,compressor=lzo" 2>/dev/null
echo $(dmesg | \
grep 'dm-inplace-compress: This device can accommodate at most'\
| tail -1 | awk '{print $(NF-1)}')
}MYNAME=$(basename $0)
OPTIND=0
while getopts "d:c:h" args $OPTIONS
do
case "$args" in
d) device=$OPTARG
;;
c) compdevname=$OPTARG
;;
*) usage $MYNAME
exit 1
;;
esacdone
if [ -z "$device" ]
then
usage $MYNAME exit 1;
fi
if [ ! -b "$device" ]
then
usage $MYNAME echo ERROR: $device is not a block device exit 1;
fi
if [ -b "/dev/mapper/$compdevname" ]
then
echo "WARNING: $compdevname already exist" usage $MYNAME
fi
echo -n "ANY DATA ON $device WILL BE LOST. Continue using $device?: y/n:" read yesorno
if [ "$yesorno" != "y" ]
then
echo "Ok, exiting" exit 1;
fi
dmsetup targets | grep inplacecompress > /dev/null
if [ $? -ne 0 ]
then
echo "Please enable dminplacecompress target in the kernel" echo "Try modprobe dm-inplace-compress.ko"
exit 1
fi
#clean and init the device
dd if=/dev/zero of=$device count=100 2> /dev/null
dmsetup remove $compdevname 2> /dev/null
size=$(getsize $device $compdevname)
if [ ! -n $size ]
then
echo "FAILURE: determining the maximum possible size of the device" exit 1
fi
ret=0
for mode in writethrough writeback=2
do
for i in lzo 842
do
cat /proc/crypto | grep -w "$i$" > /dev/null if [ $? -ne 0 ] then continue fi echo "Testing: $i $mode....:" #generate the device dmsetup create $compdevname --table "0 $size inplacecompress device=$device, $mode ,compressor=$i" if [ $? -ne 0 ] then echo "FAILURE: creating the device" exit 1 fi badblocks -wsv /dev/mapper/$compdevname if [ $? -ne 0 ] then echo "FAILURE:" ret=1 fi ## ## ## ADD MORE TESTS HERE ## ## echo "PASS" dmsetup remove $compdevname
done
done
if [ $ret -eq 0 ]
then
echo CONGRATS IT WORKS
exit 0
else
echo FAIL
exit 0
fi
------------------------
PATCH v5 1/1 DM: inplace compressed DM target
This is a simple DM target supporting inplace compression. Its best suited for SSD. The underlying disk must support 512B sector size, the target only supports 4k sector size.
Disk layout:
|super|...meta...|..data...|
Store unit is 4k (a block). Super is 1 block, which stores meta and data size and compression algorithm. Meta is a bitmap. For each data
block, there are 5 bits meta.
Data:
Data of a block is compressed. Compressed data is round up to 512B,
which is the payload. In disk, payload is stored at the beginning of logical sector of the block. Let's look at an example. Say we store data to block A, which is in sector B(A*8), its orginal size is 4k,
compressed size is 1500. Compressed data (CD) will use three
sectors (512B). The three sectors are the payload. Payload will be stored at sector B.
---------------------------------------------------
... | CD1 | CD2 | CD3 | | | | | | ...
---------------------------------------------------
^B ^B+1 ^B+2 ^B+7 ^B+8
For this block, we will not use sector B+3 to B+7 (a hole). We use four meta bits to present payload size. The compressed size (1500) isn't stored in meta directly. Instead, we store it at the last 32bits of payload. In this example, we store it at the end of sector B+2. If
compressed size + sizeof(32bits) crosses a sector, payload size will
increase one sector. If payload uses 8 sectors, we store uncompressed data directly.
If IO size is bigger than one block, we can store the data as an extent. Data of the whole extent will compressed and stored in the similar way like above. The first block of the extent is the head, all others are the tail. If extent is 1 block, the block is head. We have 1 bit of meta to present if a block is head or tail. If 4 meta bits of head block can't store extent payload size, we will borrow tail block meta
bits to store payload size. Max allowd extent size is 128k, so we
don't compress/decompress too big size data.
Meta:
Modifying data will modify meta too. Meta will be written(flush) to
disk depending on meta write policy. We support writeback and
writethrough mode. In writeback mode, meta will be written to disk in an interval or a FLUSH request. In writethrough mode, data and meta data will be written to disk together.
Advantages:
1. Simple. Since we store compressed data in-place, we don't need complicated disk data management.
2. Efficient. For each 4k, we only need 5 bits meta. 1T data will use less than 200M meta, so we can load all meta into memory. And actual
compression size is in payload. So if IO doesn't need RMW and we use
writeback meta flush, we don't need extra IO for meta.
Disadvantages:
1. hole. Since we store compressed data in-place, there are a lot of
holes (in above example, B+3 - B+7) Hole can impact IO, because we can't do IO merge.
2. 1:1 size. Compression doesn't change disk size. If disk is 1T, we can only store 1T data even we do compression.
But this is for SSD only. Generally SSD firmware has a FTL layer to map disk sectors to flash nand. High end SSD firmware has filesystem-like FTL.
1. hole. Disk has a lot of holes, but SSD FTL can still store data
continuous in nand. Even if we can't do IO merge in OS layer, SSD
firmware can do it.
2. 1:1 size. On one side, we write compressed data to SSD, which means less data is written to SSD. This will be very helpful to improve SSD garbage collection, and so write speed and life cycle. So even this is a problem, the target is still helpful. On the other side, advanced SSD FTL can easily do thin provision. For example, if nand
is 1T and we let SSD report it as 2T, and use the SSD as compressed target. In such SSD, we don't have the 1:1
size issue.
So even if SSD FTL cannot map non-continuous disk sectors to
continuous nand, the compression target can still function well.
--- .../device-mapper/dm-inplace-compress.txt | 174 ++ drivers/md/Kconfig | 6 + drivers/md/Makefile | 2 + drivers/md/dm-inplace-compress.c | 2295 ++++++++++++++++++++ drivers/md/dm-inplace-compress.h | 194 ++ 5 files changed, 2671 insertions(+) create mode 100644 Documentation/device-mapper/dm-inplace-compress.txt create mode 100644 drivers/md/dm-inplace-compress.c create mode 100644 drivers/md/dm-inplace-compress.h
Documentation/device-mapper/dm-inplace-compress.txt new file +174 -0
@@ -0,0 +1,174 @@ +Device-Mapper's "inplace-compress" target provides inplace compression of block +devices using the kernel compression API. + +Parameters: <device>=<device path> | <device>:<device path> + [, <#opt_params writethough>, ] + [, <#opt_params <writeback>=<meta_commit_delay> ] + [, <#opt_params <writeback>:<meta_commit_delay> ] + [, <#opt_params compressor>=<type> ] + [, <#opt_params compressor>:<type> ] + [, <#opt_params critical> ] + + +<writethrough> + Write data and metadata together. + +<writeback>=<meta_commit_delay> + Write metadata every 'meta_commit_delay' interval. + +<device>=<device path> + This is the device that is going to be used as backend and contains the + compressed data. You can specify it as a path like /dev/xxx or a device + number <major>:<minor>. + +<compressor>=<type> + Choose the compressor algorithm. 'lzo' and '842' compressors are supported. + +<critical> + Block device used in critical path. + +Example scripts +=============== + +create a inplace-compress block device using lzo compression. Write metadata +and data together. + +[[ +#!/bin/sh +# Create a inplace-compress device using dmsetup +device=$1 #your backing storage eg: /dev/sdc1 +size=80000 #size of your new compressed block device +dmsetup create comp1 --table "0 $size inplacecompress device=$device, + writethrough, compressor=lzo" +]] + + +create a inplace-compress block device using nx-842 hardware compression. Write +metadata periodially every 5sec. + +[[ +#!/bin/sh +# Create a inplace-compress device using dmsetup +device=$1 #your backing storage eg: /dev/sdc1 +size=80000 #size of your new compressed block device +dmsetup create comp1 --table "0 $size inplacecompress device=$device, + writeback=5, compressor=842" +]] + + +Create a inplace-compress block device. Device is used in critical path; ex: +swap device. + +[[ +#!/bin/sh +# Create a inplace-compress device using dmsetup +device=$1 #your backing storage eg: /dev/sdc1 +size=80000 #size of your new compressed block device +dmsetup create comp1 --table "0 $size inplacecompress device=$device,critical" +]] + +Description +=========== + + This is a simple DM target supporting inplace compression. Its best suited + for SSD. The underlying disk must support 512B sector size, the target only + supports 4k sector size. + + + + Disk layout: + |super|...meta...|..data...| + + Store unit is 4k (a block). Superblock is 1 block. It stores meta and data + size and compression algorithm. Metablock is a bitmap. For each data block, + there are 5 bits meta. + + + + Data: + + Data of a block is compressed. Compressed data is round up to 512B, which + is the payload. On disk, payload is stored at the beginning of logical + sector of the block. Let's look at an example. Say we store data to block + A, which is in sector B(A*8), its orginal size is 4k, compressed size is + 1500. Compressed data (CD) will use three sectors (512B). The three sectors + are the payload. Payload will be stored at sector B. + + --------------------------------------------------- + ... | CD1 | CD2 | CD3 | | | | | | ... + --------------------------------------------------- + ^B ^B+1 ^B+2 ^B+7 ^B+8 + + For this block, we will not use sector B+3 to B+7 (a hole). We use 4 meta + bits to present payload size. The compressed size (1500) is not stored in + meta directly. Instead, we store it at the last 64bits of payload. In this + example, we store it at the end of sector B+2. If compressed size + + sizeof(64bits) crosses a sector, payload size will increase one sector. If + payload uses 8 sectors, we store uncompressed data directly. A compressed + size is 32bits and it is tagged with a 32bit magic number, to ensure its + integrity. + + If IO size is bigger than one block, we can store the data as an extent. + Data of the whole extent is compressed and stored in the similar way like + above. The first block of the extent is the head, all others are the tail. + If extent is one block, the block is head. We have one bit of meta to + indicate if a block is head or tail. If four meta bits of head block can't + store extent payload size, we will borrow tail block meta bits to store + payload size. Max allowd extent size is 128k. This is to gaurd against + compression/decompression of data that is too large. + + Meta: + + Modifying data modifies meta aswell. Metadata is written(flush) to disk + depending on metadata write policy. We support writeback and writethrough + mode. In writeback mode, meta will be written to disk periodically or when + a FLUSH request is initiated. In writethrough mode, data and meta data + will be written to disk together. + + Advantages: + + 1. Simple. Since we store compressed data in-place, we don't need + complicated disk data management. + + 2. Efficient. For each 4k, we only need 5 bits meta. 1T data will use less + than 200M meta, so we can load all meta into memory. Actual compression + size is in payload. This saves a metadata write if the IO does not need + RMW. + + + + Disadvantages: + + 1. Hole. Since we store compressed data in-place, there are a lot of holes + (in above example, B+3 - B+7) hole can impact IO, because we can't merge + the IO. + + 2. 1:1 size. Compression does not change disk size. If disk is 1T, we can + only store 1T data even we do compression. + + The above disadvantages can be mitigated by using SSDs or NVMe devices. + Generally these device firmware have a FTL layer to map disk sectors to + flash nand. Some high device firmware have filesystem-like FTL. + + 1. Hole. Disk has a lot of holes, but SSD FTL can still store data + continuous in nand. Even if we can't do IO merge in OS layer, SSD firmware + can do it. + + 2. 1:1 size. We write compressed data to SSD, which means less data is + written to SSD. This will be very helpful to improve SSD garbage + collection, write speed and increase its life span. Advanced device FTL + can easily do thin provision. For example, if nand is 1T and we let the + device report it as 2T, and use the SSD as compressed target. In such + cases, we alleviate the issue. + + + + + This target need not neccessarily be backed by FTL supporting device in + order to be functional. However having such a device can help maximize + the benefits. + + +Author: + Shaohua Li <[email protected]> + Ram Pai <[email protected]>
drivers/md/Kconfig +6 -0
@@ -508,4 +508,10 @@ config DM_LOG_WRITES If unsure, say N. +config DM_INPLACE_COMPRESS + tristate "Inplace Compression target" + depends on BLK_DEV_DM + ---help--- + Allow volume managers to compress data for SSD. + endif # MD
drivers/md/Makefile +2 -0
@@ -59,6 +59,8 @@ obj-$(CONFIG_DM_CACHE_SMQ) += dm-cache-smq.o obj-$(CONFIG_DM_CACHE_CLEANER) += dm-cache-cleaner.o obj-$(CONFIG_DM_ERA) += dm-era.o obj-$(CONFIG_DM_LOG_WRITES) += dm-log-writes.o +obj-$(CONFIG_DM_LOG_WRITES) += dm-log-writes.o +obj-$(CONFIG_DM_INPLACE_COMPRESS) += dm-inplace-compress.o ifeq ($(CONFIG_DM_UEVENT),y) dm-mod-objs += dm-uevent.o
drivers/md/dm-inplace-compress.c new file +2295 -0
2,295 lines not on this page · raw message
drivers/md/dm-inplace-compress.h new file +194 -0
194 lines not on this page · raw message
Signature · 1 line
1.8.3.1