2 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright (c) 2015 Oracle. All Rights Reserved.
5 # Routines for populating a scratch fs, and helpers to exercise an FS
6 # once it's been fuzzed.
10 _require_populate_commands() {
11 _require_xfs_io_command "falloc"
12 _require_xfs_io_command "fpunch"
13 _require_test_program "punch-alternating"
14 _require_command "$XFS_DB_PROG" "xfs_db"
17 _require_xfs_db_blocktrash_z_command() {
18 test "${FSTYP}" = "xfs" || _notrun "cannot run xfs_db on ${FSTYP}"
19 $XFS_DB_PROG -x -f -c 'blocktrash -z' "${TEST_DEV}" | grep -q 'nothing on stack' || _notrun "blocktrash -z not supported"
22 # Attempt to make files of "every" format for data, dirs, attrs etc.
23 # (with apologies to Eric Sandeen for mutating xfser.sh)
25 # Create a file of a given size.
26 __populate_create_file() {
30 $XFS_IO_PROG -f -c "pwrite -S 0x62 -W -b 1m 0 $sz" "${fname}"
33 # Punch out every other hole in this file, if it exists.
35 # The goal here is to force the creation of a large number of metadata records
36 # by creating a lot of tiny extent mappings in a file. Callers should ensure
37 # that fragmenting the file actually causes record creation. Call this
38 # function /after/ creating all other metadata structures.
39 __populate_fragment_file() {
42 test -f "${fname}" && $here/src/punch-alternating "${fname}"
45 # Create a large directory
46 __populate_create_dir() {
52 seq 0 "${nr}" | while read d; do
54 test "$((d % 20))" -eq 0 && creat=touch
55 $creat "${name}/$(printf "%.08d" "$d")"
58 test -z "${missing}" && return
59 seq 1 2 "${nr}" | while read d; do
60 rm -rf "${name}/$(printf "%.08d" "$d")"
64 # Add a bunch of attrs to a file
65 __populate_create_attr() {
71 seq 0 "${nr}" | while read d; do
72 setfattr -n "user.$(printf "%.08d" "$d")" -v "$(printf "%.08d" "$d")" "${name}"
75 test -z "${missing}" && return
76 seq 1 2 "${nr}" | while read d; do
77 setfattr -x "user.$(printf "%.08d" "$d")" "${name}"
81 # Fill up some percentage of the remaining free space
82 __populate_fill_fs() {
85 test -z "${pct}" && pct=60
87 mkdir -p "${dir}/test/1"
88 cp -pRdu "${dir}"/S_IFREG* "${dir}/test/1/"
90 SRC_SZ="$(du -ks "${dir}/test/1" | cut -f 1)"
91 FS_SZ="$(( $(stat -f "${dir}" -c '%a * %S') / 1024 ))"
93 NR="$(( (FS_SZ * ${pct} / 100) / SRC_SZ ))"
96 echo "src_sz $SRC_SZ fs_sz $FS_SZ nr $NR"
97 seq 2 "${NR}" | while read nr; do
98 cp -pRdu "${dir}/test/1" "${dir}/test/${nr}"
102 # For XFS, force on all the quota options if quota is enabled
103 # and the user didn't feed us noquota.
104 _populate_xfs_qmount_option()
106 # User explicitly told us not to quota
107 if echo "${MOUNT_OPTIONS}" | grep -q 'noquota'; then
111 # Don't bother if we can't turn on quotas
112 if [ ! -f /proc/fs/xfs/xqmstat ]; then
115 elif [ "${USE_EXTERNAL}" = "yes" ] && [ ! -z "${SCRATCH_RTDEV}" ]; then
116 # Quotas not supported on rt filesystems
118 elif [ -z "${XFS_QUOTA_PROG}" ]; then
119 # xfs quota tools not installed
123 # Turn on all the quotas
124 if $XFS_INFO_PROG "${TEST_DIR}" | grep -q 'crc=1'; then
125 # v5 filesystems can have group & project quotas
126 quota="usrquota,grpquota,prjquota"
128 # v4 filesystems cannot mix group & project quotas
129 quota="usrquota,grpquota"
132 # Inject our quota mount options
133 if echo "${MOUNT_OPTIONS}" | grep -q "${quota}"; then
135 elif echo "${MOUNT_OPTIONS}" | egrep -q '(quota|noenforce)'; then
136 _qmount_option "${quota}"
138 export MOUNT_OPTIONS="$MOUNT_OPTIONS -o ${quota}"
139 echo "MOUNT_OPTIONS = $MOUNT_OPTIONS" >>$seqres.full
143 # Populate an XFS on the scratch device with (we hope) all known
144 # types of metadata block
145 _scratch_xfs_populate() {
155 _populate_xfs_qmount_option
157 blksz="$(stat -f -c '%s' "${SCRATCH_MNT}")"
158 dblksz="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep naming.*bsize | sed -e 's/^.*bsize=//g' -e 's/\([0-9]*\).*$/\1/g')"
159 crc="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep crc= | sed -e 's/^.*crc=//g' -e 's/\([0-9]*\).*$/\1/g')"
160 if [ $crc -eq 1 ]; then
165 leaf_lblk="$((32 * 1073741824 / blksz))"
166 node_lblk="$((64 * 1073741824 / blksz))"
170 # Fill up the root inode chunk
171 echo "+ fill root ino chunk"
172 seq 1 64 | while read f; do
173 $XFS_IO_PROG -f -c "truncate 0" "${SCRATCH_MNT}/dummy${f}"
178 echo "+ extents file"
179 __populate_create_file $blksz "${SCRATCH_MNT}/S_IFREG.FMT_EXTENTS"
182 echo "+ btree extents file"
183 nr="$((blksz * 2 / 16))"
184 __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/S_IFREG.FMT_BTREE"
189 __populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_INLINE" 1
193 __populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_BLOCK" "$((dblksz / 40))"
197 __populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_LEAF" "$((dblksz / 12))"
201 __populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_LEAFN" "$(( ((dblksz - leaf_hdr_size) / 8) - 3 ))"
205 __populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_NODE" "$((16 * dblksz / 40))" true
209 __populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_BTREE" "$((128 * dblksz / 40))" true
213 echo "+ inline symlink"
214 ln -s target "${SCRATCH_MNT}/S_IFLNK.FMT_LOCAL"
217 echo "+ extents symlink"
218 ln -s "$(perl -e 'print "x" x 1023;')" "${SCRATCH_MNT}/S_IFLNK.FMT_EXTENTS"
222 mknod "${SCRATCH_MNT}/S_IFCHR" c 1 1
223 mknod "${SCRATCH_MNT}/S_IFBLK" c 1 1
225 # special file with an xattr
226 setfacl -P -m u:nobody:r ${SCRATCH_MNT}/S_IFCHR
231 __populate_create_attr "${SCRATCH_MNT}/ATTR.FMT_LOCAL" 1
235 __populate_create_attr "${SCRATCH_MNT}/ATTR.FMT_LEAF" "$((blksz / 40))"
239 __populate_create_attr "${SCRATCH_MNT}/ATTR.FMT_NODE" "$((8 * blksz / 40))"
243 __populate_create_attr "${SCRATCH_MNT}/ATTR.FMT_BTREE" "$((64 * blksz / 40))" true
246 touch ${SCRATCH_MNT}/ATTR.TRUSTED
247 setfattr -n trusted.moo -v urk ${SCRATCH_MNT}/ATTR.TRUSTED
250 touch ${SCRATCH_MNT}/ATTR.SECURITY
251 setfattr -n security.foo -v bar ${SCRATCH_MNT}/ATTR.SECURITY
254 touch ${SCRATCH_MNT}/ATTR.SYSTEM
255 setfacl -m u:root:r ${SCRATCH_MNT}/ATTR.SYSTEM
257 # FMT_EXTENTS with a remote less-than-a-block value
258 echo "+ attr extents with a remote less-than-a-block value"
259 touch "${SCRATCH_MNT}/ATTR.FMT_EXTENTS_REMOTE3K"
260 $XFS_IO_PROG -f -c "pwrite -S 0x43 0 $((blksz - 300))" "${SCRATCH_MNT}/attrvalfile" > /dev/null
261 attr -q -s user.remotebtreeattrname "${SCRATCH_MNT}/ATTR.FMT_EXTENTS_REMOTE3K" < "${SCRATCH_MNT}/attrvalfile"
263 # FMT_EXTENTS with a remote block-size value
264 echo "+ attr extents with a remote one-block value"
265 touch "${SCRATCH_MNT}/ATTR.FMT_EXTENTS_REMOTE4K"
266 $XFS_IO_PROG -f -c "pwrite -S 0x44 0 ${blksz}" "${SCRATCH_MNT}/attrvalfile" > /dev/null
267 attr -q -s user.remotebtreeattrname "${SCRATCH_MNT}/ATTR.FMT_EXTENTS_REMOTE4K" < "${SCRATCH_MNT}/attrvalfile"
268 rm -rf "${SCRATCH_MNT}/attrvalfile"
270 # Make an unused inode
272 touch "${SCRATCH_MNT}/unused"
273 $XFS_IO_PROG -f -c 'fsync' "${SCRATCH_MNT}/unused"
274 rm -rf "${SCRATCH_MNT}/unused"
277 echo "+ freesp btree"
278 nr="$((blksz * 2 / 8))"
279 __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/BNOBT"
284 local rec_per_btblock=16
285 local nr="$(( 2 * (blksz / rec_per_btblock) * ino_per_rec ))"
286 local dir="${SCRATCH_MNT}/INOBT"
288 seq 0 "${nr}" | while read f; do
292 seq 0 2 "${nr}" | while read f; do
296 # Reverse-mapping btree
297 is_rmapbt="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'rmapbt=1')"
298 if [ $is_rmapbt -gt 0 ]; then
299 echo "+ rmapbt btree"
300 nr="$((blksz * 2 / 24))"
301 __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/RMAPBT"
304 # Realtime Reverse-mapping btree
305 is_rt="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'rtextents=[1-9]')"
306 if [ $is_rmapbt -gt 0 ] && [ $is_rt -gt 0 ]; then
307 echo "+ rtrmapbt btree"
308 nr="$((blksz * 2 / 32))"
309 __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/RTRMAPBT"
312 # Reference-count btree
313 is_reflink="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'reflink=1')"
314 if [ $is_reflink -gt 0 ]; then
315 echo "+ reflink btree"
316 nr="$((blksz * 2 / 12))"
317 __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/REFCOUNTBT"
318 cp --reflink=always "${SCRATCH_MNT}/REFCOUNTBT" "${SCRATCH_MNT}/REFCOUNTBT2"
321 # Copy some real files (xfs tests, I guess...)
323 test $fill -ne 0 && __populate_fill_fs "${SCRATCH_MNT}" 5
325 # Make sure we get all the fragmentation we asked for
326 __populate_fragment_file "${SCRATCH_MNT}/S_IFREG.FMT_BTREE"
327 __populate_fragment_file "${SCRATCH_MNT}/BNOBT"
328 __populate_fragment_file "${SCRATCH_MNT}/RMAPBT"
329 __populate_fragment_file "${SCRATCH_MNT}/RTRMAPBT"
330 __populate_fragment_file "${SCRATCH_MNT}/REFCOUNTBT"
332 umount "${SCRATCH_MNT}"
335 # Populate an ext4 on the scratch device with (we hope) all known
336 # types of metadata block
337 _scratch_ext4_populate() {
348 blksz="$(stat -f -c '%s' "${SCRATCH_MNT}")"
350 leaf_lblk="$((32 * 1073741824 / blksz))"
351 node_lblk="$((64 * 1073741824 / blksz))"
358 __populate_create_file 1 "${SCRATCH_MNT}/S_IFREG.FMT_INLINE"
361 echo "+ extents file"
362 __populate_create_file $blksz "${SCRATCH_MNT}/S_IFREG.FMT_EXTENTS"
365 echo "+ extent tree file"
366 nr="$((blksz * 2 / 12))"
367 __populate_create_file $((blksz * nr)) "${SCRATCH_MNT}/S_IFREG.FMT_ETREE"
372 __populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_INLINE" 1
376 __populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_BLOCK" "$((dblksz / 32))"
380 __populate_create_dir "${SCRATCH_MNT}/S_IFDIR.FMT_HTREE" "$((4 * dblksz / 24))"
384 echo "+ inline symlink"
385 ln -s target "${SCRATCH_MNT}/S_IFLNK.FMT_LOCAL"
388 echo "+ extents symlink"
389 ln -s "$(perl -e 'print "x" x 1023;')" "${SCRATCH_MNT}/S_IFLNK.FMT_EXTENTS"
393 mknod "${SCRATCH_MNT}/S_IFCHR" c 1 1
394 mknod "${SCRATCH_MNT}/S_IFBLK" c 1 1
396 # special file with an xattr
397 setfacl -P -m u:nobody:r ${SCRATCH_MNT}/S_IFCHR
402 __populate_create_attr "${SCRATCH_MNT}/ATTR.FMT_LOCAL" 0
406 __populate_create_attr "${SCRATCH_MNT}/ATTR.FMT_BLOCK" "$((blksz / 40))"
409 touch ${SCRATCH_MNT}/ATTR.TRUSTED
410 setfattr -n trusted.moo -v urk ${SCRATCH_MNT}/ATTR.TRUSTED
413 touch ${SCRATCH_MNT}/ATTR.SECURITY
414 setfattr -n security.foo -v bar ${SCRATCH_MNT}/ATTR.SECURITY
417 touch ${SCRATCH_MNT}/ATTR.SYSTEM
418 setfacl -m u:root:r ${SCRATCH_MNT}/ATTR.SYSTEM
420 # Make an unused inode
422 touch "${SCRATCH_MNT}/unused"
423 $XFS_IO_PROG -f -c 'fsync' "${SCRATCH_MNT}/unused"
424 rm -rf "${SCRATCH_MNT}/unused"
426 # Copy some real files (xfs tests, I guess...)
428 test $fill -ne 0 && __populate_fill_fs "${SCRATCH_MNT}" 5
430 # Make sure we get all the fragmentation we asked for
431 __populate_fragment_file "${SCRATCH_MNT}/S_IFREG.FMT_ETREE"
433 umount "${SCRATCH_MNT}"
436 # Find the inode number of a file
437 __populate_find_inode() {
439 inode="$(stat -c '%i' "${name}")"
443 # Check data fork format of XFS file
444 __populate_check_xfs_dformat() {
448 fmt="$(_scratch_xfs_db -c "inode ${inode}" -c 'p core.format' | sed -e 's/^.*(\([a-z]*\)).*$/\1/g')"
449 test "${format}" = "${fmt}" || _fail "failed to create ino ${inode} dformat expected ${format} saw ${fmt}"
452 # Check attr fork format of XFS file
453 __populate_check_xfs_aformat() {
457 fmt="$(_scratch_xfs_db -c "inode ${inode}" -c 'p core.aformat' | sed -e 's/^.*(\([a-z]*\)).*$/\1/g')"
458 test "${format}" = "${fmt}" || _fail "failed to create ino ${inode} aformat expected ${format} saw ${fmt}"
461 # Check structure of XFS directory
462 __populate_check_xfs_dir() {
466 (test -n "${leaf_lblk}" && test -n "${node_lblk}") || _fail "must define leaf_lblk and node_lblk before calling __populate_check_xfs_dir"
470 #echo "== check dir ${inode} type ${dtype}" ; _scratch_xfs_db -x -c "inode ${inode}" -c "bmap"
471 _scratch_xfs_db -x -c "inode ${inode}" -c "dblock 0" -c "stack" | grep -q 'file data block is unmapped' || datab=1
472 _scratch_xfs_db -x -c "inode ${inode}" -c "dblock ${leaf_lblk}" -c "stack" | grep -q 'file data block is unmapped' || leafb=1
473 _scratch_xfs_db -x -c "inode ${inode}" -c "dblock ${node_lblk}" -c "stack" | grep -q 'file data block is unmapped' || freeb=1
476 "shortform"|"inline"|"local")
477 (test "${datab}" -eq 0 && test "${leafb}" -eq 0 && test "${freeb}" -eq 0) || _fail "failed to create ${dtype} dir ino ${inode} datab ${datab} leafb ${leafb} freeb ${freeb}"
480 (test "${datab}" -eq 1 && test "${leafb}" -eq 0 && test "${freeb}" -eq 0) || _fail "failed to create ${dtype} dir ino ${inode} datab ${datab} leafb ${leafb} freeb ${freeb}"
483 (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}"
486 _scratch_xfs_db -x -c "inode ${inode}" -c "dblock ${leaf_lblk}" -c "p lhdr.info.hdr.magic" | grep -q '0x3dff' && return
487 _scratch_xfs_db -x -c "inode ${inode}" -c "dblock ${leaf_lblk}" -c "p lhdr.info.magic" | grep -q '0xd2ff' && return
488 _fail "failed to create ${dtype} dir ino ${inode} datab ${datab} leafb ${leafb} freeb ${freeb}"
491 (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}"
494 _fail "Unknown directory type ${dtype}"
498 # Check structure of XFS attr
499 __populate_check_xfs_attr() {
505 #echo "== check attr ${inode} type ${dtype}" ; _scratch_xfs_db -x -c "inode ${inode}" -c "bmap -a"
506 _scratch_xfs_db -x -c "inode ${inode}" -c "ablock 0" -c "stack" | grep -q 'file attr block is unmapped' || datab=1
507 _scratch_xfs_db -x -c "inode ${inode}" -c "ablock 1" -c "stack" | grep -q 'file attr block is unmapped' || leafb=1
510 "shortform"|"inline"|"local")
511 (test "${datab}" -eq 0 && test "${leafb}" -eq 0) || _fail "failed to create ${atype} attr ino ${inode} datab ${datab} leafb ${leafb}"
514 (test "${datab}" -eq 1 && test "${leafb}" -eq 0) || _fail "failed to create ${atype} attr ino ${inode} datab ${datab} leafb ${leafb}"
517 (test "${datab}" -eq 1 && test "${leafb}" -eq 1) || _fail "failed to create ${atype} attr ino ${inode} datab ${datab} leafb ${leafb}"
520 _fail "Unknown attribute type ${atype}"
524 # Check that there's at least one per-AG btree with multiple levels
525 __populate_check_xfs_agbtree_height() {
527 nr_ags=$(_scratch_xfs_db -c 'sb 0' -c 'p agcount' | awk '{print $3}')
530 "bno"|"cnt"|"rmap"|"refcnt")
532 bt_prefix="${bt_type}"
543 _fail "Don't know about AG btree ${bt_type}"
547 seq 0 $((nr_ags - 1)) | while read ag; do
548 bt_level=$(_scratch_xfs_db -c "${hdr} ${ag}" -c "p ${bt_prefix}level" | awk '{print $3}')
549 if [ "${bt_level}" -gt 1 ]; then
553 test $? -eq 100 || _fail "Failed to create ${bt_type} of sufficient height!"
557 # Check that populate created all the types of files we wanted
558 _scratch_xfs_populate_check() {
560 extents_file="$(__populate_find_inode "${SCRATCH_MNT}/S_IFREG.FMT_EXTENTS")"
561 btree_file="$(__populate_find_inode "${SCRATCH_MNT}/S_IFREG.FMT_BTREE")"
562 inline_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_INLINE")"
563 block_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_BLOCK")"
564 leaf_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_LEAF")"
565 leafn_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_LEAFN")"
566 node_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_NODE")"
567 btree_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_BTREE")"
568 local_slink="$(__populate_find_inode "${SCRATCH_MNT}/S_IFLNK.FMT_LOCAL")"
569 extents_slink="$(__populate_find_inode "${SCRATCH_MNT}/S_IFLNK.FMT_EXTENTS")"
570 bdev="$(__populate_find_inode "${SCRATCH_MNT}/S_IFBLK")"
571 cdev="$(__populate_find_inode "${SCRATCH_MNT}/S_IFCHR")"
572 local_attr="$(__populate_find_inode "${SCRATCH_MNT}/ATTR.FMT_LOCAL")"
573 leaf_attr="$(__populate_find_inode "${SCRATCH_MNT}/ATTR.FMT_LEAF")"
574 node_attr="$(__populate_find_inode "${SCRATCH_MNT}/ATTR.FMT_NODE")"
575 btree_attr="$(__populate_find_inode "${SCRATCH_MNT}/ATTR.FMT_BTREE")"
576 is_finobt=$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'finobt=1')
577 is_rmapbt=$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'rmapbt=1')
578 is_reflink=$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep -c 'reflink=1')
580 blksz="$(stat -f -c '%s' "${SCRATCH_MNT}")"
581 dblksz="$($XFS_INFO_PROG "${SCRATCH_MNT}" | grep naming.*bsize | sed -e 's/^.*bsize=//g' -e 's/\([0-9]*\).*$/\1/g')"
582 leaf_lblk="$((32 * 1073741824 / blksz))"
583 node_lblk="$((64 * 1073741824 / blksz))"
584 umount "${SCRATCH_MNT}"
586 __populate_check_xfs_dformat "${extents_file}" "extents"
587 __populate_check_xfs_dformat "${btree_file}" "btree"
588 __populate_check_xfs_dir "${inline_dir}" "inline"
589 __populate_check_xfs_dir "${block_dir}" "block"
590 __populate_check_xfs_dir "${leaf_dir}" "leaf"
591 __populate_check_xfs_dir "${leafn_dir}" "leafn"
592 __populate_check_xfs_dir "${node_dir}" "node"
593 __populate_check_xfs_dir "${btree_dir}" "btree"
594 __populate_check_xfs_dformat "${btree_dir}" "btree"
595 __populate_check_xfs_dformat "${bdev}" "dev"
596 __populate_check_xfs_dformat "${cdev}" "dev"
597 __populate_check_xfs_attr "${local_attr}" "local"
598 __populate_check_xfs_attr "${leaf_attr}" "leaf"
599 __populate_check_xfs_attr "${node_attr}" "node"
600 __populate_check_xfs_attr "${btree_attr}" "btree"
601 __populate_check_xfs_aformat "${btree_attr}" "btree"
602 __populate_check_xfs_agbtree_height "bno"
603 __populate_check_xfs_agbtree_height "cnt"
604 __populate_check_xfs_agbtree_height "ino"
605 test $is_finobt -ne 0 && __populate_check_xfs_agbtree_height "fino"
606 test $is_rmapbt -ne 0 && __populate_check_xfs_agbtree_height "rmap"
607 test $is_reflink -ne 0 && __populate_check_xfs_agbtree_height "refcnt"
610 # Check data fork format of ext4 file
611 __populate_check_ext4_dformat() {
618 debugfs -R "stat <${inode}>" "${dev}" 2> /dev/null | grep 'ETB[0-9]' -q && etree=1
619 iflags="$(debugfs -R "stat <${inode}>" "${dev}" 2> /dev/null | grep 'Flags:' | sed -e 's/^.*Flags: \([0-9a-fx]*\).*$/\1/g')"
620 test "$(echo "${iflags}" | awk '{print and(strtonum($1), 0x80000);}')" -gt 0 && extents=1
624 test "${extents}" -eq 0 || _fail "failed to create ino ${inode} with blockmap"
627 test "${extents}" -eq 1 || _fail "failed to create ino ${inode} with extents"
630 (test "${extents}" -eq 1 && test "${etree}" -eq 1) || _fail "failed to create ino ${inode} with extent tree"
633 _fail "Unknown dformat ${format}"
637 # Check attr fork format of ext4 file
638 __populate_check_ext4_aformat() {
644 debugfs -R "stat <${inode}>" "${dev}" 2> /dev/null | grep 'File ACL: 0' -q && ablock=0
648 test "${ablock}" -eq 0 || _fail "failed to create inode ${inode} with ${format} xattr"
651 test "${extents}" -eq 1 || _fail "failed to create inode ${inode} with ${format} xattr"
654 _fail "Unknown aformat ${format}"
658 # Check structure of ext4 dir
659 __populate_check_ext4_dir() {
666 iflags="$(debugfs -R "stat <${inode}>" "${dev}" 2> /dev/null | grep 'Flags:' | sed -e 's/^.*Flags: \([0-9a-fx]*\).*$/\1/g')"
667 test "$(echo "${iflags}" | awk '{print and(strtonum($1), 0x1000);}')" -gt 0 && htree=1
668 test "$(echo "${iflags}" | awk '{print and(strtonum($1), 0x10000000);}')" -gt 0 && inline=1
672 (test "${inline}" -eq 1 && test "${htree}" -eq 0) || _fail "failed to create ${dtype} dir ino ${inode} htree ${htree} inline ${inline}"
675 (test "${inline}" -eq 0 && test "${htree}" -eq 0) || _fail "failed to create ${dtype} dir ino ${inode} htree ${htree} inline ${inline}"
678 (test "${inline}" -eq 0 && test "${htree}" -eq 1) || _fail "failed to create ${dtype} dir ino ${inode} htree ${htree} inline ${inline}"
681 _fail "Unknown directory type ${dtype}"
686 # Check that populate created all the types of files we wanted
687 _scratch_ext4_populate_check() {
689 extents_file="$(__populate_find_inode "${SCRATCH_MNT}/S_IFREG.FMT_EXTENTS")"
690 etree_file="$(__populate_find_inode "${SCRATCH_MNT}/S_IFREG.FMT_ETREE")"
691 block_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_BLOCK")"
692 htree_dir="$(__populate_find_inode "${SCRATCH_MNT}/S_IFDIR.FMT_HTREE")"
693 extents_slink="$(__populate_find_inode "${SCRATCH_MNT}/S_IFLNK.FMT_EXTENTS")"
694 local_attr="$(__populate_find_inode "${SCRATCH_MNT}/ATTR.FMT_LOCAL")"
695 block_attr="$(__populate_find_inode "${SCRATCH_MNT}/ATTR.FMT_BLOCK")"
696 umount "${SCRATCH_MNT}"
698 __populate_check_ext4_dformat "${extents_file}" "extents"
699 __populate_check_ext4_dformat "${etree_file}" "etree"
700 __populate_check_ext4_dir "${block_dir}" "block"
701 __populate_check_ext4_dir "${htree_dir}" "htree"
702 __populate_check_ext4_dformat "${extents_slink}" "extents"
703 __populate_check_ext4_aformat "${local_attr}" "local"
704 __populate_check_ext4_aformat "${block_attr}" "block"
707 # Populate a scratch FS and check the contents to make sure we got that
708 _scratch_populate() {
711 _scratch_xfs_populate
712 _scratch_xfs_populate_check
714 "ext2"|"ext3"|"ext4")
715 _scratch_ext4_populate
716 _scratch_ext4_populate_check
719 _fail "Don't know how to populate a ${FSTYP} filesystem."
724 # Fill a file system by repeatedly creating files in the given folder
725 # starting with the given file size. Files are reduced in size when
726 # they can no longer fit until no more files can be created.
734 local bytes_written=0
737 if [ $# -ne 4 ]; then
738 echo "Usage: _fill_fs filesize dir blocksize switch_user"
742 if [ $switch_user -eq 0 ]; then
745 _user_do "mkdir -p $dir"
747 if [ ! -d $dir ]; then
751 testio=`$XFS_IO_PROG -F -fc "falloc 0 $block_size" $dir/$$.xfs_io 2>&1`
752 echo $testio | grep -q "not found" && use_falloc=0
753 echo $testio | grep -q "Operation not supported" && use_falloc=0
755 if [ $file_size -lt $block_size ]; then
756 $file_size = $block_size
759 while [ $file_size -ge $block_size ]; do
761 if [ $switch_user -eq 0 ]; then
762 if [ $use_falloc -eq 0 ]; then
763 $XFS_IO_PROG -fc "pwrite -b 8388608 0 $file_size" \
766 $XFS_IO_PROG -fc "falloc 0 $file_size" \
770 if [ $use_falloc -eq 0 ]; then
771 _user_do "$XFS_IO_PROG -f -c \"pwrite -b 8388608 0 \
772 $file_size\" $dir/$file_count"
774 _user_do "$XFS_IO_PROG -f -c \"falloc 0 \
775 $file_size\" $dir/$file_count"
779 if [ -f $dir/$file_count ]; then
780 bytes_written=$(_get_filesize $dir/$file_count)
783 # If there was no room to make the file, then divide it in
784 # half, and keep going
785 if [ $bytes_written -lt $file_size ]; then
786 file_size=$((file_size / 2))
788 file_count=$((file_count + 1))
792 # Compute the fs geometry description of a populated filesystem
793 _scratch_populate_cache_tag() {
795 local size="$(blockdev --getsz "${SCRATCH_DEV}")"
799 extra_descr="LOGDEV ${SCRATCH_LOGDEV} USE_EXTERNAL ${USE_EXTERNAL}"
802 extra_descr="LOGDEV ${SCRATCH_LOGDEV} USE_EXTERNAL ${USE_EXTERNAL} RTDEV ${SCRATCH_RTDEV}"
803 _populate_xfs_qmount_option
804 if echo "${MOUNT_OPTIONS}" | grep -q 'usrquota'; then
805 extra_descr="${extra_descr} QUOTAS"
809 echo "FSTYP ${FSTYP} MKFS_OPTIONS ${MKFS_OPTIONS} SIZE ${size} ${extra_descr} ARGS $@"
812 # Restore a cached populated fs from a metadata dump
813 _scratch_populate_restore_cached() {
818 xfs_mdrestore "${metadump}" "${SCRATCH_DEV}" && return 0
820 "ext2"|"ext3"|"ext4")
821 # ext4 cannot e2image external logs, so we cannot restore
822 test -n "${SCRATCH_LOGDEV}" && return 1
823 e2image -r "${metadump}" "${SCRATCH_DEV}" && return 0
829 # Populate a scratch FS from scratch or from a cached image.
830 _scratch_populate_cached() {
831 local meta_descr="$(_scratch_populate_cache_tag "$@")"
832 local meta_tag="$(echo "${meta_descr}" | md5sum - | cut -d ' ' -f 1)"
833 local metadump_stem="${TEST_DIR}/__populate.${FSTYP}.${meta_tag}"
835 # These variables are shared outside this function
836 POPULATE_METADUMP="${metadump_stem}.metadump"
837 POPULATE_METADUMP_DESCR="${metadump_stem}.txt"
839 # Don't keep metadata images cached for more 48 hours...
840 rm -rf "$(find "${POPULATE_METADUMP}" -mtime +2 2>/dev/null)"
842 # Throw away cached image if it doesn't match our spec.
843 cmp -s "${POPULATE_METADUMP_DESCR}" <(echo "${meta_descr}") || \
844 rm -rf "${POPULATE_METADUMP}"
846 # Try to restore from the metadump
847 test -r "${POPULATE_METADUMP}" && \
848 _scratch_populate_restore_cached "${POPULATE_METADUMP}" && \
851 # Oh well, just create one from scratch
853 echo "${meta_descr}" > "${POPULATE_METADUMP_DESCR}"
856 _scratch_xfs_populate $@
857 _scratch_xfs_populate_check
858 _scratch_metadump "${POPULATE_METADUMP}" -a -o
860 "ext2"|"ext3"|"ext4")
861 _scratch_ext4_populate $@
862 _scratch_ext4_populate_check
863 e2image -Q "${SCRATCH_DEV}" "${POPULATE_METADUMP}"
866 _fail "Don't know how to populate a ${FSTYP} filesystem."