Merge pull request #1615 from palmtenor/lua_biosnoop

Port fixes of biosnoop from Python to Lua
diff --git a/tools/biosnoop.lua b/tools/biosnoop.lua
old mode 100644
new mode 100755
index fac7f3b..0aaebd5
--- a/tools/biosnoop.lua
+++ b/tools/biosnoop.lua
@@ -94,11 +94,21 @@
                        req->rq_disk->disk_name);
     }
 
-    if (req->cmd_flags & REQ_WRITE) {
-        data.rwflag=1;
-    } else {
-        data.rwflag=0;
-    }
+/*
+ * The following deals with a kernel version change (in mainline 4.7, although
+ * it may be backported to earlier kernels) with how block request write flags
+ * are tested. We handle both pre- and post-change versions here. Please avoid
+ * kernel version tests like this as much as possible: they inflate the code,
+ * test, and maintenance burden.
+ */
+#ifdef REQ_WRITE
+    data.rwflag = !!(req->cmd_flags & REQ_WRITE);
+#elif defined(REQ_OP_SHIFT)
+    data.rwflag = !!((req->cmd_flags >> REQ_OP_SHIFT) == REQ_OP_WRITE);
+#else
+    data.rwflag = !!((req->cmd_flags & REQ_OP_MASK) == REQ_OP_WRITE);
+#endif
+
     events.perf_submit(ctx,&data,sizeof(data));
     start.delete(&req);
     infobyreq.delete(&req);