##/bin/bash
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright (c) 2015 Oracle. All Rights Reserved.
+#
# Routines for populating a scratch fs, and helpers to exercise an FS
# once it's been fuzzed.
-#-----------------------------------------------------------------------
-# Copyright (c) 2015 Oracle. All Rights Reserved.
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
-# USA
-#
-# Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
-# Mountain View, CA 94043, USA, or: http://www.sgi.com
-#-----------------------------------------------------------------------
+
. ./common/quota
_require_populate_commands() {
_scratch_mount
blksz="$(stat -f -c '%s' "${SCRATCH_MNT}")"
dblksz="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep naming.*bsize | sed -e 's/^.*bsize=//g' -e 's/\([0-9]*\).*$/\1/g')"
+ crc="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep crc= | sed -e 's/^.*crc=//g' -e 's/\([0-9]*\).*$/\1/g')"
+ if [ $crc -eq 1 ]; then
+ leaf_hdr_size=64
+ else
+ leaf_hdr_size=16
+ fi
leaf_lblk="$((32 * 1073741824 / blksz))"
node_lblk="$((64 * 1073741824 / blksz))"
echo "+ leaf dir"
__populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_LEAF" "$((dblksz / 12))"
+ # - LEAFN
+ echo "+ leafn dir"
+ __populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_LEAFN" "$(( ((dblksz - leaf_hdr_size) / 8) - 3 ))"
+
# - NODE
echo "+ node dir"
__populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_NODE" "$((16 * dblksz / 40))" true
$XFS_IO_PROG -f -c "pwrite -S 0x62 0 $((blksz * nr))" "${SCRATCH_MNT}/BNOBT"
./src/punch-alternating "${SCRATCH_MNT}/BNOBT"
+ # Inode btree
+ echo "+ inobt btree"
+ local ino_per_rec=64
+ local rec_per_btblock=16
+ local nr="$(( 2 * (blksz / rec_per_btblock) * ino_per_rec ))"
+ local dir="${SCRATCH_MNT}/INOBT"
+ mkdir -p "${dir}"
+ seq 0 "${nr}" | while read f; do
+ touch "${dir}/${f}"
+ done
+
+ seq 0 2 "${nr}" | while read f; do
+ rm -f "${dir}/${f}"
+ done
+
# Reverse-mapping btree
is_rmapbt="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'rmapbt=1')"
if [ $is_rmapbt -gt 0 ]; then
"leaf")
(test "${datab}" -eq 1 && test "${leafb}" -eq 1 && test "${freeb}" -eq 0) || _fail "failed to create ${dtype} dir ino ${inode} datab ${datab} leafb ${leafb} freeb ${freeb}"
;;
+ "leafn")
+ _scratch_xfs_db -x -c "inode ${inode}" -c "dblock ${leaf_lblk}" -c "p lhdr.info.hdr.magic" | grep -q '0x3dff' && return
+ _scratch_xfs_db -x -c "inode ${inode}" -c "dblock ${leaf_lblk}" -c "p lhdr.info.magic" | grep -q '0xd2ff' && return
+ _fail "failed to create ${dtype} dir ino ${inode} datab ${datab} leafb ${leafb} freeb ${freeb}"
+ ;;
"node"|"btree")
(test "${datab}" -eq 1 && test "${leafb}" -eq 1 && test "${freeb}" -eq 1) || _fail "failed to create ${dtype} dir ino ${inode} datab ${datab} leafb ${leafb} freeb ${freeb}"
;;
inline_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_INLINE")"
block_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_BLOCK")"
leaf_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_LEAF")"
+ leafn_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_LEAFN")"
node_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_NODE")"
btree_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_BTREE")"
local_slink="$(__populate_find_inode "${SCRATCH_MNT}/S_IFLNK.FMT_LOCAL")"
__populate_check_xfs_dir "${inline_dir}" "inline"
__populate_check_xfs_dir "${block_dir}" "block"
__populate_check_xfs_dir "${leaf_dir}" "leaf"
+ __populate_check_xfs_dir "${leafn_dir}" "leafn"
__populate_check_xfs_dir "${node_dir}" "node"
__populate_check_xfs_dir "${btree_dir}" "btree"
__populate_check_xfs_dformat "${btree_dir}" "btree"
__populate_check_xfs_aformat "${btree_attr}" "btree"
__populate_check_xfs_agbtree_height "bno"
__populate_check_xfs_agbtree_height "cnt"
+ __populate_check_xfs_agbtree_height "ino"
+ test $is_finobt -ne 0 && __populate_check_xfs_agbtree_height "fino"
test $is_rmapbt -ne 0 && __populate_check_xfs_agbtree_height "rmap"
test $is_reflink -ne 0 && __populate_check_xfs_agbtree_height "refcnt"
}
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"
-
- # Don't keep metadata images cached for more 48 hours...
- rm -rf "$(find "${POPULATE_METADUMP}" -mtime +2 2>/dev/null)"
+# Compute the fs geometry description of a populated filesystem
+_scratch_populate_cache_tag() {
+ local extra_descr=""
+ local size="$(blockdev --getsz "${SCRATCH_DEV}")"
- # 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}"
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