open_by_handle: add filename to error reports
[xfstests-dev.git] / common / module
1 ##/bin/bash
2
3 # Routines for messing around with loadable kernel modules
4 #
5 #-----------------------------------------------------------------------
6 #  Copyright (c) 2017 Oracle.  All Rights Reserved.
7 #  This program is free software; you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; either version 2 of the License, or
10 #  (at your option) any later version.
11 #
12 #  This program is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #  GNU General Public License for more details.
16 #
17 #  You should have received a copy of the GNU General Public License
18 #  along with this program; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
20 #  USA
21 #-----------------------------------------------------------------------
22
23 # Return the module name for this fs.
24 _module_for_fs()
25 {
26         echo "${FSTYP}"
27 }
28
29 # Reload a particular module.  This module MUST NOT be the module that
30 # underlies the filesystem.
31 _reload_module()
32 {
33         local module="$1"
34
35         modprobe -r "${module}" || _fail "${module} unload failed"
36         modprobe "${module}" || _fail "${module} load failed"
37 }
38
39 # Reload the filesystem module.
40 _reload_fs_module()
41 {
42         local module="$1"
43
44         # Unload test fs, try to reload module, remount
45         local had_testfs=""
46         local had_scratchfs=""
47         _check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR && had_testfs="true"
48         _check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT && had_scratchfs="true"
49         test -n "${had_testfs}" && _test_unmount
50         test -n "${had_scratchfs}" && _scratch_unmount
51         _reload_module "${module}"
52         test -n "${had_scratchfs}" && _scratch_mount 2> /dev/null
53         test -n "${had_testfs}" && _test_mount 2> /dev/null
54 }
55
56 # Check that we have a module that can be loaded.  This module MUST NOT
57 # be the module that underlies the filesystem.
58 _require_loadable_module()
59 {
60         local module="$1"
61
62         modinfo "${module}" > /dev/null 2>&1 || _notrun "${module}: must be a module."
63         modprobe -r "${module}" || _notrun "Require ${module} to be unloadable"
64         modprobe "${module}" || _notrun "${module} load failed"
65 }
66
67 # Check that the module for FSTYP can be loaded.
68 _require_loadable_fs_module()
69 {
70         local module="$1"
71
72         modinfo "${module}" > /dev/null 2>&1 || _notrun "${module}: must be a module."
73
74         # Unload test fs, try to reload module, remount
75         local had_testfs=""
76         local had_scratchfs=""
77         _check_mounted_on TEST_DEV $TEST_DEV TEST_DIR $TEST_DIR && had_testfs="true"
78         _check_mounted_on SCRATCH_DEV $SCRATCH_DEV SCRATCH_MNT $SCRATCH_MNT && had_scratchfs="true"
79         test -n "${had_testfs}" && _test_unmount
80         test -n "${had_scratchfs}" && _scratch_unmount
81         local unload_ok=""
82         local load_ok=""
83         modprobe -r "${module}" || unload_ok=0
84         modprobe "${module}" || load_ok=0
85         test -n "${had_scratchfs}" && _scratch_mount 2> /dev/null
86         test -n "${had_testfs}" && _test_mount 2> /dev/null
87         test -z "${unload_ok}" || _notrun "Require module ${module} to be unloadable"
88         test -z "${load_ok}" || _notrun "${module} load failed"
89 }
90
91 # Print the value of a filesystem module parameter
92 # at /sys/module/$FSTYP/parameters/$PARAM
93 #
94 # Usage example (FSTYP=overlay):
95 #   _get_fs_module_param index
96 _get_fs_module_param()
97 {
98         cat /sys/module/${FSTYP}/parameters/${1} 2>/dev/null
99 }