xref: /openbmc/linux/scripts/gfp-translate (revision 608e8e66a154cbc3d591a59dcebfd9cbc9e3431a)
1*608e8e66SMel Gorman#!/bin/bash
2*608e8e66SMel Gorman# Translate the bits making up a GFP mask
3*608e8e66SMel Gorman# (c) 2009, Mel Gorman <mel@csn.ul.ie>
4*608e8e66SMel Gorman# Licensed under the terms of the GNU GPL License version 2
5*608e8e66SMel GormanSOURCE=
6*608e8e66SMel GormanGFPMASK=none
7*608e8e66SMel Gorman
8*608e8e66SMel Gorman# Helper function to report failures and exit
9*608e8e66SMel Gormandie() {
10*608e8e66SMel Gorman	echo ERROR: $@
11*608e8e66SMel Gorman	if [ "$TMPFILE" != "" ]; then
12*608e8e66SMel Gorman		rm -f $TMPFILE
13*608e8e66SMel Gorman	fi
14*608e8e66SMel Gorman	exit -1
15*608e8e66SMel Gorman}
16*608e8e66SMel Gorman
17*608e8e66SMel Gormanusage() {
18*608e8e66SMel Gorman	echo "usage: gfp-translate [-h] [ --source DIRECTORY ] gfpmask"
19*608e8e66SMel Gorman	exit 0
20*608e8e66SMel Gorman}
21*608e8e66SMel Gorman
22*608e8e66SMel Gorman# Parse command-line arguements
23*608e8e66SMel Gormanwhile [ $# -gt 0 ]; do
24*608e8e66SMel Gorman	case $1 in
25*608e8e66SMel Gorman		--source)
26*608e8e66SMel Gorman			SOURCE=$2
27*608e8e66SMel Gorman			shift 2
28*608e8e66SMel Gorman			;;
29*608e8e66SMel Gorman		-h)
30*608e8e66SMel Gorman			usage
31*608e8e66SMel Gorman			;;
32*608e8e66SMel Gorman		--help)
33*608e8e66SMel Gorman			usage
34*608e8e66SMel Gorman			;;
35*608e8e66SMel Gorman		*)
36*608e8e66SMel Gorman			GFPMASK=$1
37*608e8e66SMel Gorman			shift
38*608e8e66SMel Gorman			;;
39*608e8e66SMel Gorman	esac
40*608e8e66SMel Gormandone
41*608e8e66SMel Gorman
42*608e8e66SMel Gorman# Guess the kernel source directory if it's not set. Preference is in order of
43*608e8e66SMel Gorman# o current directory
44*608e8e66SMel Gorman# o /usr/src/linux
45*608e8e66SMel Gormanif [ "$SOURCE" = "" ]; then
46*608e8e66SMel Gorman	if [ -r "/usr/src/linux/Makefile" ]; then
47*608e8e66SMel Gorman		SOURCE=/usr/src/linux
48*608e8e66SMel Gorman	fi
49*608e8e66SMel Gorman	if [ -r "`pwd`/Makefile" ]; then
50*608e8e66SMel Gorman		SOURCE=`pwd`
51*608e8e66SMel Gorman	fi
52*608e8e66SMel Gormanfi
53*608e8e66SMel Gorman
54*608e8e66SMel Gorman# Confirm that a source directory exists
55*608e8e66SMel Gormanif [ ! -r "$SOURCE/Makefile" ]; then
56*608e8e66SMel Gorman	die "Could not locate kernel source directory or it is invalid"
57*608e8e66SMel Gormanfi
58*608e8e66SMel Gorman
59*608e8e66SMel Gorman# Confirm that a GFP mask has been specified
60*608e8e66SMel Gormanif [ "$GFPMASK" = "none" ]; then
61*608e8e66SMel Gorman	usage
62*608e8e66SMel Gormanfi
63*608e8e66SMel Gorman
64*608e8e66SMel Gorman# Extract GFP flags from the kernel source
65*608e8e66SMel GormanTMPFILE=`mktemp -t gfptranslate-XXXXXX` || exit 1
66*608e8e66SMel Gormangrep "^#define __GFP" $SOURCE/include/linux/gfp.h | sed -e 's/(__force gfp_t)//' | sed -e 's/u)/)/' | grep -v GFP_BITS | sed -e 's/)\//) \//' > $TMPFILE
67*608e8e66SMel Gorman
68*608e8e66SMel Gorman# Parse the flags
69*608e8e66SMel GormanIFS="
70*608e8e66SMel Gorman"
71*608e8e66SMel Gormanecho Source: $SOURCE
72*608e8e66SMel Gormanecho Parsing: $GFPMASK
73*608e8e66SMel Gormanfor LINE in `cat $TMPFILE`; do
74*608e8e66SMel Gorman	MASK=`echo $LINE | awk '{print $3}'`
75*608e8e66SMel Gorman	if [ $(($GFPMASK&$MASK)) -ne 0 ]; then
76*608e8e66SMel Gorman		echo $LINE
77*608e8e66SMel Gorman	fi
78*608e8e66SMel Gormandone
79*608e8e66SMel Gorman
80*608e8e66SMel Gormanrm -f $TMPFILE
81*608e8e66SMel Gormanexit 0
82