1#!/bin/sh
2# Ensure dd invalidates to EOF when appropriate
3
4# Copyright (C) 2017-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ dd
21require_strace_ fadvise64,fadvise64_64
22
23head -c1234567 /dev/zero > in.f || framework_failure_
24
25# Check basic operation or skip.
26# We could check for 'Operation not supported' error here,
27# but that was seen to be brittle. HPUX returns ENOTTY for example.
28# So assume that if this basic operation fails, it's due to lack
29# of support by the system.
30dd if=in.f iflag=nocache count=0 ||
31  skip_ 'this file system lacks support for posix_fadvise()'
32
33strace_dd() {
34  strace -o dd.strace -e fadvise64,fadvise64_64 dd status=none "$@" || fail=1
35}
36
37advised_to_eof() {
38  grep -F ' 0, POSIX_FADV_DONTNEED' dd.strace >/dev/null
39}
40
41# The commented fadvise64 calls are what are expected with
42# a 4KiB page size and 128KiB IO_BUFSIZE.
43
44strace_dd if=in.f of=out.f bs=1M oflag=nocache,sync
45#fadvise64(1, 0, 1048576, POSIX_FADV_DONTNEED) = 0
46#fadvise64(1, 1048576, 131072, POSIX_FADV_DONTNEED) = 0
47#fadvise64(1, 1179648, 0, POSIX_FADV_DONTNEED) = 0
48advised_to_eof || fail=1
49
50strace_dd if=in.f count=0 iflag=nocache
51#fadvise64(0, 0, 0, POSIX_FADV_DONTNEED) = 0
52advised_to_eof || fail=1
53
54strace_dd if=in.f of=/dev/null iflag=nocache skip=10 count=300
55#fadvise64(0, 5120, 131072, POSIX_FADV_DONTNEED) = 0
56#fadvise64(0, 136192, 22528, POSIX_FADV_DONTNEED) = 0
57returns_ 1 advised_to_eof || fail=1
58
59strace_dd if=in.f of=/dev/null iflag=nocache bs=1M count=3000
60#fadvise64(0, 0, 1048576, POSIX_FADV_DONTNEED) = 0
61#fadvise64(0, 1048576, 131072, POSIX_FADV_DONTNEED) = 0
62#fadvise64(0, 1179648, 0, POSIX_FADV_DONTNEED) = 0
63advised_to_eof || fail=1
64
65strace_dd if=in.f of=/dev/null bs=1M count=1 iflag=nocache
66#fadvise64(0, 0, 1048576, POSIX_FADV_DONTNEED) = 0
67returns_ 1 advised_to_eof || fail=1
68
69strace_dd if=in.f of=out.f bs=1M iflag=nocache oflag=nocache,sync
70#fadvise64(0, 0, 1048576, POSIX_FADV_DONTNEED) = 0
71#fadvise64(1, 0, 1048576, POSIX_FADV_DONTNEED) = 0
72#fadvise64(0, 1048576, 131072, POSIX_FADV_DONTNEED) = 0
73#fadvise64(1, 1048576, 131072, POSIX_FADV_DONTNEED) = 0
74#fadvise64(0, 1179648, 0, POSIX_FADV_DONTNEED) = 0
75#fadvise64(1, 1179648, 0, POSIX_FADV_DONTNEED) = 0
76advised_to_eof || fail=1
77
78# Ensure sub page size offsets are handled.
79# I.e., only page aligned offsets are sent to fadvise.
80if ! strace -o dd.strace -e fadvise64,fadvise64_64 dd status=none \
81 if=in.f of=out.f bs=1M oflag=direct oseek=512B; then
82  warn_ '512 byte aligned O_DIRECT is not supported on this (file) system'
83  # The current file system may not support O_DIRECT,
84  # or older XFS had a page size alignment requirement
85else
86  #The first call is redundant but inconsequential
87  #fadvise64(1, 1048576, 0, POSIX_FADV_DONTNEED) = 0
88  #fadvise64(1, 1048576, 0, POSIX_FADV_DONTNEED) = 0
89  advised_to_eof || fail=1
90
91  strace_dd if=in.f of=out.f bs=1M oflag=direct
92  #fadvise64(1, 1048576, 0, POSIX_FADV_DONTNEED) = 0
93  #fadvise64(1, 1048576, 0, POSIX_FADV_DONTNEED) = 0
94  advised_to_eof || fail=1
95fi
96
97Exit $fail
98