generic: add a test for umount racing mount
[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         modprobe -r "${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         modprobe -r "${module}" || _notrun "Require ${module} to be unloadable"
48         modprobe "${module}" || _notrun "${module} load failed"
49 }
50
51 # Check that the module for FSTYP can be loaded.
52 _require_loadable_fs_module()
53 {
54         local module="$1"
55
56         modinfo "${module}" > /dev/null 2>&1 || _notrun "${module}: must be a module."
57
58         # Unload test fs, try to reload module, remount
59         local had_testfs=""
60         local had_scratchfs=""
61         _check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR && had_testfs="true"
62         _check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT && had_scratchfs="true"
63         test -n "${had_testfs}" && _test_unmount
64         test -n "${had_scratchfs}" && _scratch_unmount
65         local unload_ok=""
66         local load_ok=""
67         modprobe -r "${module}" || unload_ok=0
68         modprobe "${module}" || load_ok=0
69         test -n "${had_scratchfs}" && _scratch_mount 2> /dev/null
70         test -n "${had_testfs}" && _test_mount 2> /dev/null
71         test -z "${unload_ok}" || _notrun "Require module ${module} to be unloadable"
72         test -z "${load_ok}" || _notrun "${module} load failed"
73 }
74
75 # Print the value of a filesystem module parameter
76 # at /sys/module/$FSTYP/parameters/$PARAM
77 #
78 # Usage example (FSTYP=overlay):
79 #   _get_fs_module_param index
80 _get_fs_module_param()
81 {
82         cat /sys/module/${FSTYP}/parameters/${1} 2>/dev/null
83 }