+# rsync creates its temporary files with lenient permissions, even if
+# permissions on the original files are more strict. Tighten umask here
+# to avoid the brief window where unprivileged users might be able to
+# access the temporary file.
+umask 0077
+
+# Use stripe count = sqrt(size_in_GB) + 1, but cap object size per OST.
+function calc_stripe()
+{
+ local filename=$1
+ local filekb=$2
+ local obj_max_kb=$3
+ local filegb=$((filekb / 1048576))
+ local stripe_count=1
+ local ost_max_count=0
+
+ # Files up to 1GB will have 1 stripe if they fit within the object max
+ if [[ $filegb -lt 1 && "$obj_max_kb" && $filekb -le $obj_max_kb ]]; then
+ echo 1 "$obj_max_kb" && return
+ fi
+
+ stripe_count=$(bc <<< "scale=0; 1 + sqrt($filegb)" 2> /dev/null) ||
+ { echo "cannot auto calculate stripe count" >&2; return; }
+
+ if [ -z "$obj_max_kb" ]; then
+ local ost_min_kb=$((1 << 62))
+
+ # Calculate cap on object size at 1% of smallest OST
+ # but only include OSTs that have 256MB+ available space
+ while IFS='' read avail; do
+ [[ "$OPT_MAXFREE" && $avail -gt $OPT_MAXFREE ]] &&
+ avail=$OPT_MAXFREE
+ if [ $avail -ge $OPT_MINFREE ]; then
+ ost_max_count=$((ost_max_count + 1))
+ if [ $avail -lt $ost_min_kb ]; then
+ ost_min_kb=$avail
+ fi
+ fi
+ done < <($LFS df $OPT_POOL $OLDNAME | awk '/OST/ { print $4 }')
+
+ if [ $ost_max_count -eq 0 ]; then
+ # 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
+ echo "$stripe_count" "0"
+ return
+ fi
+
+ obj_max_kb=$((ost_min_kb / $OPT_CAP))
+ elif [ $obj_max_kb -eq 0 ]; then
+ echo "warning: unable to determine minimum OST size " \
+ "from previous migrate, object size not capped" >&2
+ echo "$stripe_count" "$obj_max_kb"
+ return
+ fi
+
+ # If disk usage would exceed the cap, increase the number of stripes.
+ # Round up to the nearest MB to ensure file will fit.
+ (( filekb > stripe_count * obj_max_kb )) &&
+ stripe_count=$(((filekb + obj_max_kb - 1024) / obj_max_kb))
+
+ # Limit the count to the number of eligible OSTs
+ if [ "$stripe_count" -gt $ost_max_count ]; then
+ echo "$ost_max_count" "$obj_max_kb"
+ else
+ echo "$stripe_count" "$obj_max_kb"
+ fi
+}
+