2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2015 Red Hat, Inc. All Rights Reserved.
5 # common functions for setting up and tearing down a dmthin device
8 # Create the 2 pool devices on lvs so we can build the whole thing
9 # from a single scratch device
12 DMTHIN_DATA_NAME="thin-data"
13 DMTHIN_DATA_DEV="/dev/mapper/$DMTHIN_DATA_NAME"
14 # Backing metadata dev
15 DMTHIN_META_NAME="thin-meta"
16 DMTHIN_META_DEV="/dev/mapper/$DMTHIN_META_NAME"
17 # Backing pool dev (combination of above)
18 DMTHIN_POOL_NAME="thin-pool"
19 DMTHIN_POOL_DEV="/dev/mapper/$DMTHIN_POOL_NAME"
21 DMTHIN_VOL_NAME="thin-vol"
22 DMTHIN_VOL_DEV="/dev/mapper/$DMTHIN_VOL_NAME"
24 echo $MOUNT_OPTIONS | grep -q dax
26 _notrun "Cannot run tests with DAX on dmthin devices"
31 $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
32 _dmsetup_remove $DMTHIN_VOL_NAME
33 _dmsetup_remove $DMTHIN_POOL_NAME
34 _dmsetup_remove $DMTHIN_META_NAME
35 _dmsetup_remove $DMTHIN_DATA_NAME
40 $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1
41 _check_scratch_fs $DMTHIN_VOL_DEV
44 # Set up a dm-thin device on $SCRATCH_DEV
46 # All arguments are optional, and in this order; defaults follows:
47 # data_dev_size: half of $SCRATCH_DEV
48 # virtual_size: 10x $data_dev_size
52 # You may specify 0 to 4 of these arguments, but they
53 # must be in the above order.
56 local data_dev_size=$1 # Backing pool data size in sectors
57 local virtual_size=$2 # Virtual size in sectors
58 local cluster_size=$3 # cluster/alloc size, sectors
59 local low_water=$4 # low water mark, sectors
61 local dm_backing_dev=$SCRATCH_DEV
62 local blk_dev_size=`blockdev --getsz $dm_backing_dev`
66 # Default to something small-ish
67 if [ -z "$data_dev_size" ]; then
68 data_dev_size=$(($blk_dev_size / 2))
71 # Default to something big-is; 10x backing
72 if [ -z "$virtual_size" ]; then
73 virtual_size=$(($data_dev_size * 10))
77 if [ -z "$cluster_size" ]; then
78 cluster_size=1024 # 512k in sectors
82 if [ -z "$low_water" ]; then
83 low_water=204800 # 100M, in sectors
85 # low_water is expressed in blocks of size $cluster_size
86 low_water=$((low_water / cluster_size))
88 # Need to make linear metadata and data devs. From kernel docs:
89 # As a guide, we suggest you calculate the number of bytes to use in the
90 # metadata device as 48 * $data_dev_size / $data_block_size but round it up
91 # to 2MB (4096 sectors) if the answer is smaller.
94 local meta_dev_size=$((48 * $data_dev_size / $cluster_size))
95 if [ "$meta_dev_size" -lt "4096" ]; then
96 meta_dev_size=4096 # 2MB
99 # scratch dev gets a metadata vol & data vol, start at this offset
100 local meta_dev_offset=10240
102 local total_data_dev_size=$(($meta_dev_offset + $meta_dev_size + $data_dev_size))
103 if [ "$total_data_dev_size" -gt "$blk_dev_size" ]; then
104 _notrun "$SCRATCH_DEV too small"
107 # Unmount & tear down old stuff
111 DMTHIN_META_TABLE="0 $meta_dev_size linear $dm_backing_dev $meta_dev_offset"
112 _dmsetup_create $DMTHIN_META_NAME --table "$DMTHIN_META_TABLE" || \
113 _fatal "failed to create dm thin meta device"
116 local data_dev_offset=$((meta_dev_offset + $meta_dev_size))
117 DMTHIN_DATA_TABLE="0 $data_dev_size linear $dm_backing_dev $data_dev_offset"
118 _dmsetup_create $DMTHIN_DATA_NAME --table "$DMTHIN_DATA_TABLE" || \
119 _fatal "failed to create dm thin data device"
121 # Zap the pool metadata dev
122 dd if=/dev/zero of=$DMTHIN_META_DEV bs=4096 count=1 &>/dev/null
125 # "start length thin-pool metadata_dev data_dev data_block_size low_water_mark"
126 DMTHIN_POOL_TABLE="0 $data_dev_size thin-pool $DMTHIN_META_DEV $DMTHIN_DATA_DEV $cluster_size $low_water"
127 _dmsetup_create $DMTHIN_POOL_NAME --table "$DMTHIN_POOL_TABLE" || \
128 _fatal "failed to create dm thin pool device"
131 $DMSETUP_PROG message $DMTHIN_POOL_DEV 0 "create_thin $pool_id" || \
132 _fatal "failed to message pool device"
134 # start length thin pool_dev dev_id [external_origin_dev]
135 DMTHIN_VOL_TABLE="0 $virtual_size thin $DMTHIN_POOL_DEV $pool_id"
136 _dmsetup_create $DMTHIN_VOL_NAME --table "$DMTHIN_VOL_TABLE" || \
137 _fatal "failed to create dm thin volume device"
142 _dmthin_reload_table()
147 $DMSETUP_PROG suspend $dev_name || \
148 _fail "dmsetup suspend of $dev_name failed"
150 $DMSETUP_PROG load $dev_name --table "$table" || \
151 _fail "dmsetup failed to reload $dev_name table"
153 $DMSETUP_PROG resume $dev_name || \
154 _fail "dmsetup resume of $dev_name failed"
158 # Grow the dm-thin device by the given amount
159 # Argument is number of sectors to add, if not specified
160 # defaults to 1/4 of the $SCRATCH_DEV size
163 local add_sectors=$1 # Number of sectors to add
165 local dm_backing_dev=$SCRATCH_DEV
166 local blk_dev_size=`blockdev --getsz $dm_backing_dev`
168 # Get current sizes & values
169 local meta_dev_size=`$DMSETUP_PROG table | grep ^$DMTHIN_META_NAME | awk '{print $3}'`
170 local meta_dev_offset=`$DMSETUP_PROG table | grep ^$DMTHIN_META_NAME | awk '{print $6}'`
171 local data_dev_size=`$DMSETUP_PROG table | grep ^$DMTHIN_DATA_NAME | awk '{print $3}'`
172 local pool_dev_size=`$DMSETUP_PROG table | grep ^$DMTHIN_POOL_NAME | awk '{print $3}'`
173 local cluster_size=`$DMSETUP_PROG table | grep ^$DMTHIN_POOL_NAME | awk '{print $7}'`
174 local low_water=`$DMSETUP_PROG table | grep ^$DMTHIN_POOL_NAME | awk '{print $8}'`
176 # default to 25% growth
177 if [ -z "$add_sectors" ]; then
178 add_sectors=$(($data_dev_size / 4))
181 local data_dev_offset=$(($meta_dev_offset + $meta_dev_size))
184 data_dev_size=$(($data_dev_size + $add_sectors))
185 pool_dev_size=$(($pool_dev_size + $add_sectors))
188 local total_data_dev_size=$(($meta_dev_offset + $meta_dev_size + $data_dev_size))
189 if [ "$total_data_dev_size" -gt "$blk_dev_size" ]; then
190 _fail "$SCRATCH_DEV too small"
193 # Grow the data device
194 DMTHIN_DATA_TABLE="0 $data_dev_size linear $dm_backing_dev $data_dev_offset"
195 _dmthin_reload_table $DMTHIN_DATA_NAME "$DMTHIN_DATA_TABLE"
198 DMTHIN_POOL_TABLE="0 $data_dev_size thin-pool $DMTHIN_META_DEV $DMTHIN_DATA_DEV $cluster_size $low_water"
199 _dmthin_reload_table $DMTHIN_POOL_NAME "$DMTHIN_POOL_TABLE"
202 # Queue IOs when full
205 local data_dev_size=`$DMSETUP_PROG table | grep $DMTHIN_DATA_NAME | awk '{print $3}'`
206 local cluster_size=`$DMSETUP_PROG table | grep $DMTHIN_POOL_NAME | awk '{print $7}'`
207 local low_water=`$DMSETUP_PROG table | grep $DMTHIN_POOL_NAME | awk '{print $8}'`
209 DMTHIN_POOL_TABLE="0 $data_dev_size thin-pool $DMTHIN_META_DEV $DMTHIN_DATA_DEV $cluster_size $low_water"
210 _dmthin_reload_table $DMTHIN_POOL_NAME "$DMTHIN_POOL_TABLE"
216 local data_dev_size=`$DMSETUP_PROG table | grep $DMTHIN_DATA_NAME | awk '{print $3}'`
217 local cluster_size=`$DMSETUP_PROG table | grep $DMTHIN_POOL_NAME | awk '{print $7}'`
218 local low_water=`$DMSETUP_PROG table | grep $DMTHIN_POOL_NAME | awk '{print $8}'`
220 DMTHIN_POOL_TABLE="0 $data_dev_size thin-pool $DMTHIN_META_DEV $DMTHIN_DATA_DEV $cluster_size $low_water 1 error_if_no_space"
221 _dmthin_reload_table $DMTHIN_POOL_NAME "$DMTHIN_POOL_TABLE"
224 _dmthin_mount_options()
226 echo `_common_dev_mount_options $*` $DMTHIN_VOL_DEV $SCRATCH_MNT
231 _mount -t $FSTYP `_dmthin_mount_options $*`