Whamcloud - gitweb
LU-11510 lfs: migrate a composite layout file correctly
[fs/lustre-release.git] / lustre / scripts / lfs_migrate
index eb0799d..6e78e99 100755 (executable)
@@ -47,8 +47,8 @@ usage() {
     cat -- <<USAGE 1>&2
 usage: lfs_migrate [--dry-run|-n] [--help|-h] [--no-rsync|--rsync] [--quiet|-q]
                   [--auto-stripe|-A [-C <cap>]
-                    [--min-free|-M <min_free>] [--max-free|-X <max_free>]]
-                  [--stripe-count|-c <stripe_count>]
+                  [--min-free|-M <min_free>] [--max-free|-X <max_free>]]
+                  [--pool|-p <pool>] [--stripe-count|-c <stripe_count>]
                   [--stripe-size|-S <stripe_size>]
                   [-D] [-h] [-n] [-S]
                   [--restripe|-R] [--skip|-s] [--verbose|-v] [--yes|-y] [-0]
@@ -67,6 +67,7 @@ usage: lfs_migrate [--dry-run|-n] [--help|-h] [--no-rsync|--rsync] [--quiet|-q]
                   use in the migration
        --no-rsync do not fall back to rsync mode even if lfs migrate fails
        -n         only print the names of files to be migrated
+       -p <pool>  use the specified OST pool for the destination file
        -q         run quietly (don't print filenames or status)
        --rsync    force rsync mode instead of using lfs migrate
        -R         restripe file using default directory striping
@@ -112,10 +113,12 @@ OPT_DEBUG=false
 OPT_DRYRUN=false
 OPT_FILE=()
 OPT_LAYOUT=()
+OPT_COMP=false
 OPT_NO_RSYNC=false
 OPT_NO_DIRECT=false
 OPT_NULL=false
 OPT_PASSTHROUGH=()
+OPT_POOL=""
 OPT_RESTRIPE=false
 OPT_YES=false
 LFS_OPT_DIRECTIO=""
@@ -134,8 +137,10 @@ while [ -n "$*" ]; do
        case "$arg" in
        -h|--help) usage;;
        -l|--link) ;; # maintained backward compatibility for now
-       -n|--dry-run) OPT_DRYRUN=true; OPT_YES=true
+       -n) OPT_DRYRUN=true; OPT_YES=true
           echo "$PROG: -n deprecated, use --dry-run or --non-block" 1>&2;;
+       --dry-run) OPT_DRYRUN=true; OPT_YES=true;;
+       -p|--pool) OPT_POOL="$arg $2"; OPT_LAYOUT+="$OPT_POOL "; shift;;
        -q|--quiet) ECHO=:;;
        -R|--restripe) OPT_RESTRIPE=true;;
        -s|--skip) OPT_CHECK=false;;
@@ -147,9 +152,9 @@ while [ -n "$*" ]; do
           OPT_PASSTHROUGH+=("$arg");;
        --rsync) OPT_RSYNC=true;;
        --no-rsync) OPT_NO_RSYNC=true;;
-       --copy|--yaml|--file)
+       --copy|--yaml|--file) OPT_COMP=true;
           # these options have files as arguments, pass both through
-          OPT_LAYOUT+="$arg $2"; shift;;
+          OPT_LAYOUT+="$arg $2 "; shift;;
        --auto-stripe|-A) OPT_AUTOSTRIPE=true;;
        -C) OPT_CAP="$2"; shift;;
        -D) LFS_OPT_DIRECTIO="-D";;
@@ -163,22 +168,18 @@ while [ -n "$*" ]; do
        shift
 done
 
-if $OPT_RESTRIPE && [ -n "$OPT_LAYOUT" ]; then
-       echo "$PROG: Options $OPT_LAYOUT cannot be used with the -R option" 1>&2
+if $OPT_RESTRIPE || $OPT_AUTOSTRIPE && [ -n "$OPT_LAYOUT" ]; then
+       echo "$PROG error: Options '$OPT_LAYOUT' can't be used with -R or -A" \
+               1>&2
        exit 1
 elif $OPT_RESTRIPE && [[ "$OPT_STRIPE_COUNT" || "$OPT_STRIPE_SIZE" ]]; then
-       echo "$(basename $0): Options -c <stripe_count> and -S <stripe_size> "\
-       "may not be specified at the same time as the -R option." 1>&2
+       echo "$PROG error: Option -R can't be used with -c or -S" 1>&2
        exit 1
 elif $OPT_AUTOSTRIPE && [ -n "$OPT_STRIPE_COUNT" ]; then
-       echo ""
-       echo "$(basename $0) error: The -c <stripe_count> option may not" 1>&2
-       echo "be specified at the same time as the -A option." 1>&2
+       echo "$PROG error: Option -A can't be used with -c" 1>&2
        exit 1
 elif $OPT_AUTOSTRIPE && $OPT_RESTRIPE; then
-       echo ""
-       echo "$(basename $0) error: The -A option may not be specified at" 1>&2
-       echo "the same time as the -R option." 1>&2
+       echo "$PROG error: Option -A can't be used with -R" 1>&2
        exit 1
 fi
 
@@ -246,21 +247,18 @@ function calc_stripe()
                                        ost_min_kb=$avail
                                fi
                        fi
-               done < <($LFS df $OLDNAME | awk '/OST/ { print $4 }')
-               # Once this script supports pools, the lfs df command above
-               # should also include the -p <pool> option to restrict the
-               # listed OSTs to the correct pool.
+               done < <($LFS df $OPT_POOL $OLDNAME | awk '/OST/ { print $4 }')
 
                if [ $ost_max_count -eq 0 ]; then
-                       echo "no OSTs with sufficient available space" >&2
+                       # no OSTs with enough space, stripe over all of them
+                       echo "-1" "0"
                        return
                fi
 
                if (( ost_min_kb == (1 << 62) )); then
                        echo "warning: unable to determine minimum OST size, " \
                             "object size not capped" >&2
-                       obj_max_kb=0
-                       echo "$stripe_count" "$obj_max_kb"
+                       echo "$stripe_count" "0"
                        return
                fi
 
@@ -286,25 +284,29 @@ function calc_stripe()
 }
 
 lfs_migrate() {
+       local last_dev
+       local mntpoint
+
        while IFS='' read -d '' OLDNAME; do
                local hlinks=()
-               local stripe_size="$OPT_STRIPE_SIZE"
-               local stripe_count="$OPT_STRIPE_COUNT"
-               local parent_count=""
-               local parent_size=""
-               local stripe_pool
-               local mirror_count
                local layout
+               local fid
 
                $ECHO -n "$OLDNAME: "
 
-               # avoid duplicate stat if possible
-               local nlink_type=($(LANG=C stat -c "%h %F %s" "$OLDNAME" \
+               # avoid duplicate stat call by fetching all attrs at once
+               local nlink_idx_link=0 # %h is the hard link count
+               local nlink_idx_type=1 # %F is "regular file", ignore others
+               local nlink_idx_file=2 #       "file" is here
+               local nlink_idx_size=3 # %s is file size in bytes
+               local nlink_idx_dev=4  # %D is the underlying device number
+               # nlink_type=(1 regular file 1234 0x810)
+               local nlink_type=($(LANG=C stat -c "%h %F %s %D" "$OLDNAME" \
                                 2> /dev/null))
 
                # skip non-regular files, since they don't have any objects
                # and there is no point in trying to migrate them.
-               if [ "${nlink_type[1]}" != "regular" ]; then
+               if [ "${nlink_type[$nlink_idx_type]}" != "regular" ]; then
                        echo -e "\r$OLDNAME: not a regular file, skipped" 1>&2
                        continue
                fi
@@ -329,18 +331,14 @@ lfs_migrate() {
                fi
                OLDNAME=$oldname_absolute
 
-               # In the future, the path2fid and fid2path calls below
-               # should be replaced with a single call to
-               # "lfs path2links" once that command is available.  The logic
-               # for detecting unlisted hard links could then be removed.
-               local fid=$($LFS path2fid "$OLDNAME" 2> /dev/null)
-               if [ $? -ne 0 ]; then
-                       echo -e "\r$OLDNAME: cannot determine FID; skipping; " \
-                               "is this a Lustre file system?" 1>&2
-                       continue
-               fi
+               if [[ ${nlink_type[$nlink_idx_link]} -gt 1 ]] ||
+                  $RSYNC_WITH_HLINKS; then
+                       fid=$($LFS path2fid "$OLDNAME" 2> /dev/null)
+                       if [ $? -ne 0 ]; then
+                               echo -e "\r$OLDNAME: cannot get FID, skipping; is this a Lustre file system?" 1>&2
+                               continue
+                       fi
 
-               if [[ ${nlink_type[0]} -gt 1 ]] || $RSYNC_WITH_HLINKS; then
                        # don't migrate a hard link if it was already migrated
                        if path_in_set "$OLDNAME"; then
                                $ECHO "already migrated via another hard link"
@@ -355,45 +353,69 @@ lfs_migrate() {
                        local migrated=$(old_fid_in_set "$fid")
                        if [ -n "$migrated" ]; then
                                $ECHO "already migrated via another hard link"
-                               if $OPT_RSYNC; then
-                                       # Only the rsync case has to relink.
-                                       # The lfs migrate case preserves the
-                                       # inode so the links are already
-                                       # correct.
-                                       [ "$migrated" != "$OLDNAME" ] &&
-                                               ln -f "$migrated" "$OLDNAME"
-                               fi
+                               # Only the rsync case has to relink.  The
+                               # "lfs migrate" case keeps the same inode so
+                               # all of the links are already correct.
+                               $OPT_RSYNC && [ "$migrated" != "$OLDNAME" ] &&
+                                       ln -f "$migrated" "$OLDNAME"
+
                                add_to_set "$fid" "$OLDNAME"
                                continue;
                        fi
                fi
 
+               local stripe_size="$OPT_STRIPE_SIZE"
+               local stripe_count="$OPT_STRIPE_COUNT"
+               local stripe_opts="-N --comp-count -c -S -p -y"
+               local parent_count=""
+               local parent_size=""
+               local stripe_pool="${OPT_POOL#-p }"
+               local mirror_count=1
+               local comp_count=0
+               # avoid multiple getstripe calls
+               #   lcm_mirror_count:  1
+               #   lcm_entry_count:   0
+               #      lmm_stripe_count:  1
+               #      lmm_stripe_size:   1048576
+               #      lmm_pool:          pool_abc
+               local l_mirror_count=0
+               local l_comp_count=1
+               local l_stripe_count=2
+               local l_stripe_size=3
+               local l_stripe_pool=4
+               local layout_info=($($LFS getstripe $stripe_opts $OLDNAME \
+                       2>/dev/null | awk '{ print $2 }'))
+
+               layout="${OPT_PASSTHROUGH[@]} "
+
                if $OPT_RESTRIPE; then
                        UNLINK=""
+                       layout+="--copy $(dirname $OLDNAME)"
+                       OPT_COMP=true
                else
-                       # if rsync copies Lustre xattrs properly in the future
+                       # If rsync copies Lustre xattrs properly in the future
                        # (i.e. before the file data, so that it preserves
-                       # striping) then we don't need to do this getstripe
-                       # stuff.
+                       # striping) then we don't need this getstripe stuff.
                        UNLINK="-u"
 
-                       stripe_pool=$($LFS getstripe -p "$OLDNAME" 2> /dev/null)
-                       mirror_count=$($LFS getstripe -N "$OLDFILE" 2> /dev/null)
+                       [ -n "$OPT_POOL" ] ||
+                               stripe_pool=${layout_info[$l_stripe_pool]}
+                       mirror_count=${layout_info[$l_mirror_count]}
 
                        if $OPT_AUTOSTRIPE; then
-                               local filekb=$((${nlink_type[3]} / 1024))
+                               local filekb=$((${nlink_type[$nlink_idx_size]} /
+                                               1024))
 
                                read stripe_count OBJ_MAX_KB < <(calc_stripe \
                                        "$OLDNAME" "$filekb" "$OBJ_MAX_KB")
                                [ -z "$stripe_count" ] && exit 1
                                [ $stripe_count -lt 1 ] && stripe_count=1
                        else
-                               [ "$OPT_STRIPE_COUNT" ] && stripe_count=$OPT_STRIPE_COUNT ||
-                                       stripe_count=$($LFS getstripe -c "$OLDNAME" \
-                                               2> /dev/null)
+                               [ -n "$stripe_count" ] ||
+                                       stripe_count=${layout_info[$l_stripe_count]}
                        fi
-                       [ -z "$stripe_size" ] &&
-                               stripe_size=$($LFS getstripe -S "$OLDNAME" 2> /dev/null)
+                       [ -n "$stripe_size" ] ||
+                               stripe_size=${layout_info[$l_stripe_size]}
 
                        [ -z "$stripe_count" -o -z "$stripe_size" ] && UNLINK=""
                fi
@@ -401,26 +423,22 @@ lfs_migrate() {
                if $OPT_DEBUG; then
                        local parent_count
                        local parent_size
+                       local parent_layout
 
                        if $OPT_RESTRIPE; then
-                               parent_count=$($LFS getstripe -c \
-                                              $(dirname "$OLDNAME") 2> \
-                                              /dev/null)
-                               parent_size=$($LFS getstripe -S \
-                                             $(dirname "$OLDNAME") 2> \
-                                             /dev/null)
-                               stripe_pool=$($LFS getstripe --pool \
-                                             $(dirname "$OLDNAME") 2> \
-                                             /dev/null)
-                               mirror_count=$($LFS getstripe -N \
-                                              $(dirname "$OLDFILE") 2> \
-                                              /dev/null)
+                               parent_layout=($($LFS getstripe $stripe_opts \
+                                       -d $(dirname $OLDNAME) 2>/dev/null |
+                                               awk '{print $2 }'))
+                               parent_count=${parent_layout[$l_stripe_count]}
+                               parent_size=${parent_layout[$l_stripe_size]}
+                               stripe_pool=${parent_layout[$l_stripe_pool]}
+                               mirror_count=${parent_layout[$l_mirror_count]}
                        fi
 
                        $ECHO -n "stripe_count=${stripe_count:-$parent_count},stripe_size=${stripe_size:-$parent_size}"
                        [ -n "$stripe_pool" ] &&
                                $ECHO -n ",pool=${stripe_pool}"
-                       [ -n "$mirror_count" ] &&
+                       [[ $mirror_count -gt 1 ]] &&
                                $ECHO -n ",mirror_count=${mirror_count}"
                        $ECHO -n " "
                fi
@@ -430,33 +448,44 @@ lfs_migrate() {
                        continue
                fi
 
-               [ -n "$stripe_count" ] && stripe_count="-c $stripe_count"
-               [ -n "$stripe_size" ] && stripe_size="-S $stripe_size"
-               [ -n "$stripe_pool" ] && stripe_pool="-p $stripe_pool"
-               [ -n "$mirror_count" ] && mirror_count="-N $mirror_count"
-               layout="$stripe_count $stripe_size $stripe_pool $mirror_count \
-                       $OPT_LAYOUT"
+               if ! $OPT_COMP && [ ${layout_info[$l_comp_count]} -gt 0 ]; then
+                       layout+="--copy $OLDNAME"
+                       OPT_COMP=true
+               fi
+               if ! $OPT_COMP; then
+                       [ -n "$stripe_count" ] && layout+="-c $stripe_count "
+                       [ -n "$stripe_size" ] && layout+="-S $stripe_size "
+                       [ -n "$OPT_POOL" -a -n "$stripe_pool" ] &&
+                                               layout+="-p $stripe_pool "
+                       [[ $mirror_count -gt 1 ]] && layout+="-N $mirror_count "
+               fi
+               layout+="$OPT_LAYOUT"
 
                # detect other hard links and store them on a global
                # list so we don't re-migrate them
-               local mntpoint=$(df -P "$OLDNAME" |
-                               awk 'NR==2 { print $NF; exit }')
-               if [ -z "$mntpoint" ]; then
-                       echo -e "\r$OLDNAME: cannot determine mount point; skipped" 1>&2
-                       continue
-               fi
-               hlinks=$($LFS fid2path "$mntpoint" "$fid" 2> /dev/null)
-               if [ $? -ne 0 ]; then
-                       echo -e "\r$OLDNAME: cannot determine hard link paths, skipped" 1>&2
-                       continue
+               if [[ ${nlink_type[$nlink_idx_link]} -gt 1 ]]; then
+                       [ "${nlink_type[$nlink_idx_dev]}" == "$last_dev" ] ||
+                               mntpoint=$(df -P "$OLDNAME" |
+                                          awk 'NR==2 { print $NF }')
+                       if [ -z "$mntpoint" ]; then
+                               echo -e "\r$OLDNAME: cannot determine mount point; skipped" 1>&2
+                               continue
+                       fi
+                       hlinks=$($LFS fid2path "$mntpoint" "$fid" 2> /dev/null)
+                       if $OPT_RSYNC && [ $? -ne 0 ]; then
+                               echo -e "\r$OLDNAME: cannot determine hard link paths, skipped" 1>&2
+                               continue
+                       fi
+                       hlinks+=("$OLDNAME")
+               else
+                       hlinks=
                fi
-               hlinks+=("$OLDNAME")
 
                # first try to migrate via Lustre tools, then fall back to rsync
                if ! $OPT_RSYNC; then
-                       if $LFS migrate "${OPT_PASSTHROUGH[@]}" $layout \
-                          "$OLDNAME"; then
+                       if $LFS migrate $layout "$OLDNAME"; then
                                $ECHO "done"
+                               # no-op if hlinks empty for 1-link files
                                for link in ${hlinks[*]}; do
                                        add_to_set "$fid" "$link"
                                done
@@ -470,15 +499,16 @@ lfs_migrate() {
                        fi
                fi
 
-               NEWNAME=$(mktemp $UNLINK "$OLDNAME-lfs_migrate.tmp.XXXXXX")
+               local olddir=$(dirname $OLDNAME)
+               local oldfile=$(basename $OLDNAME)
+               NEWNAME=$(mktemp $UNLINK "$olddir/.$oldfile.XXXXXX")
                if [ $? -ne 0 -o -z "$NEWNAME" ]; then
                        echo -e "\r$OLDNAME: cannot make temp file, skipped" 1>&2
                        continue
                fi
 
                if [ "$UNLINK" ]; then
-                       if ! $LFS setstripe "${OPT_PASSTHROUGH[@]}" $layout \
-                            "$NEWNAME"; then
+                       if ! $LFS setstripe $layout "$NEWNAME"; then
                                echo -e "\r$NEWNAME: setstripe failed, exiting" 1>&2
                                exit 2
                        fi
@@ -501,6 +531,7 @@ lfs_migrate() {
                fi
 
                $ECHO "done rsync"
+               # no-op if hlinks empty for 1-link files
                for link in ${hlinks[*]}; do
                        if [ "$link" != "$OLDNAME" ]; then
                                ln -f "$OLDNAME" "$link"
@@ -513,7 +544,7 @@ lfs_migrate() {
                # of 1 (all other links will point to the new inode).
                # This flag indicates that even paths with a link count of
                # 1 are potentially part of a link set.
-               [ ${#hlinks[*]} -gt 1 ] && RSYNC_WITH_HLINKS=true
+               (( ${#hlinks[*]} == 1 )) || RSYNC_WITH_HLINKS=true
        done
 }