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 2009 Sun Microsystems, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 
  28 
  29 #
  30 # Copyright (c) 2013 by Delphix. All rights reserved.
  31 #
  32 
  33 . $STF_SUITE/include/libtest.shlib
  34 . $STF_SUITE/tests/functional/snapshot/snapshot.cfg
  35 . $STF_SUITE/tests/functional/cli_root/zfs_rollback/zfs_rollback_common.kshlib
  36 
  37 #
  38 # DESCRIPTION:
  39 #       Verify snapshot can be created or destroy via mkdir or rm
  40 #       in .zfs/snapshot.
  41 #
  42 # STRATEGY:
  43 #       1. Verify make directories only successfully in .zfs/snapshot.
  44 #       2. Verify snapshot can be created and destroy via mkdir and remove
  45 #       directories in .zfs/snapshot.
  46 #       3. Verify rollback to previous snapshot can succeed.
  47 #       4. Verify remove directory in snapdir can destroy snapshot.
  48 #
  49 
  50 verify_runnable "both"
  51 
  52 function cleanup
  53 {
  54         typeset -i i=0
  55         while ((i < snap_cnt)); do
  56                 typeset snap=$fs@snap.$i
  57                 datasetexists $snap && log_must $ZFS destroy -f $snap
  58 
  59                 ((i += 1))
  60         done
  61 }
  62 
  63 $ZFS 2>&1 | $GREP "allow" > /dev/null
  64 (($? != 0)) && log_unsupported
  65 
  66 log_assert "Verify snapshot can be created via mkdir in .zfs/snapshot."
  67 log_onexit cleanup
  68 
  69 fs=$TESTPOOL/$TESTFS
  70 # Verify all the other directories are readonly.
  71 mntpnt=$(get_prop mountpoint $fs)
  72 snapdir=$mntpnt/.zfs
  73 set -A ro_dirs "$snapdir" "$snapdir/snap" "$snapdir/snapshot"
  74 for dir in ${ro_dirs[@]}; do
  75         if [[ -d $dir ]]; then
  76                 log_mustnot $RM -rf $dir
  77                 log_mustnot $TOUCH $dir/testfile
  78         else
  79                 log_mustnot $MKDIR $dir
  80         fi
  81 done
  82 
  83 # Verify snapshot can be created via mkdir in .zfs/snapshot
  84 typeset -i snap_cnt=5
  85 typeset -i cnt=0
  86 while ((cnt < snap_cnt)); do
  87         testfile=$mntpnt/testfile.$cnt
  88         log_must $MKFILE 1M $testfile
  89         log_must $MKDIR $snapdir/snapshot/snap.$cnt
  90         if ! datasetexists $fs@snap.$cnt ; then
  91                 log_fail "ERROR: $fs@snap.$cnt should exists."
  92         fi
  93 
  94         ((cnt += 1))
  95 done
  96 
  97 # Verify rollback to previous snapshot succeed.
  98 ((cnt = RANDOM % snap_cnt))
  99 log_must $ZFS rollback -r $fs@snap.$cnt
 100 
 101 typeset -i i=0
 102 while ((i < snap_cnt)); do
 103         testfile=$mntpnt/testfile.$i
 104         if ((i <= cnt)); then
 105                 if [[ ! -f $testfile ]]; then
 106                         log_fail "ERROR: $testfile should exists."
 107                 fi
 108         else
 109                 if [[ -f $testfile ]]; then
 110                         log_fail "ERROR: $testfile should not exists."
 111                 fi
 112         fi
 113 
 114         ((i += 1))
 115 done
 116 
 117 # Verify remove directory in snapdir can destroy snapshot.
 118 log_must $RMDIR $snapdir/snapshot/snap.$cnt
 119 log_mustnot datasetexists $fs@snap.$cnt
 120 
 121 log_pass "Verify snapshot can be created via mkdir in .zfs/snapshot passed."