xref: /spdk/test/ftl/dirty_shutdown.sh (revision 7640e3fc9f22ca14c20c87930b6d6108a10f2f0c)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2019 Intel Corporation
4#  All rights reserved.
5#
6testdir=$(readlink -f $(dirname $0))
7rootdir=$(readlink -f $testdir/../..)
8source $rootdir/test/common/autotest_common.sh
9source $testdir/common.sh
10
11rpc_py=$rootdir/scripts/rpc.py
12spdk_dd="$SPDK_BIN_DIR/spdk_dd"
13
14while getopts ':u:c:' opt; do
15	case $opt in
16		u) uuid=$OPTARG ;;
17		c) nv_cache=$OPTARG ;;
18		?) echo "Usage: $0 [-u UUID] [-c NV_CACHE_PCI_BDF] BASE_PCI_BDF" && exit 1 ;;
19	esac
20done
21shift $((OPTIND - 1))
22
23device=$1
24timeout=240
25
26block_size=4096
27chunk_size=262144
28data_size=$chunk_size
29
30restore_kill() {
31	rm -f $testdir/config/ftl.json
32	rm -f $testdir/testfile
33	rm -f $testdir/testfile2
34	rm -f $testdir/testfile.md5
35	rm -f $testdir/testfile2.md5
36
37	killprocess $svcpid || true
38	rmmod nbd || true
39	remove_shm
40}
41
42trap "restore_kill; exit 1" SIGINT SIGTERM EXIT
43
44"$SPDK_BIN_DIR/spdk_tgt" -m 0x1 &
45svcpid=$!
46# Wait until spdk_tgt starts
47waitforlisten $svcpid
48
49split_bdev=$(create_base_bdev nvme0 $device $((1024 * 101)))
50
51if [ -n "$nv_cache" ]; then
52	nvc_bdev=$(create_nv_cache_bdev nvc0 $nv_cache $split_bdev)
53fi
54
55l2p_dram_size_mb=$(($(get_bdev_size $split_bdev) * 10 / 100 / 1024))
56ftl_construct_args="bdev_ftl_create -b ftl0 -d $split_bdev --l2p_dram_limit $l2p_dram_size_mb"
57
58[ -n "$uuid" ] && ftl_construct_args+=" -u $uuid"
59[ -n "$nv_cache" ] && ftl_construct_args+=" -c $nvc_bdev"
60
61$rpc_py -t $timeout $ftl_construct_args
62
63(
64	echo '{"subsystems": ['
65	$rpc_py save_subsystem_config -n bdev
66	echo ']}'
67) > $testdir/config/ftl.json
68
69# Load the nbd driver
70modprobe nbd
71$rpc_py nbd_start_disk ftl0 /dev/nbd0
72waitfornbd nbd0
73
74# Write and calculate checksum of the data written
75$spdk_dd -m 0x2 --if=/dev/urandom --of=$testdir/testfile --bs=$block_size --count=$data_size
76md5sum $testdir/testfile > $testdir/testfile.md5
77$spdk_dd -m 0x2 --if=$testdir/testfile --of=/dev/nbd0 --bs=$block_size --count=$data_size --oflag=direct
78sync /dev/nbd0
79$rpc_py nbd_stop_disk /dev/nbd0
80$rpc_py bdev_ftl_unload -b ftl0
81
82# Force kill bdev service (dirty shutdown) and start it again
83kill -9 $svcpid
84rm -f /dev/shm/spdk_tgt_trace.pid$svcpid
85
86# Write extra data after restore
87$spdk_dd --if=/dev/urandom --of=$testdir/testfile2 --bs=$block_size --count=$chunk_size
88$spdk_dd --if=$testdir/testfile2 --ob=ftl0 --count=$chunk_size --seek=$data_size --json=$testdir/config/ftl.json
89# Save md5 data
90md5sum $testdir/testfile2 > $testdir/testfile2.md5
91
92# Verify that the checksum matches and the data is consistent
93$spdk_dd --ib=ftl0 --of=$testdir/testfile --count=$data_size --json=$testdir/config/ftl.json
94md5sum -c $testdir/testfile.md5
95$spdk_dd --ib=ftl0 --of=$testdir/testfile2 --count=$chunk_size --skip=$data_size --json=$testdir/config/ftl.json
96md5sum -c $testdir/testfile2.md5
97
98trap - SIGINT SIGTERM EXIT
99restore_kill
100