ia64/xen-unstable

changeset 8126:8a00d9af3291

Tidy and comment use of canonicalise_mode, et al.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Tue Nov 29 15:01:47 2005 +0000 (2005-11-29)
parents 74fa88cf3dc7
children 4268d0ecdde7
files tools/examples/block
line diff
     1.1 --- a/tools/examples/block	Tue Nov 29 15:00:27 2005 +0000
     1.2 +++ b/tools/examples/block	Tue Nov 29 15:01:47 2005 +0000
     1.3 @@ -1,7 +1,5 @@
     1.4  #!/bin/sh
     1.5  
     1.6 -set -x
     1.7 -
     1.8  dir=$(dirname "$0")
     1.9  . "$dir/block-common.sh"
    1.10  
    1.11 @@ -19,30 +17,49 @@ expand_dev() {
    1.12  }
    1.13  
    1.14  
    1.15 +##
    1.16 +# canonicalise_mode mode
    1.17 +#
    1.18 +# Takes the given mode, which may be r, w, ro, rw, w!, or rw!, or variations
    1.19 +# thereof, and canonicalises them to one of
    1.20 +#
    1.21 +#   'r': perform checks for a new read-only mount;
    1.22 +#   'w': perform checks for a read-write mount; or
    1.23 +#   '!': perform no checks at all.
    1.24 +#
    1.25  canonicalise_mode()
    1.26  {
    1.27    local mode="$1"
    1.28  
    1.29    if ! expr index "$mode" 'w' >/dev/null
    1.30    then
    1.31 -    echo 'ro'
    1.32 +    echo 'r'
    1.33    elif ! expr index "$mode" '!' >/dev/null
    1.34    then
    1.35 -    echo 'rw'
    1.36 +    echo 'w'
    1.37    else
    1.38 -    echo 'no'
    1.39 +    echo '!'
    1.40    fi
    1.41  }
    1.42  
    1.43  
    1.44  ##
    1.45 -# check_sharing device device_major_minor mode
    1.46 +# check_sharing device mode
    1.47  #
    1.48  # Check whether the device requested is already in use.  To use the device in
    1.49  # read-only mode, it may be in use in read-only mode, but may not be in use in
    1.50  # read-write anywhere at all.  To use the device in read-write mode, it must
    1.51  # not be in use anywhere at all.
    1.52  #
    1.53 +# Prints one of
    1.54 +#
    1.55 +#    'local': the device may not be used because it is mounted in the current
    1.56 +#             (i.e. the privileged domain) in a way incompatible with the
    1.57 +#             requested mode;
    1.58 +#    'guest': the device may not be used because it already mounted by a guest
    1.59 +#             in a way incompatible with the requested mode; or
    1.60 +#    'ok':    the device may be used.
    1.61 +#
    1.62  check_sharing()
    1.63  {
    1.64    local dev="$1"
    1.65 @@ -51,7 +68,7 @@ check_sharing()
    1.66    local devmm=$(device_major_minor "$dev")
    1.67    local file
    1.68  
    1.69 -  if [ "$mode" == 'rw' ]
    1.70 +  if [ "$mode" == 'w' ]
    1.71    then
    1.72      toskip="^$"
    1.73    else
    1.74 @@ -79,7 +96,7 @@ check_sharing()
    1.75        local d=$(cat "$file")
    1.76        if [ "$d" == "$devmm" ]
    1.77        then
    1.78 -        if [ "$mode" == 'rw' ]
    1.79 +        if [ "$mode" == 'w' ]
    1.80          then
    1.81            echo 'guest'
    1.82            return
    1.83 @@ -100,13 +117,18 @@ check_sharing()
    1.84  }
    1.85  
    1.86  
    1.87 +##
    1.88 +# check_device_sharing dev mode
    1.89 +#
    1.90 +# Perform the sharing check for the given physical device and mode.
    1.91 +#
    1.92  check_device_sharing()
    1.93  {
    1.94    local dev="$1"
    1.95    local mode=$(canonicalise_mode "$2")
    1.96    local result
    1.97  
    1.98 -  if [ "$mode" == 'no' ]
    1.99 +  if [ "$mode" == '!' ]
   1.100    then
   1.101      return 0
   1.102    fi
   1.103 @@ -120,6 +142,12 @@ check_device_sharing()
   1.104  }
   1.105  
   1.106  
   1.107 +##
   1.108 +# check_device_sharing file dev mode
   1.109 +#
   1.110 +# Perform the sharing check for the given file mounted through the given
   1.111 +# loopback interface, in the given mode.
   1.112 +#
   1.113  check_file_sharing()
   1.114  {
   1.115    local file="$1"
   1.116 @@ -136,6 +164,13 @@ which is mounted " "$mode" "$result"
   1.117  }
   1.118  
   1.119  
   1.120 +##
   1.121 +# do_ebusy prefix mode result
   1.122 +#
   1.123 +# Helper function for check_device_sharing check_file_sharing, calling ebusy
   1.124 +# with an error message constructed from the given prefix, mode, and result
   1.125 +# from a call to check_sharing.
   1.126 +#
   1.127  do_ebusy()
   1.128  {
   1.129    local prefix="$1"
   1.130 @@ -151,7 +186,7 @@ do_ebusy()
   1.131      when='by a guest'
   1.132    fi
   1.133  
   1.134 -  if [ "$mode" == 'rw' ]
   1.135 +  if [ "$mode" == 'w' ]
   1.136    then
   1.137      m1=''
   1.138      m2=''
   1.139 @@ -195,7 +230,7 @@ case "$command" in
   1.140          file=$(readlink -f "$p")
   1.141          mode=$(canonicalise_mode "$mode")
   1.142  
   1.143 -        if [ "$mode" == 'rw' ] && ! stat "$file" -c %A | grep w >&/dev/null
   1.144 +        if [ "$mode" == 'w' ] && ! stat "$file" -c %A | grep -q w
   1.145          then
   1.146            ebusy \
   1.147  "File $file is read-only, and so I will not
   1.148 @@ -221,7 +256,7 @@ mount it read-write in a guest domain."
   1.149              if [ "$f" ]
   1.150              then
   1.151                # $dev is in use.  Check sharing.
   1.152 -              if [ "$mode" == 'no' ]
   1.153 +              if [ "$mode" == '!' ]
   1.154                then
   1.155                  continue
   1.156                fi