common/rc: generalize _get_filesize()
[xfstests-dev.git] / common / populate
index 9595347663d5bfd9888a8e7cd3aa56178d1b222a..0b036051c8c30c738813826fe633942d76d74d17 100644 (file)
@@ -22,6 +22,26 @@ _require_xfs_db_blocktrash_z_command() {
 # Attempt to make files of "every" format for data, dirs, attrs etc.
 # (with apologies to Eric Sandeen for mutating xfser.sh)
 
+# Create a file of a given size.
+__populate_create_file() {
+       local sz="$1"
+       local fname="$2"
+
+       $XFS_IO_PROG -f -c "pwrite -S 0x62 -W -b 1m 0 $sz" "${fname}"
+}
+
+# Punch out every other hole in this file, if it exists.
+#
+# The goal here is to force the creation of a large number of metadata records
+# by creating a lot of tiny extent mappings in a file.  Callers should ensure
+# that fragmenting the file actually causes record creation.  Call this
+# function /after/ creating all other metadata structures.
+__populate_fragment_file() {
+       local fname="$1"
+
+       test -f "${fname}" && $here/src/punch-alternating "${fname}"
+}
+
 # Create a large directory
 __populate_create_dir() {
        name="$1"
@@ -156,13 +176,12 @@ _scratch_xfs_populate() {
        # Regular files
        # - FMT_EXTENTS
        echo "+ extents file"
-       $XFS_IO_PROG -f -c "pwrite -S 0x61 0 ${blksz}" "${SCRATCH_MNT}/S_IFREG.FMT_EXTENTS"
+       __populate_create_file $blksz "${SCRATCH_MNT}/S_IFREG.FMT_EXTENTS"
 
        # - FMT_BTREE
        echo "+ btree extents file"
        nr="$((blksz * 2 / 16))"
-       $XFS_IO_PROG -f -c "pwrite -S 0x62 0 $((blksz * nr))" "${SCRATCH_MNT}/S_IFREG.FMT_BTREE"
-       ./src/punch-alternating "${SCRATCH_MNT}/S_IFREG.FMT_BTREE"
+       __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/S_IFREG.FMT_BTREE"
 
        # Directories
        # - INLINE
@@ -257,8 +276,7 @@ _scratch_xfs_populate() {
        # Free space btree
        echo "+ freesp btree"
        nr="$((blksz * 2 / 8))"
-       $XFS_IO_PROG -f -c "pwrite -S 0x62 0 $((blksz * nr))" "${SCRATCH_MNT}/BNOBT"
-       ./src/punch-alternating "${SCRATCH_MNT}/BNOBT"
+       __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/BNOBT"
 
        # Inode btree
        echo "+ inobt btree"
@@ -271,7 +289,7 @@ _scratch_xfs_populate() {
                touch "${dir}/${f}"
        done
 
-       seq 0 "$((ino_per_rec + 1))" "${nr}" | while read f; do
+       seq 0 2 "${nr}" | while read f; do
                rm -f "${dir}/${f}"
        done
 
@@ -280,8 +298,7 @@ _scratch_xfs_populate() {
        if [ $is_rmapbt -gt 0 ]; then
                echo "+ rmapbt btree"
                nr="$((blksz * 2 / 24))"
-               $XFS_IO_PROG -f -c "pwrite -S 0x62 0 $((blksz * nr))" "${SCRATCH_MNT}/RMAPBT"
-               ./src/punch-alternating "${SCRATCH_MNT}/RMAPBT"
+               __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/RMAPBT"
        fi
 
        # Realtime Reverse-mapping btree
@@ -289,8 +306,7 @@ _scratch_xfs_populate() {
        if [ $is_rmapbt -gt 0 ] && [ $is_rt -gt 0 ]; then
                echo "+ rtrmapbt btree"
                nr="$((blksz * 2 / 32))"
-               $XFS_IO_PROG -f -R -c "pwrite -S 0x62 0 $((blksz * nr))" "${SCRATCH_MNT}/RTRMAPBT"
-               ./src/punch-alternating "${SCRATCH_MNT}/RTRMAPBT"
+               __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/RTRMAPBT"
        fi
 
        # Reference-count btree
@@ -298,15 +314,21 @@ _scratch_xfs_populate() {
        if [ $is_reflink -gt 0 ]; then
                echo "+ reflink btree"
                nr="$((blksz * 2 / 12))"
-               $XFS_IO_PROG -f -c "pwrite -S 0x62 0 $((blksz * nr))" "${SCRATCH_MNT}/REFCOUNTBT"
+               __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/REFCOUNTBT"
                cp --reflink=always "${SCRATCH_MNT}/REFCOUNTBT" "${SCRATCH_MNT}/REFCOUNTBT2"
-               ./src/punch-alternating "${SCRATCH_MNT}/REFCOUNTBT"
        fi
 
        # Copy some real files (xfs tests, I guess...)
        echo "+ real files"
        test $fill -ne 0 && __populate_fill_fs "${SCRATCH_MNT}" 5
 
+       # Make sure we get all the fragmentation we asked for
+       __populate_fragment_file "${SCRATCH_MNT}/S_IFREG.FMT_BTREE"
+       __populate_fragment_file "${SCRATCH_MNT}/BNOBT"
+       __populate_fragment_file "${SCRATCH_MNT}/RMAPBT"
+       __populate_fragment_file "${SCRATCH_MNT}/RTRMAPBT"
+       __populate_fragment_file "${SCRATCH_MNT}/REFCOUNTBT"
+
        umount "${SCRATCH_MNT}"
 }
 
@@ -333,17 +355,16 @@ _scratch_ext4_populate() {
        # Regular files
        # - FMT_INLINE
        echo "+ inline file"
-       $XFS_IO_PROG -f -c "pwrite -S 0x61 0 1" "${SCRATCH_MNT}/S_IFREG.FMT_INLINE"
+       __populate_create_file 1 "${SCRATCH_MNT}/S_IFREG.FMT_INLINE"
 
        # - FMT_EXTENTS
        echo "+ extents file"
-       $XFS_IO_PROG -f -c "pwrite -S 0x61 0 ${blksz}" "${SCRATCH_MNT}/S_IFREG.FMT_EXTENTS"
+       __populate_create_file $blksz "${SCRATCH_MNT}/S_IFREG.FMT_EXTENTS"
 
        # - FMT_ETREE
        echo "+ extent tree file"
        nr="$((blksz * 2 / 12))"
-       $XFS_IO_PROG -f -c "pwrite -S 0x62 0 $((blksz * nr))" "${SCRATCH_MNT}/S_IFREG.FMT_ETREE"
-       ./src/punch-alternating "${SCRATCH_MNT}/S_IFREG.FMT_ETREE"
+       __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/S_IFREG.FMT_ETREE"
 
        # Directories
        # - INLINE
@@ -406,6 +427,9 @@ _scratch_ext4_populate() {
        echo "+ real files"
        test $fill -ne 0 && __populate_fill_fs "${SCRATCH_MNT}" 5
 
+       # Make sure we get all the fragmentation we asked for
+       __populate_fragment_file "${SCRATCH_MNT}/S_IFREG.FMT_ETREE"
+
        umount "${SCRATCH_MNT}"
 }
 
@@ -753,7 +777,7 @@ _fill_fs()
                fi
 
                if [ -f $dir/$file_count ]; then
-                       bytes_written=$(stat -c '%s' $dir/$file_count)
+                       bytes_written=$(_get_filesize $dir/$file_count)
                fi
 
                # If there was no room to make the file, then divide it in
@@ -765,20 +789,14 @@ _fill_fs()
        done
 }
 
-# Populate a scratch FS from scratch or from a cached image.
-_scratch_populate_cached() {
-       POPULATE_METADUMP="${TEST_DIR}/__populate.${FSTYP}"
-       POPULATE_METADUMP_DESCR="${TEST_DIR}/__populate.${FSTYP}.txt"
+# Compute the fs geometry description of a populated filesystem
+_scratch_populate_cache_tag() {
+       local extra_descr=""
+       local size="$(blockdev --getsz "${SCRATCH_DEV}")"
 
-       # Don't keep metadata images cached for more 48 hours...
-       rm -rf "$(find "${POPULATE_METADUMP}" -mtime +2 2>/dev/null)"
-
-       # Throw away cached image if it doesn't match our spec.
        case "${FSTYP}" in
        "ext4")
                extra_descr="LOGDEV ${SCRATCH_LOGDEV} USE_EXTERNAL ${USE_EXTERNAL}"
-               # ext4 cannot e2image external logs, so we cannot restore
-               test -n "${SCRATCH_LOGDEV}" && rm -f "${POPULATE_METADUMP}"
                ;;
        "xfs")
                extra_descr="LOGDEV ${SCRATCH_LOGDEV} USE_EXTERNAL ${USE_EXTERNAL} RTDEV ${SCRATCH_RTDEV}"
@@ -787,23 +805,48 @@ _scratch_populate_cached() {
                        extra_descr="${extra_descr} QUOTAS"
                fi
                ;;
-       *)
-               extra_descr="";;
        esac
-       meta_descr="FSTYP ${FSTYP} MKFS_OPTIONS ${MKFS_OPTIONS} SIZE $(blockdev --getsz "${SCRATCH_DEV}") ${extra_descr} ARGS $@"
-       cmp -s "${POPULATE_METADUMP_DESCR}" <(echo "${meta_descr}") || rm -rf "${POPULATE_METADUMP}"
-
-       # Do we have a cached image?
-       if [ -r "${POPULATE_METADUMP}" ]; then
-               case "${FSTYP}" in
-               "xfs")
-                       xfs_mdrestore "${POPULATE_METADUMP}" "${SCRATCH_DEV}" && return
-                       ;;
-               "ext2"|"ext3"|"ext4")
-                       e2image -r "${POPULATE_METADUMP}" "${SCRATCH_DEV}" && return
-                       ;;
-               esac
-       fi
+       echo "FSTYP ${FSTYP} MKFS_OPTIONS ${MKFS_OPTIONS} SIZE ${size} ${extra_descr} ARGS $@"
+}
+
+# Restore a cached populated fs from a metadata dump
+_scratch_populate_restore_cached() {
+       local metadump="$1"
+
+       case "${FSTYP}" in
+       "xfs")
+               xfs_mdrestore "${metadump}" "${SCRATCH_DEV}" && return 0
+               ;;
+       "ext2"|"ext3"|"ext4")
+               # ext4 cannot e2image external logs, so we cannot restore
+               test -n "${SCRATCH_LOGDEV}" && return 1
+               e2image -r "${metadump}" "${SCRATCH_DEV}" && return 0
+               ;;
+       esac
+       return 1
+}
+
+# Populate a scratch FS from scratch or from a cached image.
+_scratch_populate_cached() {
+       local meta_descr="$(_scratch_populate_cache_tag "$@")"
+       local meta_tag="$(echo "${meta_descr}" | md5sum - | cut -d ' ' -f 1)"
+       local metadump_stem="${TEST_DIR}/__populate.${FSTYP}.${meta_tag}"
+
+       # These variables are shared outside this function
+       POPULATE_METADUMP="${metadump_stem}.metadump"
+       POPULATE_METADUMP_DESCR="${metadump_stem}.txt"
+
+       # Don't keep metadata images cached for more 48 hours...
+       rm -rf "$(find "${POPULATE_METADUMP}" -mtime +2 2>/dev/null)"
+
+       # Throw away cached image if it doesn't match our spec.
+       cmp -s "${POPULATE_METADUMP_DESCR}" <(echo "${meta_descr}") || \
+               rm -rf "${POPULATE_METADUMP}"
+
+       # Try to restore from the metadump
+       test -r "${POPULATE_METADUMP}" && \
+               _scratch_populate_restore_cached "${POPULATE_METADUMP}" && \
+               return
 
        # Oh well, just create one from scratch
        _scratch_mkfs