Whamcloud - gitweb
LU-13004 ptlrpc: Allow BULK_BUF_KIOV to accept a kvec
[fs/lustre-release.git] / lustre / scripts / lustre_rmmod
1 #!/bin/bash
2 #
3 # Takes a list of modules and unloads them and all dependent modules.
4 # If a module cannot be unloaded (e.g. it's in use), an error is
5 # returned.
6 ###############################################################################
7
8 # Unload all modules dependent on $1 (exclude removal of $1)
9 unload_dep_modules_exclusive() {
10         local MODULE=$1
11         local DEPS="$(lsmod | awk '($1 == "'$MODULE'") { print $4 }')"
12         for SUBMOD in $(echo $DEPS | tr ',' ' '); do
13                 unload_dep_modules_inclusive $SUBMOD || return 1
14         done
15         return 0
16 }
17
18 # Unload all modules dependent on $1 (include removal of $1)
19 unload_dep_modules_inclusive() {
20         local MODULE=$1
21
22         # if $MODULE not loaded, return 0
23         lsmod | egrep -q "^\<$MODULE\>" || return 0
24         unload_dep_modules_exclusive $MODULE || return 1
25         rmmod $MODULE || return 1
26         return 0
27 }
28
29 modules="$@"
30
31 # To maintain backwards compatibility, ldiskfs and libcfs must be
32 # unloaded if no parameters are given, or if only the ldiskfs parameter
33 # is given. It's ugly, but is needed to emulate the prior functionality
34 if [ -z "$modules" ] || [ "$modules" = "ldiskfs" ]; then
35         modules="ptlrpc lnet_selftest ldiskfs libcfs"
36 fi
37
38 if [ -f /sys/kernel/debug/kmemleak ] ; then
39         cat /proc/modules >/tmp/kmemleak-modules-list.txt
40         echo scan > /sys/kernel/debug/kmemleak
41         cat /sys/kernel/debug/kmemleak > /tmp/kmemleak-before-unload.txt
42         test -s /tmp/kmemleak-before-unload.txt && logger -t leak-pre -f /tmp/kmemleak-before-unload.txt
43         rm /tmp/kmemleak-before-unload.txt
44         # Clear everything here so that only new leaks show up
45         # after module unload
46         echo clear > /sys/kernel/debug/kmemleak
47 fi
48
49 for mod in $modules; do
50         unload_dep_modules_inclusive $mod || exit 1
51 done
52
53 if [ -f /sys/kernel/debug/kmemleak ] ; then
54         echo scan > /sys/kernel/debug/kmemleak
55         cat /sys/kernel/debug/kmemleak > /tmp/kmemleak-after-unload.txt
56         test -s /tmp/kmemleak-after-unload.txt && logger -t leak-mods -f /tmp/kmemleak-modules-list.txt && logger -t leak-post -f /tmp/kmemleak-after-unload.txt
57         rm -f /tmp/kmemleak-after-unload.txt /tmp/kmemleak-modules-list.txt
58 fi
59
60 exit 0