Fix dnode refcount tracking
[zfs.git] / scripts / zfs.sh
blobc131429c919c357485503d9f5e45ec3c53d250c8
1 #!/bin/sh
3 # A simple script to load/unload the ZFS module stack.
6 BASE_DIR=$(dirname "$0")
7 SCRIPT_COMMON=common.sh
8 if [ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]; then
9 . "${BASE_DIR}/${SCRIPT_COMMON}"
10 else
11 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
14 PROG=zfs.sh
15 VERBOSE="no"
16 UNLOAD="no"
17 STACK_TRACER="no"
19 ZED_PIDFILE=${ZED_PIDFILE:-/var/run/zed.pid}
20 LDMOD=${LDMOD:-/sbin/modprobe}
22 KMOD_ZLIB_DEFLATE=${KMOD_ZLIB_DEFLATE:-zlib_deflate}
23 KMOD_ZLIB_INFLATE=${KMOD_ZLIB_INFLATE:-zlib_inflate}
24 KMOD_SPL=${KMOD_SPL:-spl}
25 KMOD_ZAVL=${KMOD_ZAVL:-zavl}
26 KMOD_ZNVPAIR=${KMOD_ZNVPAIR:-znvpair}
27 KMOD_ZUNICODE=${KMOD_ZUNICODE:-zunicode}
28 KMOD_ZCOMMON=${KMOD_ZCOMMON:-zcommon}
29 KMOD_ZLUA=${KMOD_ZLUA:-zlua}
30 KMOD_ICP=${KMOD_ICP:-icp}
31 KMOD_ZFS=${KMOD_ZFS:-zfs}
32 KMOD_FREEBSD=${KMOD_FREEBSD:-openzfs}
33 KMOD_ZZSTD=${KMOD_ZZSTD:-zzstd}
36 usage() {
37 cat << EOF
38 USAGE:
39 $0 [hvudS] [module-options]
41 DESCRIPTION:
42 Load/unload the ZFS module stack.
44 OPTIONS:
45 -h Show this message
46 -v Verbose
47 -u Unload modules
48 -S Enable kernel stack tracer
49 EOF
52 while getopts 'hvuS' OPTION; do
53 case $OPTION in
55 usage
56 exit 1
59 VERBOSE="yes"
62 UNLOAD="yes"
65 STACK_TRACER="yes"
68 usage
69 exit
71 esac
72 done
74 kill_zed() {
75 if [ -f "$ZED_PIDFILE" ]; then
76 PID=$(cat "$ZED_PIDFILE")
77 kill "$PID"
81 check_modules_linux() {
82 LOADED_MODULES=""
83 MISSING_MODULES=""
85 for KMOD in $KMOD_SPL $KMOD_ZAVL $KMOD_ZNVPAIR $KMOD_ZUNICODE $KMOD_ZCOMMON \
86 $KMOD_ZLUA $KMOD_ZZSTD $KMOD_ICP $KMOD_ZFS; do
87 NAME=$(basename "$KMOD" .ko)
89 if lsmod | grep -E -q "^${NAME}"; then
90 LOADED_MODULES="$LOADED_MODULES\t$NAME\n"
93 if ! modinfo "$KMOD" >/dev/null 2>&1; then
94 MISSING_MODULES="$MISSING_MODULES\t${KMOD}\n"
96 done
98 if [ -n "$LOADED_MODULES" ]; then
99 printf "Unload the kernel modules by running '%s -u':\n" "$PROG"
100 printf "%b" "$LOADED_MODULES"
101 exit 1
104 if [ -n "$MISSING_MODULES" ]; then
105 printf "The following kernel modules can not be found:\n"
106 printf "%b" "$MISSING_MODULES"
107 exit 1
110 return 0
113 load_module_linux() {
114 KMOD=$1
116 FILE=$(modinfo "$KMOD" | awk '/^filename:/ {print $2}')
117 VERSION=$(modinfo "$KMOD" | awk '/^version:/ {print $2}')
119 if [ "$VERBOSE" = "yes" ]; then
120 echo "Loading: $FILE ($VERSION)"
123 $LDMOD "$KMOD" >/dev/null 2>&1
124 # shellcheck disable=SC2181
125 if [ $? -ne 0 ]; then
126 echo "Failed to load $KMOD"
127 return 1
130 return 0
133 load_modules_freebsd() {
134 kldload "$KMOD_FREEBSD" || return 1
136 if [ "$VERBOSE" = "yes" ]; then
137 echo "Successfully loaded ZFS module stack"
140 return 0
143 load_modules_linux() {
144 mkdir -p /etc/zfs
146 if modinfo "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1; then
147 modprobe "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1
150 if modinfo "$KMOD_ZLIB_INFLATE">/dev/null 2>&1; then
151 modprobe "$KMOD_ZLIB_INFLATE" >/dev/null 2>&1
154 for KMOD in $KMOD_SPL $KMOD_ZAVL $KMOD_ZNVPAIR \
155 $KMOD_ZUNICODE $KMOD_ZCOMMON $KMOD_ZLUA $KMOD_ZZSTD \
156 $KMOD_ICP $KMOD_ZFS; do
157 load_module_linux "$KMOD" || return 1
158 done
160 if [ "$VERBOSE" = "yes" ]; then
161 echo "Successfully loaded ZFS module stack"
164 return 0
167 unload_module_linux() {
168 KMOD=$1
170 NAME=$(basename "$KMOD" .ko)
171 FILE=$(modinfo "$KMOD" | awk '/^filename:/ {print $2}')
172 VERSION=$(modinfo "$KMOD" | awk '/^version:/ {print $2}')
174 if [ "$VERBOSE" = "yes" ]; then
175 echo "Unloading: $KMOD ($VERSION)"
178 rmmod "$NAME" || echo "Failed to unload $NAME"
180 return 0
183 unload_modules_freebsd() {
184 kldunload "$KMOD_FREEBSD" || echo "Failed to unload $KMOD_FREEBSD"
186 if [ "$VERBOSE" = "yes" ]; then
187 echo "Successfully unloaded ZFS module stack"
190 return 0
193 unload_modules_linux() {
194 for KMOD in $KMOD_ZFS $KMOD_ICP $KMOD_ZZSTD $KMOD_ZLUA $KMOD_ZCOMMON \
195 $KMOD_ZUNICODE $KMOD_ZNVPAIR $KMOD_ZAVL $KMOD_SPL; do
196 NAME=$(basename "$KMOD" .ko)
197 USE_COUNT=$(lsmod | grep -E "^${NAME} " | awk '{print $3}')
199 if [ "$USE_COUNT" = "0" ] ; then
200 unload_module_linux "$KMOD" || return 1
201 else
202 echo "Module ${NAME} is still in use!"
203 return 1
205 done
207 if modinfo "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1; then
208 modprobe -r "$KMOD_ZLIB_DEFLATE" >/dev/null 2>&1
211 if modinfo "$KMOD_ZLIB_INFLATE">/dev/null 2>&1; then
212 modprobe -r "$KMOD_ZLIB_INFLATE" >/dev/null 2>&1
215 if [ "$VERBOSE" = "yes" ]; then
216 echo "Successfully unloaded ZFS module stack"
219 return 0
222 stack_clear_linux() {
223 STACK_MAX_SIZE=/sys/kernel/debug/tracing/stack_max_size
224 STACK_TRACER_ENABLED=/proc/sys/kernel/stack_tracer_enabled
226 if [ "$STACK_TRACER" = "yes" ] && [ -e "$STACK_MAX_SIZE" ]; then
227 echo 1 >"$STACK_TRACER_ENABLED"
228 echo 0 >"$STACK_MAX_SIZE"
232 stack_check_linux() {
233 STACK_MAX_SIZE=/sys/kernel/debug/tracing/stack_max_size
234 STACK_TRACE=/sys/kernel/debug/tracing/stack_trace
235 STACK_LIMIT=15362
237 if [ -e "$STACK_MAX_SIZE" ]; then
238 STACK_SIZE=$(cat "$STACK_MAX_SIZE")
240 if [ "$STACK_SIZE" -ge "$STACK_LIMIT" ]; then
241 echo
242 echo "Warning: max stack size $STACK_SIZE bytes"
243 cat "$STACK_TRACE"
248 if [ "$(id -u)" != 0 ]; then
249 echo "Must run as root"
250 exit 1
253 UNAME=$(uname -s)
255 if [ "$UNLOAD" = "yes" ]; then
256 kill_zed
257 umount -t zfs -a
258 case $UNAME in
259 FreeBSD)
260 unload_modules_freebsd
262 Linux)
263 stack_check_linux
264 unload_modules_linux
266 esac
267 else
268 case $UNAME in
269 FreeBSD)
270 load_modules_freebsd
272 Linux)
273 stack_clear_linux
274 check_modules_linux
275 load_modules_linux "$@"
276 udevadm trigger
277 udevadm settle
279 esac
282 exit 0