Blame SOURCES/0004-checkopts-add-python-script-to-cross-check-mount-opt.patch

75f41f
From 0c846b0201e7d74186a10facfed222596a0d1d50 Mon Sep 17 00:00:00 2001
75f41f
From: =?UTF-8?q?Aur=C3=A9lien=20Aptel?= <aaptel@suse.com>
75f41f
Date: Tue, 10 Jul 2018 17:50:42 +0200
75f41f
Subject: [PATCH 04/36] checkopts: add python script to cross check mount
75f41f
 options
75f41f
75f41f
Signed-off-by: Aurelien Aptel <aaptel@suse.com>
75f41f
(cherry picked from commit 97209a56d13b8736579a58cccf00d2da4e4a0e5a)
75f41f
Signed-off-by: Sachin Prabhu <sprabhu@redhat.com>
75f41f
---
75f41f
 checkopts | 240 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75f41f
 1 file changed, 240 insertions(+)
75f41f
 create mode 100755 checkopts
75f41f
75f41f
diff --git a/checkopts b/checkopts
75f41f
new file mode 100755
75f41f
index 0000000..26ca271
75f41f
--- /dev/null
75f41f
+++ b/checkopts
75f41f
@@ -0,0 +1,240 @@
75f41f
+#!/usr/bin/env python3
75f41f
+#
75f41f
+# Script to check for inconsistencies between documented mount options
75f41f
+# and implemented kernel options.
75f41f
+# Copyright (C) 2018 Aurelien Aptel (aaptel@suse.com)
75f41f
+#
75f41f
+# This program is free software; you can redistribute it and/or modify
75f41f
+# it under the terms of the GNU General Public License as published by
75f41f
+# the Free Software Foundation; either version 3 of the License, or
75f41f
+# (at your option) any later version.
75f41f
+#
75f41f
+# This program is distributed in the hope that it will be useful,
75f41f
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
75f41f
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
75f41f
+# GNU General Public License for more details.
75f41f
+#
75f41f
+# You should have received a copy of the GNU General Public License
75f41f
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
75f41f
+
75f41f
+import os
75f41f
+import sys
75f41f
+import re
75f41f
+import subprocess
75f41f
+import argparse
75f41f
+from pprint import pprint as P
75f41f
+
75f41f
+def extract_canonical_opts(s):
75f41f
+    """
75f41f
+    Return list of option names present in s.
75f41f
+    e.g "opt1=a|opt2=d" => ["opt1", "opt2"])
75f41f
+    """
75f41f
+    opts = s.split("|")
75f41f
+    res = []
75f41f
+    for o in opts:
75f41f
+        x = o.split("=")
75f41f
+        res.append(x[0])
75f41f
+    return res
75f41f
+
75f41f
+def extract_kernel_opts(fn):
75f41f
+    STATE_BASE = 0
75f41f
+    STATE_DEF = 1
75f41f
+    STATE_USE = 2
75f41f
+    STATE_EXIT = 3
75f41f
+
75f41f
+    state = STATE_BASE
75f41f
+    fmt2enum = {}
75f41f
+    enum2code = {}
75f41f
+    code = ''
75f41f
+    current_opt = ''
75f41f
+    rx = RX()
75f41f
+
75f41f
+    def code_add(s):
75f41f
+        if current_opt != '':
75f41f
+            if current_opt not in enum2code:
75f41f
+                enum2code[current_opt] = ''
75f41f
+            enum2code[current_opt] += s
75f41f
+
75f41f
+    with open(fn) as f:
75f41f
+        for s in f.readlines():
75f41f
+            if state == STATE_EXIT:
75f41f
+                break
75f41f
+
75f41f
+            elif state == STATE_BASE:
75f41f
+                if rx.search(r'cifs_mount_option_tokens.*\{', s):
75f41f
+                    state = STATE_DEF
75f41f
+                elif rx.search(r'^cifs_parse_mount_options', s):
75f41f
+                    state = STATE_USE
75f41f
+
75f41f
+            elif state == STATE_DEF:
75f41f
+                if rx.search(r'(Opt_[a-zA-Z0-9_]+)\s*,\s*"([^"]+)"', s):
75f41f
+                    fmt = rx.group(2)
75f41f
+                    opts = extract_canonical_opts(fmt)
75f41f
+                    assert(len(opts) == 1)
75f41f
+                    name = opts[0]
75f41f
+                    fmt2enum[name] = {'enum':rx.group(1), 'fmt':fmt}
75f41f
+                elif rx.search(r'^};', s):
75f41f
+                    state = STATE_BASE
75f41f
+
75f41f
+            elif state == STATE_USE:
75f41f
+                if rx.search(r'^\s*case (Opt_[a-zA-Z0-9_]+)', s):
75f41f
+                    current_opt = rx.group(1)
75f41f
+                elif current_opt != '' and rx.search(r'^\s*default:', s):
75f41f
+                    state = STATE_EXIT
75f41f
+                else:
75f41f
+                    code_add(s)
75f41f
+    return fmt2enum, enum2code
75f41f
+
75f41f
+def chomp(s):
75f41f
+    if s[-1] == '\n':
75f41f
+        return s[:-1]
75f41f
+    return s
75f41f
+
75f41f
+def extract_man_opts(fn):
75f41f
+    STATE_EXIT = 0
75f41f
+    STATE_BASE = 1
75f41f
+    STATE_OPT = 2
75f41f
+
75f41f
+    state = STATE_BASE
75f41f
+    rx = RX()
75f41f
+    opts = {}
75f41f
+
75f41f
+    with open(fn) as f:
75f41f
+        for s in f.readlines():
75f41f
+            if state == STATE_EXIT:
75f41f
+                break
75f41f
+
75f41f
+            elif state == STATE_BASE:
75f41f
+                if rx.search(r'^OPTION', s):
75f41f
+                    state = STATE_OPT
75f41f
+
75f41f
+            elif state == STATE_OPT:
75f41f
+                if rx.search('^[a-z]', s) and len(s) < 50:
75f41f
+                    s = chomp(s)
75f41f
+                    names = extract_canonical_opts(s)
75f41f
+                    for name in names:
75f41f
+                        opts[name] = s
75f41f
+                elif rx.search(r'^[A-Z]+', s):
75f41f
+                    state = STATE_EXIT
75f41f
+    return opts
75f41f
+
75f41f
+def format_code(s):
75f41f
+    # remove common indent in the block
75f41f
+    min_indent = None
75f41f
+    for ln in s.split("\n"):
75f41f
+        indent = 0
75f41f
+        for c in ln:
75f41f
+            if c == '\t': indent += 1
75f41f
+            else: break
75f41f
+        if min_indent is None:
75f41f
+            min_indent = indent
75f41f
+        elif indent > 0:
75f41f
+            min_indent = min(indent, min_indent)
75f41f
+    out = ''
75f41f
+    lines = s.split("\n")
75f41f
+    if lines[-1].strip() == '':
75f41f
+        lines.pop()
75f41f
+    for ln in lines:
75f41f
+        out += "| %s\n" % ln[min_indent:]
75f41f
+    return out
75f41f
+
75f41f
+def sortedset(s):
75f41f
+    return sorted(list(s), key=lambda x: re.sub('^no', '', x))
75f41f
+
75f41f
+def opt_neg(opt):
75f41f
+    if opt.startswith("no"):
75f41f
+        return opt[2:]
75f41f
+    else:
75f41f
+        return "no"+opt
75f41f
+
75f41f
+def main():
75f41f
+    ap = argparse.ArgumentParser(description="Cross-check mount options from cifs.ko/man page")
75f41f
+    ap.add_argument("cfile", help="path to connect.c")
75f41f
+    ap.add_argument("rstfile", help="path to mount.cifs.rst")
75f41f
+    args = ap.parse_args()
75f41f
+
75f41f
+    fmt2enum, enum2code = extract_kernel_opts(args.cfile)
75f41f
+    manopts = extract_man_opts(args.rstfile)
75f41f
+
75f41f
+    kernel_opts_set = set(fmt2enum.keys())
75f41f
+    man_opts_set = set(manopts.keys())
75f41f
+
75f41f
+    def opt_alias_is_doc(o):
75f41f
+        enum = fmt2enum[o]['enum']
75f41f
+        aliases = []
75f41f
+        for k,v in fmt2enum.items():
75f41f
+            if k != o and v['enum'] == enum:
75f41f
+                if opt_is_doc(k):
75f41f
+                    return k
75f41f
+        return None
75f41f
+
75f41f
+    def opt_exists(o):
75f41f
+        return o in fmt2enum
75f41f
+
75f41f
+    def opt_is_doc(o):
75f41f
+        return o in manopts
75f41f
+
75f41f
+
75f41f
+    print('UNDOCUMENTED OPTIONS')
75f41f
+    print('====================')
75f41f
+
75f41f
+    undoc_opts = kernel_opts_set - man_opts_set
75f41f
+    # group opts and their negations together
75f41f
+    for opt in sortedset(undoc_opts):
75f41f
+        fmt = fmt2enum[opt]['fmt']
75f41f
+        enum = fmt2enum[opt]['enum']
75f41f
+        code = format_code(enum2code[enum])
75f41f
+        neg = opt_neg(opt)
75f41f
+
75f41f
+        if enum == 'Opt_ignore':
75f41f
+            print("# skipping %s (Opt_ignore)\n"%opt)
75f41f
+            continue
75f41f
+
75f41f
+        if opt_exists(neg) and opt_is_doc(neg):
75f41f
+            print("# skipping %s (%s is documented)\n"%(opt, neg))
75f41f
+            continue
75f41f
+
75f41f
+        alias = opt_alias_is_doc(opt)
75f41f
+        if alias:
75f41f
+            print("# skipping %s (alias %s is documented)\n"%(opt, alias))
75f41f
+            continue
75f41f
+
75f41f
+        print('OPTION %s ("%s" -> %s):\n%s'%(opt, fmt, enum, code))
75f41f
+
75f41f
+    print('')
75f41f
+    print('DOCUMENTED BUT NON-EXISTING OPTIONS')
75f41f
+    print('===================================')
75f41f
+
75f41f
+    unex_opts = man_opts_set - kernel_opts_set
75f41f
+    # group opts and their negations together
75f41f
+    for opt in sortedset(unex_opts):
75f41f
+        fmt = manopts[opt]
75f41f
+        print('OPTION %s ("%s")' % (opt, fmt))
75f41f
+
75f41f
+
75f41f
+    print('')
75f41f
+    print('NEGATIVE OPTIONS WITHOUT POSITIVE')
75f41f
+    print('=================================')
75f41f
+
75f41f
+    for opt in sortedset(kernel_opts_set):
75f41f
+        if not opt.startswith('no'):
75f41f
+            continue
75f41f
+
75f41f
+        neg = opt[2:]
75f41f
+        if not opt_exists(neg):
75f41f
+            print("OPTION %s exists but not %s"%(opt,neg))
75f41f
+
75f41f
+# little helper to test AND store result at the same time so you can
75f41f
+# do if/elsif easily instead of nesting them when you need to do
75f41f
+# captures
75f41f
+class RX:
75f41f
+    def __init__(self):
75f41f
+        pass
75f41f
+    def search(self, rx, s, flags=0):
75f41f
+        self.r = re.search(rx, s, flags)
75f41f
+        return self.r
75f41f
+    def group(self, n):
75f41f
+        return self.r.group(n)
75f41f
+
75f41f
+if __name__ == '__main__':
75f41f
+    main()
75f41f
-- 
75f41f
1.8.3.1
75f41f