| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# Script to check for inconsistencies between documented mount options |
| 4 |
# and implemented kernel options. |
| 5 |
# Copyright (C) 2018 Aurelien Aptel ([email protected]) |
| 6 |
# |
| 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 3 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, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import os |
| 21 |
import sys |
| 22 |
import re |
| 23 |
import subprocess |
| 24 |
import argparse |
| 25 |
from pprint import pprint as P |
| 26 |
from collections import defaultdict |
| 27 |
|
| 28 |
def extract_canonical_opts(s): |
| 29 |
""" |
| 30 |
Return list of option names present in s. |
| 31 |
e.g "opt1=a|opt2=d" => ["opt1", "opt2"]) |
| 32 |
""" |
| 33 |
opts = s.split("|") |
| 34 |
res = [] |
| 35 |
for o in opts: |
| 36 |
x = o.split("=") |
| 37 |
res.append(x[0]) |
| 38 |
return res |
| 39 |
|
| 40 |
def extract_kernel_opts(fn): |
| 41 |
STATE_BASE = 0 |
| 42 |
STATE_USE = 1 |
| 43 |
|
| 44 |
state = STATE_BASE |
| 45 |
name2enum = {} |
| 46 |
enum2code = defaultdict(lambda: '') |
| 47 |
rx = RX() |
| 48 |
|
| 49 |
with open(fn) as f: |
| 50 |
for s in f.readlines(): |
| 51 |
if state == STATE_BASE: |
| 52 |
if rx.search(r'^\s*fsparam_(.*)\("([^,]+)",\s+([^,]+)(?:,\s+([^,]+))?\)', s): |
| 53 |
fmt = rx.group(1) |
| 54 |
name = rx.group(2) |
| 55 |
name2enum[name] = { 'enum': rx.group(3), 'fmt': fmt } |
| 56 |
elif rx.search(r'^\s*case (Opt_[a-zA-Z0-9_]+)', s): |
| 57 |
current_opt = rx.group(1) |
| 58 |
state = STATE_USE |
| 59 |
|
| 60 |
elif state == STATE_USE: |
| 61 |
enum2code[current_opt] += s |
| 62 |
if rx.search(r'\s*break;', s): |
| 63 |
state = STATE_BASE |
| 64 |
|
| 65 |
return name2enum, enum2code |
| 66 |
|
| 67 |
def chomp(s): |
| 68 |
if s[-1] == '\n': |
| 69 |
return s[:-1] |
| 70 |
return s |
| 71 |
|
| 72 |
def extract_man_opts(fn): |
| 73 |
STATE_EXIT = 0 |
| 74 |
STATE_BASE = 1 |
| 75 |
STATE_OPT = 2 |
| 76 |
|
| 77 |
state = STATE_BASE |
| 78 |
rx = RX() |
| 79 |
opts = {} |
| 80 |
ln = 0 |
| 81 |
|
| 82 |
with open(fn) as f: |
| 83 |
for s in f.readlines(): |
| 84 |
ln += 1 |
| 85 |
|
| 86 |
if state == STATE_EXIT: |
| 87 |
break |
| 88 |
|
| 89 |
elif state == STATE_BASE: |
| 90 |
if rx.search(r'^OPTION', s): |
| 91 |
state = STATE_OPT |
| 92 |
|
| 93 |
elif state == STATE_OPT: |
| 94 |
if rx.search('^[a-z]', s) and len(s) < 50: |
| 95 |
s = chomp(s) |
| 96 |
names = extract_canonical_opts(s) |
| 97 |
for name in names: |
| 98 |
if name not in opts: |
| 99 |
opts[name] = [] |
| 100 |
opts[name].append({'ln':ln, 'fmt':s}) |
| 101 |
elif rx.search(r'^[A-Z]+', s): |
| 102 |
state = STATE_EXIT |
| 103 |
return opts |
| 104 |
|
| 105 |
def format_code(s): |
| 106 |
# remove common indent in the block |
| 107 |
min_indent = None |
| 108 |
for ln in s.split("\n"): |
| 109 |
indent = 0 |
| 110 |
for c in ln: |
| 111 |
if c == '\t': indent += 1 |
| 112 |
else: break |
| 113 |
if min_indent is None: |
| 114 |
min_indent = indent |
| 115 |
elif indent > 0: |
| 116 |
min_indent = min(indent, min_indent) |
| 117 |
out = '' |
| 118 |
lines = s.split("\n") |
| 119 |
if lines[-1].strip() == '': |
| 120 |
lines.pop() |
| 121 |
for ln in lines: |
| 122 |
out += "| %s\n" % ln[min_indent:] |
| 123 |
return out |
| 124 |
|
| 125 |
def sortedset(s): |
| 126 |
return sorted(list(s), key=lambda x: re.sub('^no', '', x)) |
| 127 |
|
| 128 |
def opt_neg(opt): |
| 129 |
if opt.startswith("no"): |
| 130 |
return opt[2:] |
| 131 |
else: |
| 132 |
return "no"+opt |
| 133 |
|
| 134 |
def main(): |
| 135 |
ap = argparse.ArgumentParser(description="Cross-check mount options from cifs.ko/man page") |
| 136 |
ap.add_argument("cfile", help="path to connect.c") |
| 137 |
ap.add_argument("rstfile", help="path to mount.cifs.rst") |
| 138 |
args = ap.parse_args() |
| 139 |
|
| 140 |
name2enum, enum2code = extract_kernel_opts(args.cfile) |
| 141 |
manopts = extract_man_opts(args.rstfile) |
| 142 |
|
| 143 |
kernel_opts_set = set(name2enum.keys()) |
| 144 |
man_opts_set = set(manopts.keys()) |
| 145 |
|
| 146 |
def opt_alias_is_doc(o): |
| 147 |
enum = name2enum[o]['enum'] |
| 148 |
aliases = [] |
| 149 |
for k,v in name2enum.items(): |
| 150 |
if k != o and v['enum'] == enum: |
| 151 |
if opt_is_doc(k): |
| 152 |
return k |
| 153 |
return None |
| 154 |
|
| 155 |
def opt_exists(o): |
| 156 |
return o in name2enum |
| 157 |
|
| 158 |
def opt_is_doc(o): |
| 159 |
return o in manopts |
| 160 |
|
| 161 |
print('DUPLICATED DOC OPTIONS') |
| 162 |
print('======================') |
| 163 |
|
| 164 |
for opt in sortedset(man_opts_set): |
| 165 |
if len(manopts[opt]) > 1: |
| 166 |
lines = ", ".join([str(x['ln']) for x in manopts[opt]]) |
| 167 |
print("OPTION %-20.20s (lines %s)"%(opt, lines)) |
| 168 |
print() |
| 169 |
|
| 170 |
print('UNDOCUMENTED OPTIONS') |
| 171 |
print('====================') |
| 172 |
|
| 173 |
undoc_opts = kernel_opts_set - man_opts_set |
| 174 |
# group opts and their negations together |
| 175 |
for opt in sortedset(undoc_opts): |
| 176 |
fmt = name2enum[opt]['fmt'] |
| 177 |
enum = name2enum[opt]['enum'] |
| 178 |
code = format_code(enum2code[enum]) |
| 179 |
neg = opt_neg(opt) |
| 180 |
|
| 181 |
if enum == 'Opt_ignore': |
| 182 |
print("# skipping %s (Opt_ignore)\n"%opt) |
| 183 |
continue |
| 184 |
|
| 185 |
if opt_exists(neg) and opt_is_doc(neg): |
| 186 |
print("# skipping %s (%s is documented)\n"%(opt, neg)) |
| 187 |
continue |
| 188 |
|
| 189 |
alias = opt_alias_is_doc(opt) |
| 190 |
if alias: |
| 191 |
print("# skipping %s (alias %s is documented)\n"%(opt, alias)) |
| 192 |
continue |
| 193 |
|
| 194 |
print('OPTION %s ("%s" -> %s):\n%s'%(opt, fmt, enum, code)) |
| 195 |
|
| 196 |
print('') |
| 197 |
print('DOCUMENTED BUT NON-EXISTING OPTIONS') |
| 198 |
print('===================================') |
| 199 |
|
| 200 |
unex_opts = man_opts_set - kernel_opts_set |
| 201 |
# group opts and their negations together |
| 202 |
for opt in sortedset(unex_opts): |
| 203 |
man = manopts[opt][0] |
| 204 |
|
| 205 |
# If positive opt exists and it is used, then negative opt does |
| 206 |
# not need to exist |
| 207 |
if opt.startswith('no') and opt[2:] in name2enum: |
| 208 |
print(f'# skipping {opt} ({opt[2:]} exists)') |
| 209 |
continue |
| 210 |
|
| 211 |
print('OPTION %s ("%s") line %d' % (opt, man['fmt'], man['ln'])) |
| 212 |
|
| 213 |
|
| 214 |
print('') |
| 215 |
print('NEGATIVE OPTIONS WITHOUT POSITIVE') |
| 216 |
print('=================================') |
| 217 |
|
| 218 |
for opt in sortedset(kernel_opts_set): |
| 219 |
if not opt.startswith('no'): |
| 220 |
continue |
| 221 |
|
| 222 |
neg = opt[2:] |
| 223 |
if not opt_exists(neg): |
| 224 |
print("OPTION %s exists but not %s"%(opt,neg)) |
| 225 |
|
| 226 |
# little helper to test AND store result at the same time so you can |
| 227 |
# do if/elsif easily instead of nesting them when you need to do |
| 228 |
# captures |
| 229 |
class RX: |
| 230 |
def __init__(self): |
| 231 |
pass |
| 232 |
def search(self, rx, s, flags=0): |
| 233 |
self.r = re.search(rx, s, flags) |
| 234 |
return self.r |
| 235 |
def group(self, n): |
| 236 |
return self.r.group(n) |
| 237 |
|
| 238 |
if __name__ == '__main__': |
| 239 |
main() |