| ##/bin/bash |
| # |
| # Copyright (c) 2013 Fusion IO, Inc. All Rights Reserved. |
| # |
| # This program is free software; you can redistribute it and/or |
| # modify it under the terms of the GNU General Public License as |
| # published by the Free Software Foundation. |
| # |
| # This program is distributed in the hope that it would be useful, |
| # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| # GNU General Public License for more details. |
| # |
| # You should have received a copy of the GNU General Public License |
| # along with this program; if not, write the Free Software Foundation, |
| # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| # |
| # |
| # common functions for setting up and tearing down a dmflakey device |
| |
| FLAKEY_ALLOW_WRITES=0 |
| FLAKEY_DROP_WRITES=1 |
| FLAKEY_ERROR_WRITES=2 |
| |
| echo $MOUNT_OPTIONS | grep -q dax |
| if [ $? -eq 0 ]; then |
| _notrun "Cannot run tests with DAX on dmflakey devices" |
| fi |
| |
| _init_flakey() |
| { |
| local BLK_DEV_SIZE=`blockdev --getsz $SCRATCH_DEV` |
| FLAKEY_DEV=/dev/mapper/flakey-test |
| FLAKEY_TABLE="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 180 0" |
| FLAKEY_TABLE_DROP="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 0 180 1 drop_writes" |
| FLAKEY_TABLE_ERROR="0 $BLK_DEV_SIZE flakey $SCRATCH_DEV 0 0 180 1 error_writes" |
| $DMSETUP_PROG create flakey-test --table "$FLAKEY_TABLE" || \ |
| _fatal "failed to create flakey device" |
| $DMSETUP_PROG mknodes > /dev/null 2>&1 |
| } |
| |
| _mount_flakey() |
| { |
| _scratch_options mount |
| mount -t $FSTYP $SCRATCH_OPTIONS $MOUNT_OPTIONS $FLAKEY_DEV $SCRATCH_MNT |
| } |
| |
| _unmount_flakey() |
| { |
| $UMOUNT_PROG $SCRATCH_MNT |
| } |
| |
| _cleanup_flakey() |
| { |
| # If dmsetup load fails then we need to make sure to do resume here |
| # otherwise the umount will hang |
| $DMSETUP_PROG resume flakey-test > /dev/null 2>&1 |
| $UMOUNT_PROG $SCRATCH_MNT > /dev/null 2>&1 |
| # wait for device to be fully settled so that 'dmsetup remove' doesn't |
| # fail due to EBUSY |
| $UDEV_SETTLE_PROG >/dev/null 2>&1 |
| $DMSETUP_PROG remove flakey-test > /dev/null 2>&1 |
| $DMSETUP_PROG mknodes > /dev/null 2>&1 |
| } |
| |
| # _load_flakey_table <table> [lockfs] |
| # |
| # This defaults to --nolockfs, which doesn't freeze_fs() before loading the new |
| # table, so it simulates power failure. |
| _load_flakey_table() |
| { |
| table="$FLAKEY_TABLE" |
| [ $1 -eq $FLAKEY_DROP_WRITES ] && table="$FLAKEY_TABLE_DROP" |
| [ $1 -eq $FLAKEY_ERROR_WRITES ] && table="$FLAKEY_TABLE_ERROR" |
| |
| suspend_opt="--nolockfs" |
| [ $# -gt 1 ] && [ $2 -eq 1 ] && suspend_opt="" |
| |
| $DMSETUP_PROG suspend $suspend_opt flakey-test |
| [ $? -ne 0 ] && _fatal "failed to suspend flakey-test" |
| |
| # There may be multiple dm targets in the table, and these dm targets |
| # will be joined by the newline ("\n"). Option --table can not cope with |
| # the multiple-targets case, so get them by reading from standard input. |
| echo -e "$table" | $DMSETUP_PROG load flakey-test |
| [ $? -ne 0 ] && _fatal "failed to load table into flakey-test" |
| |
| $DMSETUP_PROG resume flakey-test |
| [ $? -ne 0 ] && _fatal "failed to resume flakey-test" |
| } |
| |
| # Silently drop all writes and unmount/remount to simulate a crash/power |
| # failure. |
| _flakey_drop_and_remount() |
| { |
| _load_flakey_table $FLAKEY_DROP_WRITES |
| _unmount_flakey |
| |
| if [ "x$1" = "xyes" ]; then |
| _check_scratch_fs $FLAKEY_DEV |
| fi |
| |
| _load_flakey_table $FLAKEY_ALLOW_WRITES |
| _mount_flakey |
| } |
| |
| _require_flakey_with_error_writes() |
| { |
| local SIZE |
| local TABLE |
| local NAME=flakey-test |
| |
| _require_dm_target flakey |
| |
| SIZE=`blockdev --getsz $SCRATCH_DEV` |
| TABLE="0 $SIZE flakey $SCRATCH_DEV 0 0 180 1 error_writes" |
| |
| $DMSETUP_PROG create $NAME --table "$TABLE" >/dev/null 2>&1 |
| if [ $? -ne 0 ]; then |
| _notrun "This test requires error_writes feature in dm-flakey" |
| fi |
| $DMSETUP_PROG mknodes >/dev/null 2>&1 |
| |
| _cleanup_flakey |
| } |