generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / common / module
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0+
3 # Copyright (c) 2017 Oracle.  All Rights Reserved.
4 #
5 # Routines for messing around with loadable kernel modules
6
7 # Return the module name for this fs.
8 _module_for_fs()
9 {
10         echo "${FSTYP}"
11 }
12
13 # Reload a particular module.  This module MUST NOT be the module that
14 # underlies the filesystem.
15 _reload_module()
16 {
17         local module="$1"
18
19         _patient_rmmod "${module}" || _fail "${module} unload failed"
20         modprobe "${module}" || _fail "${module} load failed"
21 }
22
23 # Reload the filesystem module.
24 _reload_fs_module()
25 {
26         local module="$1"
27
28         # Unload test fs, try to reload module, remount
29         local had_testfs=""
30         local had_scratchfs=""
31         _check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR && had_testfs="true"
32         _check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT && had_scratchfs="true"
33         test -n "${had_testfs}" && _test_unmount
34         test -n "${had_scratchfs}" && _scratch_unmount
35         _reload_module "${module}"
36         test -n "${had_scratchfs}" && _scratch_mount 2> /dev/null
37         test -n "${had_testfs}" && _test_mount 2> /dev/null
38 }
39
40 # Check that we have a module that can be loaded.  This module MUST NOT
41 # be the module that underlies the filesystem.
42 _require_loadable_module()
43 {
44         local module="$1"
45
46         modinfo "${module}" > /dev/null 2>&1 || _notrun "${module}: must be a module."
47         _patient_rmmod "${module}" || _notrun "Require ${module} to be unloadable"
48         modprobe "${module}" || _notrun "${module} load failed"
49 }
50
51 # Test if the module for FSTYP can be unloaded and reloaded.
52 #
53 # If not, returns 1 if $FSTYP is not a loadable module; 2 if the module could
54 # not be unloaded; or 3 if loading the module fails.
55 _test_loadable_fs_module()
56 {
57         local module="$1"
58
59         modinfo "${module}" > /dev/null 2>&1 || return 1
60
61         # Unload test fs, try to reload module, remount
62         local had_testfs=""
63         local had_scratchfs=""
64         _check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR && had_testfs="true"
65         _check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT && had_scratchfs="true"
66         test -n "${had_testfs}" && _test_unmount
67         test -n "${had_scratchfs}" && _scratch_unmount
68         local unload_ok=""
69         local load_ok=""
70         _patient_rmmod "${module}" || unload_ok=0
71         modprobe "${module}" || load_ok=0
72         test -n "${had_scratchfs}" && _scratch_mount 2> /dev/null
73         test -n "${had_testfs}" && _test_mount 2> /dev/null
74         test -z "${unload_ok}" || return 2
75         test -z "${load_ok}" || return 3
76         return 0
77 }
78
79 _require_loadable_fs_module()
80 {
81         local module="$1"
82
83         _test_loadable_fs_module "${module}"
84         ret=$?
85         case "$ret" in
86         1)
87                 _notrun "${module}: must be a module."
88                 ;;
89         2)
90                 _notrun "${module}: module could not be unloaded"
91                 ;;
92         3)
93                 _notrun "${module}: module reload failed"
94                 ;;
95         esac
96 }
97
98 # Print the value of a filesystem module parameter
99 # at /sys/module/$FSTYP/parameters/$PARAM
100 #
101 # Usage example (FSTYP=overlay):
102 #   _get_fs_module_param index
103 _get_fs_module_param()
104 {
105         cat /sys/module/${FSTYP}/parameters/${1} 2>/dev/null
106 }
107
108 # checks the refcount and returns 0 if we can safely remove the module. rmmod
109 # does this check for us, but we can use this to also iterate checking for this
110 # refcount before we even try to remove the module. This is useful when using
111 # debug test modules which take a while to quiesce.
112 _patient_rmmod_check_refcnt()
113 {
114         local module=$1
115         local refcnt=0
116
117         if [[ -f /sys/module/$module/refcnt ]]; then
118                 refcnt=$(cat /sys/module/$module/refcnt 2>/dev/null)
119                 if [[ $? -ne 0 || $refcnt -eq 0 ]]; then
120                         return 0
121                 fi
122                 return 1
123         fi
124         return 0
125 }
126
127 # Patiently tries to wait to remove a module by ensuring first
128 # the refcnt is 0 and then trying to persistently remove the module within
129 # the time allowed. The timeout is configurable per test, just set
130 # MODPROBE_PATIENT_RM_TIMEOUT_SECONDS prior to including this file.
131 # If you want this to try forever just set MODPROBE_PATIENT_RM_TIMEOUT_SECONDS
132 # to the special value of "forever". This applies to both cases where kmod
133 # supports the patient module remover (modrobe -p) and where it does not.
134 #
135 # If your version of kmod supports modprobe -p, we instead use that
136 # instead. Otherwise we have to implement a patient module remover
137 # ourselves.
138 _patient_rmmod()
139 {
140         local module=$1
141         local max_tries_max=$MODPROBE_PATIENT_RM_TIMEOUT_SECONDS
142         local max_tries=0
143         local mod_ret=0
144         local refcnt_is_zero=0
145
146         if [[ ! -z $MODPROBE_REMOVE_PATIENT ]]; then
147                 $MODPROBE_REMOVE_PATIENT $module
148                 mod_ret=$?
149                 if [[ $mod_ret -ne 0 ]]; then
150                         echo "kmod patient module removal for $module timed out waiting for refcnt to become 0 using timeout of $max_tries_max returned $mod_ret"
151                 fi
152                 return $mod_ret
153         fi
154
155         max_tries=$max_tries_max
156
157         # We must use a string check as otherwise if max_tries is set to
158         # "forever" and we don't use a string check we can end up skipping
159         # entering this loop.
160         while [[ "$max_tries" != "0" ]]; do
161                 _patient_rmmod_check_refcnt $module
162                 if [[ $? -eq 0 ]]; then
163                         refcnt_is_zero=1
164                         break
165                 fi
166                 sleep 1
167                 if [[ "$max_tries" == "forever" ]]; then
168                         continue
169                 fi
170                 let max_tries=$max_tries-1
171         done
172
173         if [[ $refcnt_is_zero -ne 1 ]]; then
174                 echo "custom patient module removal for $module timed out waiting for refcnt to become 0 using timeout of $max_tries_max"
175                 return -1
176         fi
177
178         # If we ran out of time but our refcnt check confirms we had
179         # a refcnt of 0, just try to remove the module once.
180         if [[ "$max_tries" == "0" ]]; then
181                 modprobe -r $module
182                 return $?
183         fi
184
185         # If we have extra time left. Use the time left to now try to
186         # persistently remove the module. We do this because although through
187         # the above we found refcnt to be 0, removal can still fail since
188         # userspace can always race to bump the refcnt. An example is any
189         # blkdev_open() calls against a block device. These issues have been
190         # tracked and documented in the following bug reports, which justifies
191         # our need to do this in userspace:
192         # https://bugzilla.kernel.org/show_bug.cgi?id=212337
193         # https://bugzilla.kernel.org/show_bug.cgi?id=214015
194         while [[ $max_tries != 0 ]]; do
195                 if [[ -d /sys/module/$module ]]; then
196                         modprobe -r $module 2> /dev/null
197                         mod_ret=$?
198                         if [[ $mod_ret == 0 ]]; then
199                                 break;
200                         fi
201                         sleep 1
202                         if [[ "$max_tries" == "forever" ]]; then
203                                 continue
204                         fi
205                         let max_tries=$max_tries-1
206                 else
207                         break
208                 fi
209         done
210
211         if [[ $mod_ret -ne 0 ]]; then
212                 echo "custom patient module removal for $module timed out trying to remove $module using timeout of $max_tries_max last try returned $mod_ret"
213         fi
214
215         return $mod_ret
216 }