1 #!nfsh
   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 2006 Sun Microsystems, Inc.  All rights reserved.
  25 # Use is subject to license terms.
  26 #
  27 
  28 #
  29 # This script is to test some irregular conditions of Lock/Open/Close 
  30 # operations:
  31 # 1. Try to close a locked file;
  32 # 2. Try to close a file twice;
  33 # 3. Locku without lock in file;
  34 # 4. Close with lock_stateid;
  35 #
  36 
  37 if { $argc != 2 } {
  38     puts "Usage: $argv0 <hostname> <pathname>"
  39     puts "       <pathname>: full path dir name to create test file."
  40     exit 1
  41 }
  42 
  43 # set host and file pathname (original)
  44 set host [ lindex $argv 0 ]
  45 set path [ path2comp [lindex $argv 1] "/" ]
  46 set fname "tfile.[pid]"
  47 
  48 # connect to the server
  49 connect $host
  50 
  51 # get dir file handle
  52 set dfh [get_fh $path]
  53 if {$dfh == ""} {
  54         puts stderr "ERROR: pathname=($path) not found."
  55         return 1
  56 }
  57 
  58 # negotiate the cleintid
  59 # set unique clientid and verifier
  60 set verifier "[clock clicks]01010"
  61 set owner "[pid]"
  62 
  63 set tag "SETCLIENTID"
  64 set res [compound {Setclientid $verifier $owner {0 0 0}}]
  65 puts "\nSetclientid $verifier $owner ..."
  66 puts "Res: $res"
  67 set clientid [lindex [lindex [lindex $res 0] 2] 0]
  68 set cid_verifier [lindex [lindex [lindex $res 0] 2] 1]
  69 
  70 # confirm clientid
  71 set tag "SETCLIENTID_CONFIRM"
  72 set res [compound {Setclientid_confirm $clientid $cid_verifier}]
  73 puts "\nSetclientid_confirm $clientid $cid_verifier ..."
  74 puts "Res: $res"
  75 
  76 # Now try to create (open_type=1) a new test file under $path with Open op
  77 set oseqid 1
  78 set otype 1
  79 set mode 0644
  80 set oclaim 0
  81 set tag "OPEN_CREATE"
  82 set res [compound {Putfh $dfh; 
  83         Open $oseqid 3 0 {$clientid $owner} \
  84         {$otype 0 {{mode $mode} {size 0}}} {$oclaim $fname};
  85         Getfh; Getattr {mode size}}]
  86 puts "\nOpen to create $fname (size=0) ..."
  87 puts -nonewline "  Open $oseqid 3 0 {$clientid $owner}"
  88 puts " {$otype 0 {{mode $mode} {size 0}}} {$oclaim $fname}"
  89 puts "Res: $res"
  90 if {$status != "OK"} {
  91         puts stderr "ERROR: Unable to create ($fname) with Open."
  92         return 2
  93 }
  94 # store the needed open info
  95 set open_sid [lindex [lindex $res 1] 2]
  96 set rflags [lindex [lindex $res 1] 4] 
  97 set nfh [lindex [lindex $res 2] 2]
  98 set attr [lindex [lindex $res 3] 2]
  99 incr oseqid
 100 
 101 # do open_confirm if needed, e.g. rflags==OPEN4_RESULT_CONFIRM=2
 102 if {[expr $rflags & 2] == 2} {
 103         set tag "OPEN_CONFIRM"
 104         set res [compound {Putfh $nfh; Open_confirm "$open_sid" $oseqid}]
 105         puts "\nOpen_confirm ($open_sid) $oseqid ..."
 106         puts "Res: $res"
 107         if {$status != "OK"} {
 108                 puts stderr "ERROR: unable to confirm created file $fname."
 109                 return 2
 110         }
 111         set open_sid [lindex [lindex $res 1] 2]
 112         incr oseqid
 113 }
 114 
 115 # Now try a WRITEW_LT Lock
 116 set lseqid 1
 117 set ltype 4
 118 set reclaim F
 119 set offset 16
 120 set length 1023
 121 set newlock T
 122 set tag "LOCK_WRITEW"
 123 set res [compound {Putfh $nfh; 
 124         Lock $ltype $reclaim $offset $length $newlock \
 125         $open_sid $lseqid {$oseqid $clientid $owner}}]
 126 puts "\nLock $ltype $reclaim $offset $length $newlock ($open_sid) $lseqid ..."
 127 puts "Res: $res"
 128 incr oseqid
 129 if {$status != "OK"} {
 130         puts stderr "ERROR: failed to set WRITEW_LT lock, Close and exit."
 131         set res [compound {Putfh $nfh; Close $oseqid $open_sid}]
 132         puts "\nafter fail to lock, Close $oseqid ($open_sid) ..."
 133         puts "Res: $res"
 134         return 2;
 135 }
 136 set lock_sid [lindex [lindex $res 1] 2]
 137 
 138 # The TEST #1, close this file without Locku - expect OK | LOCKS_HELD
 139 puts "\n** Test #1: Close this file without Locku - expect OK | LOCKS_HELD"
 140 set tag "CLOSE1"
 141 set res [compound {Putfh $nfh; Close $oseqid $open_sid}]
 142 puts "\nTest#1: Close $oseqid ($open_sid) (without Locku)..."
 143 puts "Res: $res"
 144 if { ($status != "OK") && ($status != "LOCKS_HELD") } {
 145         puts stderr "ERROR: close failed unexpectedly, statue=($status)."
 146         return 2
 147 }
 148 set close_sid [lindex [lindex $res 1] 2]
 149 
 150 # The TEST #2, try to close this closed file again - expect fails.
 151 puts "\n** Test #2: try to close this closed file - expect fails."
 152 set tag "CLOSE2"
 153 incr oseqid
 154 set res [compound {Putfh $nfh; Close $oseqid $close_sid}]
 155 puts "\ntry to close $oseqid $close_sid again ..."
 156 puts "Res: $res"
 157 if { ($status != "OK") && ($status != "OLD_STATEID") } {
 158         puts stderr "ERROR: close the file again returned status=($status)."
 159         puts stderr "\texpected to get OK|OLD_STATEID"
 160         return 2
 161 }
 162 
 163 # Test3, setup to open the file again.
 164 puts "\n** Setup to open the file again ..."
 165 set verifier "[clock clicks]01011"
 166 set owner "[pid]-2"
 167 set tag "SETCLIENTID2"
 168 set res [compound {Setclientid $verifier $owner {0 0 0}}]
 169 puts "\nSetclientid $verifier $owner ..."
 170 puts "Res: $res"
 171 set clientid [lindex [lindex [lindex $res 0] 2] 0]
 172 set cid_verifier [lindex [lindex [lindex $res 0] 2] 1]
 173 
 174 # confirm clientid
 175 set tag "SETCLIENTID_CONFIRM2"
 176 set res [compound {Setclientid_confirm $clientid $cid_verifier}]
 177 puts "\nSetclientid_confirm $clientid $cid_verifier ..."
 178 puts "Res: $res"
 179 
 180 # Now try to open the test file (open_type=0) file without create.
 181 set oseqid2 10
 182 set otype 0
 183 set mode 0644
 184 set oclaim 0
 185 set tag "OPEN-no-create"
 186 set res [compound {Putfh $dfh; 
 187         Open $oseqid2 3 0 {$clientid $owner} \
 188         {$otype 0 {{mode $mode}}} {$oclaim $fname};
 189         Getfh; Getattr {mode size}}]
 190 puts "\nOpen without create $fname ..."
 191 puts -nonewline "  Open $oseqid2 3 0 {$clientid $owner}"
 192 puts " {$otype 0 {{mode $mode}}} {$oclaim $fname}"
 193 puts "Res: $res"
 194 if {$status != "OK"} {
 195         puts stderr "ERROR: unable to Open ($fname)."
 196         return 2
 197 }
 198 # store the needed open info
 199 set open_sid2 [lindex [lindex $res 1] 2]
 200 set rflags [lindex [lindex $res 1] 4] 
 201 set nfh2 [lindex [lindex $res 2] 2]
 202 incr oseqid2
 203 
 204 # do open_confirm if needed, e.g. rflags==OPEN4_RESULT_CONFIRM=2
 205 if {[expr $rflags & 2] == 2} {
 206         set tag "OPEN_CONFIRM2"
 207         set res [compound {Putfh $nfh2; Open_confirm "$open_sid2" $oseqid2}]
 208         puts "\nOpen_confirm $open_sid2 $oseqid2 ..."
 209         puts "Res: $res"
 210         if {$status != "OK"} {
 211                 puts stderr "unable to confirm created file $fname."
 212                 return 2
 213         }
 214         set open_sid2 [lindex [lindex $res 1] 2]
 215         incr oseqid2
 216 }
 217 
 218 # Now Lockt without any lock in file using new clientid - expect OK
 219 puts "\n** Test #3: Lockt with no lock in file w/new clientid - expect OK"
 220 set lseqid2 1
 221 set ltype 3
 222 set offset 256
 223 set length 10
 224 puts "\nLockt $ltype $clientid $owner $offset $length ..."
 225 set tag "LOCKT2"
 226 set res [compound {Putfh $nfh2; Lockt $ltype $clientid $owner $offset $length}]
 227 puts "Res: $res"
 228 if {$status != "OK"} {
 229         puts stderr "ERROR: Lockt failed"
 230         set res [compound {Putfh $nfh2; Close $oseqid2 $open_sid2}]
 231         puts "\nafter fail to Lockt, Close $oseqid2 $open_sid2 ..."
 232         puts "Res: $res"
 233         return 2;
 234 }
 235 
 236 # Now Locku without any lock in file using old lock_sid - expect OLD_STATEID
 237 puts "\n** Test #4: Locku w/no lock in file w/old lock_sid - expect OK|OLD_STATEID"
 238 set ltype 3
 239 set offset 256
 240 set length 10
 241 incr lseqid
 242 set tag "LOCKU-w/noLock"
 243 set res [compound {Putfh $nfh2; 
 244         Locku $ltype $lseqid $lock_sid $offset $length}]
 245 puts "\nLocku $ltype $lseqid $lock_sid $offset $length..."
 246 puts "Res: $res"
 247 if { ($status != "OK") && ($status != "OLD_STATEID") } {
 248         puts stderr "ERROR: Locku failed w/status=($status)"
 249         puts stderr "\texpoected to get OK|OLD_STATEID"
 250         set res [compound {Putfh $nfh2; Close $oseqid2 $open_sid2}]
 251         puts "\nafter fail to unlock, Close $oseqid2 $open_sid2 ..."
 252         puts "Res: $res"
 253         return 2;
 254 }
 255 
 256 
 257 # Finally close the file and exit
 258 puts "\n** Finally close the file and exit - expect OK"
 259 set tag "CLOSE3"
 260 set res [compound {Putfh $nfh2; Close $oseqid2 $open_sid2}]
 261 puts "\nClose $oseqid2 $open_sid2 ..."
 262 puts "Res: $res"
 263 if {$status != "OK"} {
 264         puts stderr "ERROR: final Close failed"
 265         puts "Res: $res"
 266         return 2;
 267 }
 268 
 269 puts "\n** --- the end ---"
 270 
 271 disconnect
 272 exit 0