1 #! /bin/ksh -p
   2 #
   3 # CDDL HEADER START
   4 #
   5 # The contents of this file are subject to the terms of the
   6 # Common Development and Distribution License (the "License").
   7 # You may not use this file except in compliance with the License.
   8 #
   9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10 # or http://www.opensolaris.org/os/licensing.
  11 # See the License for the specific language governing permissions
  12 # and limitations under the License.
  13 #
  14 # When distributing Covered Code, include this CDDL HEADER in each
  15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16 # If applicable, add the following below this CDDL HEADER, with the
  17 # fields enclosed by brackets "[]" replaced with your own identifying
  18 # information: Portions Copyright [yyyy] [name of copyright owner]
  19 #
  20 # CDDL HEADER END
  21 #
  22 
  23 #
  24 # Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 
  28 #
  29 # Copyright (c) 2013 by Delphix. All rights reserved.
  30 #
  31 
  32 . $STF_SUITE/include/libtest.shlib
  33 . $STF_SUITE/tests/functional/snapshot/snapshot.cfg
  34 
  35 #
  36 # DESCRIPTION:
  37 #       use 'snapshot -r' to create a snapshot tree, add some files to one child
  38 #       filesystem, rollback the child filesystem snapshot, verify that the child
  39 #       filesystem gets back to the status while taking the snapshot.
  40 #
  41 # STRATEGY:
  42 #       1. Add some files to a target child filesystem
  43 #       2. snapshot -r the parent filesystem
  44 #       3. Add some other files to the target child filesystem
  45 #       4. rollback the child filesystem snapshot
  46 #       5. verify that the child filesystem get back to the status while being
  47 #          snapshot'd
  48 #
  49 
  50 verify_runnable "both"
  51 
  52 function cleanup
  53 {
  54         snapexists $SNAPPOOL && \
  55                 log_must $ZFS destroy -r $SNAPPOOL
  56 
  57         [[ -e $TESTDIR ]] && \
  58                 log_must $RM -rf $TESTDIR/* > /dev/null 2>&1
  59 }
  60 
  61 log_assert "Verify that rollback to a snapshot created by snapshot -r succeeds."
  62 log_onexit cleanup
  63 
  64 [[ -n $TESTDIR ]] && \
  65     log_must $RM -rf $TESTDIR/* > /dev/null 2>&1
  66 
  67 typeset -i COUNT=10
  68 
  69 log_note "Populate the $TESTDIR directory (prior to snapshot)"
  70 typeset -i i=0
  71 while (( i < COUNT )); do
  72         log_must $FILE_WRITE -o create -f $TESTDIR/before_file$i \
  73            -b $BLOCKSZ -c $NUM_WRITES -d $i
  74 
  75         (( i = i + 1 ))
  76 done
  77 
  78 log_must $ZFS snapshot -r $SNAPPOOL
  79 
  80 FILE_COUNT=`$LS -Al $SNAPDIR | $GREP -v "total" | wc -l`
  81 if (( FILE_COUNT != COUNT )); then
  82         $LS -Al $SNAPDIR
  83         log_fail "AFTER: $SNAPFS contains $FILE_COUNT files(s)."
  84 fi
  85 
  86 log_note "Populate the $TESTDIR directory (post snapshot)"
  87 typeset -i i=0
  88 while (( i < COUNT )); do
  89         log_must $FILE_WRITE -o create -f $TESTDIR/after_file$i \
  90            -b $BLOCKSZ -c $NUM_WRITES -d $i
  91 
  92         (( i = i + 1 ))
  93 done
  94 
  95 #
  96 # Now rollback to latest snapshot
  97 #
  98 log_must $ZFS rollback $SNAPFS
  99 
 100 FILE_COUNT=`$LS -Al $TESTDIR/after* 2> /dev/null | $GREP -v "total" | wc -l`
 101 if (( FILE_COUNT != 0 )); then
 102         $LS -Al $TESTDIR
 103         log_fail "$TESTDIR contains $FILE_COUNT after* files(s)."
 104 fi
 105 
 106 FILE_COUNT=`$LS -Al $TESTDIR/before* 2> /dev/null \
 107     | $GREP -v "total" | wc -l`
 108 if (( FILE_COUNT != $COUNT )); then
 109         $LS -Al $TESTDIR
 110         log_fail "$TESTDIR contains $FILE_COUNT before* files(s)."
 111 fi
 112 
 113 log_pass "Rollback with child snapshot works as expected."