Revert "HACKS *.oplock.batch9 ... this is very strange with stat opens and different...
[metze/samba/wip.git] / source4 / torture / smb2 / oplock.c
index d4899439860ad0ea7b6530aa7a1152c7ec02f44e..d39b904cc7cddfa30b642bf08eb2c38155db1206 100644 (file)
@@ -26,6 +26,7 @@
 #include "libcli/smb2/smb2_calls.h"
 #include "libcli/smb_composite/smb_composite.h"
 #include "libcli/resolve/resolve.h"
+#include "libcli/smb/smbXcli_base.h"
 
 #include "lib/cmdline/popt_common.h"
 #include "lib/events/events.h"
@@ -36,6 +37,8 @@
 #include "torture/torture.h"
 #include "torture/smb2/proto.h"
 
+#include "lib/util/sys_rw.h"
+
 #define CHECK_RANGE(v, min, max) do { \
        if ((v) < (min) || (v) > (max)) { \
                torture_result(tctx, TORTURE_FAIL, "(%s): wrong value for %s " \
@@ -281,7 +284,6 @@ static bool open_smb2_connection_no_level2_oplocks(struct torture_context *tctx,
        NTSTATUS status;
        const char *host = torture_setting_string(tctx, "host", NULL);
        const char *share = torture_setting_string(tctx, "share", NULL);
-       struct cli_credentials *credentials = cmdline_credentials;
        struct smbcli_options options;
 
        lpcfg_smbcli_options(tctx->lp_ctx, &options);
@@ -290,7 +292,8 @@ static bool open_smb2_connection_no_level2_oplocks(struct torture_context *tctx,
        status = smb2_connect(tctx, host,
                              lpcfg_smb_ports(tctx->lp_ctx), share,
                              lpcfg_resolve_context(tctx->lp_ctx),
-                             credentials, tree, tctx->ev, &options,
+                             popt_get_cmdline_credentials(),
+                             tree, tctx->ev, &options,
                              lpcfg_socket_options(tctx->lp_ctx),
                              lpcfg_gensec_settings(tctx, tctx->lp_ctx));
        if (!NT_STATUS_IS_OK(status)) {
@@ -302,60 +305,6 @@ static bool open_smb2_connection_no_level2_oplocks(struct torture_context *tctx,
        return true;
 }
 
-/*
-   Timer handler function notifies the registering function that time is up
-*/
-static void timeout_cb(struct tevent_context *ev,
-                      struct tevent_timer *te,
-                      struct timeval current_time,
-                      void *private_data)
-{
-       bool *timesup = (bool *)private_data;
-       *timesup = true;
-       return;
-}
-
-/*
-   Wait a short period of time to receive a single oplock break request
-*/
-static void torture_wait_for_oplock_break(struct torture_context *tctx)
-{
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-       struct tevent_timer *te = NULL;
-       struct timeval ne;
-       bool timesup = false;
-       int old_count = break_info.count;
-
-       /* Wait .1 seconds for an oplock break */
-       ne = tevent_timeval_current_ofs(0, 100000);
-
-       if ((te = tevent_add_timer(tctx->ev, tmp_ctx, ne, timeout_cb, &timesup))
-           == NULL)
-       {
-               torture_comment(tctx, "Failed to wait for an oplock break. "
-                                     "test results may not be accurate.");
-               goto done;
-       }
-
-       while (!timesup && break_info.count < old_count + 1) {
-               if (tevent_loop_once(tctx->ev) != 0) {
-                       torture_comment(tctx, "Failed to wait for an oplock "
-                                             "break. test results may not be "
-                                             "accurate.");
-                       goto done;
-               }
-       }
-
-done:
-       /* We don't know if the timed event fired and was freed, we received
-        * our oplock break, or some other event triggered the loop.  Thus,
-        * we create a tmp_ctx to be able to safely free/remove the timed
-        * event in all 3 cases. */
-       talloc_free(tmp_ctx);
-
-       return;
-}
-
 static bool test_smb2_oplock_exclusive1(struct torture_context *tctx,
                                        struct smb2_tree *tree1,
                                        struct smb2_tree *tree2)
@@ -364,7 +313,6 @@ static bool test_smb2_oplock_exclusive1(struct torture_context *tctx,
        NTSTATUS status;
        bool ret = true;
        union smb_open io;
-       union smb_unlink unl;
        struct smb2_handle h1;
        struct smb2_handle h;
 
@@ -411,8 +359,6 @@ static bool test_smb2_oplock_exclusive1(struct torture_context *tctx,
        CHECK_VAL(break_info.failures, 0);
 
        torture_comment(tctx, "unlink it - should also be no break\n");
-       unl.unlink.in.pattern = fname;
-       unl.unlink.in.attrib = 0;
        status = smb2_util_unlink(tree2, fname);
        torture_assert_ntstatus_equal(tctx, status, NT_STATUS_SHARING_VIOLATION,
                                      "Incorrect status");
@@ -435,7 +381,6 @@ static bool test_smb2_oplock_exclusive2(struct torture_context *tctx,
        NTSTATUS status;
        bool ret = true;
        union smb_open io;
-       union smb_unlink unl;
        struct smb2_handle h, h1, h2;
 
        status = torture_smb2_testdir(tree1, BASEDIR, &h);
@@ -490,8 +435,6 @@ static bool test_smb2_oplock_exclusive2(struct torture_context *tctx,
 
        /* now we have 2 level II oplocks... */
        torture_comment(tctx, "try to unlink it - should cause a break\n");
-       unl.unlink.in.pattern = fname;
-       unl.unlink.in.attrib = 0;
        status = smb2_util_unlink(tree2, fname);
        torture_assert_ntstatus_ok(tctx, status, "Error unlinking the file");
        torture_wait_for_oplock_break(tctx);
@@ -833,6 +776,87 @@ static bool test_smb2_oplock_exclusive6(struct torture_context *tctx,
        return ret;
 }
 
+static bool test_smb2_oplock_exclusive9(struct torture_context *tctx,
+                                       struct smb2_tree *tree1,
+                                       struct smb2_tree *tree2)
+{
+       const char *fname = BASEDIR "\\test_exclusive9.dat";
+       NTSTATUS status;
+       bool ret = true;
+       union smb_open io;
+       struct smb2_handle h1, h2;
+       int i;
+
+       struct {
+               uint32_t create_disposition;
+               uint32_t break_level;
+       } levels[] = {
+               { NTCREATEX_DISP_SUPERSEDE, SMB2_OPLOCK_LEVEL_NONE },
+               { NTCREATEX_DISP_OPEN, SMB2_OPLOCK_LEVEL_II },
+               { NTCREATEX_DISP_OVERWRITE_IF, SMB2_OPLOCK_LEVEL_NONE },
+               { NTCREATEX_DISP_OPEN_IF, SMB2_OPLOCK_LEVEL_II },
+       };
+
+
+       status = torture_smb2_testdir(tree1, BASEDIR, &h1);
+       torture_assert_ntstatus_ok(tctx, status, "Error creating directory");
+       smb2_util_close(tree1, h1);
+
+       /* cleanup */
+       smb2_util_unlink(tree1, fname);
+
+       tree1->session->transport->oplock.handler = torture_oplock_handler;
+       tree1->session->transport->oplock.private_data = tree1;
+
+       /*
+         base ntcreatex parms
+       */
+       ZERO_STRUCT(io.smb2);
+       io.generic.level = RAW_OPEN_SMB2;
+       io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       io.smb2.in.alloc_size = 0;
+       io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
+               NTCREATEX_SHARE_ACCESS_WRITE | NTCREATEX_SHARE_ACCESS_DELETE;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       io.smb2.in.create_options = 0;
+       io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       io.smb2.in.security_flags = 0;
+       io.smb2.in.fname = fname;
+
+       for (i=0; i<ARRAY_SIZE(levels); i++) {
+
+               io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
+               io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+
+               status = smb2_create(tree1, tctx, &(io.smb2));
+               torture_assert_ntstatus_ok(tctx, status,
+                                          "Error opening the file");
+               h1 = io.smb2.out.file.handle;
+               CHECK_VAL(io.smb2.out.oplock_level,
+                         SMB2_OPLOCK_LEVEL_EXCLUSIVE);
+
+               ZERO_STRUCT(break_info);
+
+               io.smb2.in.create_disposition = levels[i].create_disposition;
+               status = smb2_create(tree2, tctx, &(io.smb2));
+               torture_assert_ntstatus_ok(tctx, status,
+                                          "Error opening the file");
+               h2 = io.smb2.out.file.handle;
+               CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_II);
+
+               CHECK_VAL(break_info.count, 1);
+               CHECK_VAL(break_info.level, levels[i].break_level);
+               CHECK_VAL(break_info.failures, 0);
+
+               smb2_util_close(tree2, h2);
+               smb2_util_close(tree1, h1);
+       }
+
+       smb2_deltree(tree1, BASEDIR);
+       return ret;
+}
+
 static bool test_smb2_oplock_batch1(struct torture_context *tctx,
                                    struct smb2_tree *tree1,
                                    struct smb2_tree *tree2)
@@ -1467,11 +1491,141 @@ static bool test_smb2_oplock_batch9(struct torture_context *tctx,
                                SEC_FILE_WRITE_ATTRIBUTE |
                                SEC_STD_SYNCHRONIZE;
        io.smb2.in.create_disposition = NTCREATEX_DISP_CREATE;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
        status = smb2_create(tree1, tctx, &(io.smb2));
        torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
        h1 = io.smb2.out.file.handle;
+       CHECK_VAL(io.smb2.out.create_action, FILE_WAS_CREATED);
+       CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
+
+       torture_comment(tctx, "Subsequent normal open should break oplock on "
+                       "attribute only open to level II\n");
+
+       ZERO_STRUCT(break_info);
+
+       io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
+       io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+       io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       status = smb2_create(tree2, tctx, &(io.smb2));
+       torture_assert_ntstatus_ok(tctx, status, "Incorrect status");
+       h2 = io.smb2.out.file.handle;
+       torture_wait_for_oplock_break(tctx);
+       CHECK_VAL(break_info.count, 1);
+       CHECK_VAL(break_info.handle.data[0], h1.data[0]);
+       CHECK_VAL(break_info.failures, 0);
+       CHECK_VAL(break_info.level, SMB2_OPLOCK_LEVEL_II);
+       CHECK_VAL(io.smb2.out.create_action, FILE_WAS_OPENED);
+       CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_II);
+       smb2_util_close(tree2, h2);
+
+       torture_comment(tctx, "third oplocked open should grant level2 without "
+                       "break\n");
+       ZERO_STRUCT(break_info);
+
+       tree2->session->transport->oplock.handler = torture_oplock_handler;
+       tree2->session->transport->oplock.private_data = tree2;
+
+       io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
+       io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+       io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
+       status = smb2_create(tree2, tctx, &(io.smb2));
+       torture_assert_ntstatus_ok(tctx, status, "Incorrect status");
+       h2 = io.smb2.out.file.handle;
+       torture_wait_for_oplock_break(tctx);
+       CHECK_VAL(break_info.count, 0);
+       CHECK_VAL(break_info.failures, 0);
+       CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_II);
+
+       ZERO_STRUCT(break_info);
+
+       torture_comment(tctx, "write should trigger a break to none on both\n");
+       tree1->session->transport->oplock.handler =
+           torture_oplock_handler_level2_to_none;
+       tree2->session->transport->oplock.handler =
+           torture_oplock_handler_level2_to_none;
+       smb2_util_write(tree2, h2, &c, 0, 1);
+
+       /* We expect two breaks */
+       torture_wait_for_oplock_break(tctx);
+       torture_wait_for_oplock_break(tctx);
+
+       CHECK_VAL(break_info.count, 2);
+       CHECK_VAL(break_info.level, 0);
+       CHECK_VAL(break_info.failures, 0);
+
+       smb2_util_close(tree1, h1);
+       smb2_util_close(tree2, h2);
+       smb2_util_close(tree1, h);
+
+       smb2_deltree(tree1, BASEDIR);
+       return ret;
+}
+
+static bool test_smb2_oplock_batch9a(struct torture_context *tctx,
+                                    struct smb2_tree *tree1,
+                                    struct smb2_tree *tree2)
+{
+       const char *fname = BASEDIR "\\test_batch9a.dat";
+       NTSTATUS status;
+       bool ret = true;
+       union smb_open io;
+       struct smb2_handle h, h1, h2, h3;
+       char c = 0;
+
+       status = torture_smb2_testdir(tree1, BASEDIR, &h);
+       torture_assert_ntstatus_ok(tctx, status, "Error creating directory");
+
+       /* cleanup */
+       smb2_util_unlink(tree1, fname);
+
+       tree1->session->transport->oplock.handler = torture_oplock_handler;
+       tree1->session->transport->oplock.private_data = tree1;
+
+       /*
+         base ntcreatex parms
+       */
+       ZERO_STRUCT(io.smb2);
+       io.generic.level = RAW_OPEN_SMB2;
+       io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       io.smb2.in.alloc_size = 0;
+       io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       io.smb2.in.create_options = 0;
+       io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       io.smb2.in.security_flags = 0;
+       io.smb2.in.fname = fname;
+
+       torture_comment(tctx, "BATCH9: open with attributes only can create "
+                       "file\n");
+
+       io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
+       io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+       io.smb2.in.desired_access = SEC_FILE_READ_ATTRIBUTE |
+                               SEC_FILE_WRITE_ATTRIBUTE |
+                               SEC_STD_SYNCHRONIZE;
+       status = smb2_create(tree1, tctx, &(io.smb2));
+       torture_assert_ntstatus_ok(tctx, status, "Error creating the file");
+       h1 = io.smb2.out.file.handle;
+       CHECK_VAL(io.smb2.out.create_action, FILE_WAS_CREATED);
        CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
 
+       torture_comment(tctx, "Subsequent attributes open should not break\n");
+
+       ZERO_STRUCT(break_info);
+
+       status = smb2_create(tree2, tctx, &(io.smb2));
+       torture_assert_ntstatus_ok(tctx, status, "Incorrect status");
+       h3 = io.smb2.out.file.handle;
+       torture_wait_for_oplock_break(tctx);
+       CHECK_VAL(break_info.count, 0);
+       CHECK_VAL(io.smb2.out.create_action, FILE_WAS_OPENED);
+       CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+       smb2_util_close(tree2, h3);
+
        torture_comment(tctx, "Subsequent normal open should break oplock on "
                        "attribute only open to level II\n");
 
@@ -1536,6 +1690,7 @@ static bool test_smb2_oplock_batch9(struct torture_context *tctx,
        return ret;
 }
 
+
 static bool test_smb2_oplock_batch10(struct torture_context *tctx,
                                     struct smb2_tree *tree1,
                                     struct smb2_tree *tree2)
@@ -1609,7 +1764,7 @@ static bool test_smb2_oplock_batch10(struct torture_context *tctx,
        {
                struct smb2_write wr;
                DATA_BLOB data;
-               data = data_blob_talloc(tree1, NULL, UINT16_MAX);
+               data = data_blob_talloc_zero(tree1, UINT16_MAX);
                data.data[0] = (const uint8_t)'x';
                ZERO_STRUCT(wr);
                wr.in.file.handle = h1;
@@ -2422,10 +2577,6 @@ static bool test_smb2_oplock_batch22(struct torture_context *tctx,
        int timeout = torture_setting_int(tctx, "oplocktimeout", 30);
        int te;
 
-       if (torture_setting_bool(tctx, "samba3", false)) {
-               torture_skip(tctx, "BATCH22 disabled against samba3\n");
-       }
-
        status = torture_smb2_testdir(tree1, BASEDIR, &h);
        torture_assert_ntstatus_ok(tctx, status, "Error creating directory");
 
@@ -2730,6 +2881,85 @@ static bool test_smb2_oplock_batch25(struct torture_context *tctx,
        return ret;
 }
 
+static bool test_smb2_oplock_batch26(struct torture_context *tctx,
+                                        struct smb2_tree *tree1)
+{
+
+        NTSTATUS status;
+        bool ret = true;
+        union smb_open io;
+        struct smb2_handle h, h1, h2, h3;
+        const char *fname_base = BASEDIR "\\test_oplock.txt";
+        const char *stream = "Stream One:$DATA";
+        const char *fname_stream;
+
+        status = torture_smb2_testdir(tree1, BASEDIR, &h);
+        torture_assert_ntstatus_ok(tctx, status, "Error creating directory");
+
+        tree1->session->transport->oplock.handler = torture_oplock_handler;
+        tree1->session->transport->oplock.private_data = tree1;
+
+        fname_stream = talloc_asprintf(tctx, "%s:%s", fname_base, stream);
+
+        /*
+          base ntcreatex parms
+        */
+        ZERO_STRUCT(io.smb2);
+        io.generic.level = RAW_OPEN_SMB2;
+        io.smb2.in.desired_access = 0x120089;
+        io.smb2.in.alloc_size = 0;
+        io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+        io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_DELETE |
+                                  NTCREATEX_SHARE_ACCESS_WRITE;
+        io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+        io.smb2.in.create_options = 0;
+        io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+        io.smb2.in.security_flags = 0;
+        io.smb2.in.fname = fname_base;
+
+        /*
+          Open base file with a batch oplock.
+        */
+        torture_comment(tctx, "Open the base file with batch oplock\n");
+        io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
+        io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+
+        status = smb2_create(tree1, tctx, &(io.smb2));
+        torture_assert_ntstatus_ok(tctx, status, "Error opening base file");
+        h1 = io.smb2.out.file.handle;
+        CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
+
+        torture_comment(tctx, "Got batch oplock on base file\n");
+
+        torture_comment(tctx, "Opening stream file with batch oplock..\n");
+
+        io.smb2.in.fname = fname_stream;
+
+        status = smb2_create(tree1, tctx, &(io.smb2));
+        torture_assert_ntstatus_ok(tctx, status, "Error opening stream file");
+        h2 = io.smb2.out.file.handle;
+        CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
+
+        torture_comment(tctx, "Got batch oplock on stream file\n");
+
+        torture_comment(tctx, "Open base file again with batch oplock\n");
+
+        io.smb2.in.fname = fname_base;
+
+        status = smb2_create(tree1, tctx, &(io.smb2));
+        torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
+        h3 = io.smb2.out.file.handle;
+        CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_II);
+
+        smb2_util_close(tree1, h1);
+        smb2_util_close(tree1, h2);
+        smb2_util_close(tree1, h3);
+        smb2_util_close(tree1, h);
+        smb2_deltree(tree1, BASEDIR);
+        return ret;
+
+}
+
 /* Test how oplocks work on streams. */
 static bool test_raw_oplock_stream1(struct torture_context *tctx,
                                    struct smb2_tree *tree1,
@@ -2765,12 +2995,6 @@ static bool test_raw_oplock_stream1(struct torture_context *tctx,
                {&fname_default_stream, true,  SMB2_OPLOCK_LEVEL_EXCLUSIVE, SMB2_OPLOCK_LEVEL_II},
        };
 
-       /* Only passes against windows at the moment. */
-       if (torture_setting_bool(tctx, "samba3", false) ||
-           torture_setting_bool(tctx, "samba4", false)) {
-               torture_skip(tctx, "STREAM1 disabled against samba3+4\n");
-       }
-
        fname_stream = talloc_asprintf(tctx, "%s:%s", fname_base, stream);
        fname_default_stream = talloc_asprintf(tctx, "%s%s", fname_base,
                                               default_stream);
@@ -2912,16 +3136,19 @@ static bool test_raw_oplock_stream1(struct torture_context *tctx,
        return ret;
 }
 
-static bool test_smb2_oplock_doc(struct torture_context *tctx, struct smb2_tree *tree)
+static bool test_smb2_oplock_doc(struct torture_context *tctx, struct smb2_tree *tree,
+                                struct smb2_tree *tree2)
 {
        const char *fname = BASEDIR "\\test_oplock_doc.dat";
        NTSTATUS status;
        bool ret = true;
        union smb_open io;
        struct smb2_handle h, h1;
+       union smb_setfileinfo sfinfo;
 
        status = torture_smb2_testdir(tree, BASEDIR, &h);
        torture_assert_ntstatus_ok(tctx, status, "Error creating directory");
+       smb2_util_close(tree, h);
 
        /* cleanup */
        smb2_util_unlink(tree, fname);
@@ -2936,15 +3163,15 @@ static bool test_smb2_oplock_doc(struct torture_context *tctx, struct smb2_tree
        io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
        io.smb2.in.alloc_size = 0;
        io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
-       io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
+       io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ|
+               NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE;
        io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
-       io.smb2.in.create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
+       io.smb2.in.create_options = 0;
        io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
        io.smb2.in.security_flags = 0;
        io.smb2.in.fname = fname;
 
-       torture_comment(tctx, "open a delete-on-close file with a batch "
-                       "oplock\n");
+       torture_comment(tctx, "open a file with a batch oplock\n");
        ZERO_STRUCT(break_info);
        io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
        io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
@@ -2954,6 +3181,25 @@ static bool test_smb2_oplock_doc(struct torture_context *tctx, struct smb2_tree
        h1 = io.smb2.out.file.handle;
        CHECK_VAL(io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
 
+       torture_comment(tctx, "Set delete on close\n");
+       ZERO_STRUCT(sfinfo);
+       sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
+       sfinfo.generic.in.file.handle = h1;
+       sfinfo.disposition_info.in.delete_on_close = 1;
+       status = smb2_setinfo_file(tree, &sfinfo);
+       torture_assert_ntstatus_ok(tctx, status, "Incorrect status");
+
+       torture_comment(tctx, "2nd open should not break and get "
+                       "DELETE_PENDING\n");
+       ZERO_STRUCT(break_info);
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN;
+       io.smb2.in.create_options = 0;
+       io.smb2.in.desired_access = SEC_FILE_READ_DATA;
+       status = smb2_create(tree2, tctx, &io.smb2);
+       torture_assert_ntstatus_equal(tctx, status, NT_STATUS_DELETE_PENDING,
+                                     "Incorrect status");
+       CHECK_VAL(break_info.count, 0);
+
        smb2_util_close(tree, h1);
 
        smb2_util_unlink(tree, fname);
@@ -2973,7 +3219,6 @@ static bool test_smb2_oplock_brl1(struct torture_context *tctx,
        /*int fname, f;*/
        bool ret = true;
        uint8_t buf[1000];
-       bool correct = true;
        union smb_open io;
        NTSTATUS status;
        struct smb2_lock lck;
@@ -3025,7 +3270,7 @@ static bool test_smb2_oplock_brl1(struct torture_context *tctx,
        status = smb2_util_write(tree1, h1,buf, 0, sizeof(buf));
        if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
                torture_comment(tctx, "Failed to create file\n");
-               correct = false;
+               ret = false;
                goto done;
        }
 
@@ -3047,6 +3292,9 @@ static bool test_smb2_oplock_brl1(struct torture_context *tctx,
        ZERO_STRUCT(break_info);
 
        torture_comment(tctx, "a self BRL acquisition should break to none\n");
+
+       ZERO_STRUCT(lock);
+
        lock[0].offset = 0;
        lock[0].length = 4;
        lock[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
@@ -3096,7 +3344,6 @@ static bool test_smb2_oplock_brl2(struct torture_context *tctx, struct smb2_tree
        /*int fname, f;*/
        bool ret = true;
        uint8_t buf[1000];
-       bool correct = true;
        union smb_open io;
        NTSTATUS status;
        struct smb2_handle h, h1;
@@ -3148,7 +3395,7 @@ static bool test_smb2_oplock_brl2(struct torture_context *tctx, struct smb2_tree
        status = smb2_util_write(tree1, h1, buf, 0, sizeof(buf));
        if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
                torture_comment(tctx, "Failed to create file\n");
-               correct = false;
+               ret = false;
                goto done;
        }
 
@@ -3157,6 +3404,8 @@ static bool test_smb2_oplock_brl2(struct torture_context *tctx, struct smb2_tree
        torture_comment(tctx, "a self BRL acquisition should not break to "
                        "none\n");
 
+       ZERO_STRUCT(lock);
+
        lock[0].offset = 0;
        lock[0].length = 4;
        lock[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
@@ -3198,7 +3447,6 @@ static bool test_smb2_oplock_brl3(struct torture_context *tctx, struct smb2_tree
        const char *fname = BASEDIR "\\test_batch_brl.dat";
        bool ret = true;
        uint8_t buf[1000];
-       bool correct = true;
        union smb_open io;
        NTSTATUS status;
        struct smb2_handle h, h1, h2;
@@ -3249,7 +3497,7 @@ static bool test_smb2_oplock_brl3(struct torture_context *tctx, struct smb2_tree
 
        if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
                torture_comment(tctx, "Failed to create file\n");
-               correct = false;
+               ret = false;
                goto done;
        }
 
@@ -3270,6 +3518,8 @@ static bool test_smb2_oplock_brl3(struct torture_context *tctx, struct smb2_tree
 
        torture_comment(tctx, "a self BRL acquisition should break to none\n");
 
+       ZERO_STRUCT(lock);
+
        lock[0].offset = 0;
        lock[0].length = 4;
        lock[0].flags = SMB2_LOCK_FLAG_EXCLUSIVE |
@@ -3396,33 +3646,362 @@ static bool test_smb2_oplock_levelII500(struct torture_context *tctx,
        return ret;
 }
 
-struct torture_suite *torture_smb2_oplocks_init(void)
+/*
+ * Test a double-break. Open a file with exclusive. Send off a second open
+ * request with OPEN_IF, triggering a break to level2. This should respond
+ * with level2. Before replying to the break to level2, fire off a third open
+ * with OVERWRITE_IF. The expected sequence would be that the 3rd opener gets
+ * a level2 immediately triggered by a break to none, but that seems not the
+ * case. Still investigating what the right behaviour should be.
+ */
+
+struct levelII501_state {
+       struct torture_context *tctx;
+       struct smb2_tree *tree1;
+       struct smb2_tree *tree2;
+       struct smb2_tree *tree3;
+       struct smb2_handle h;
+       struct smb2_handle h1;
+       union smb_open io;
+
+       struct smb2_handle break_handle;
+       uint8_t break_to;
+       struct smb2_break br;
+
+       bool done;
+};
+
+static bool torture_oplock_break_delay(struct smb2_transport *transport,
+                                      const struct smb2_handle *handle,
+                                      uint8_t level, void *private_data);
+static void levelII501_break_done(struct smb2_request *req);
+static void levelII501_open1_done(struct smb2_request *req);
+static void levelII501_open2_done(struct smb2_request *req);
+static void levelII501_2ndopen_cb(struct tevent_context *ev,
+                                 struct tevent_timer *te,
+                                 struct timeval current_time,
+                                 void *private_data);
+static void levelII501_break_timeout_cb(struct tevent_context *ev,
+                                       struct tevent_timer *te,
+                                       struct timeval current_time,
+                                       void *private_data);
+static void levelII501_timeout_cb(struct tevent_context *ev,
+                                 struct tevent_timer *te,
+                                 struct timeval current_time,
+                                 void *private_data);
+
+static bool test_smb2_oplock_levelII501(struct torture_context *tctx,
+                                       struct smb2_tree *tree1,
+                                       struct smb2_tree *tree2)
 {
-       struct torture_suite *suite =
-           torture_suite_create(talloc_autofree_context(), "oplock");
+       const char *fname = BASEDIR "\\test_levelII501.dat";
+       NTSTATUS status;
+       bool ret = true;
+       struct levelII501_state *state;
+       struct smb2_request *req;
+       struct tevent_timer *te;
 
-       torture_suite_add_2smb2_test(suite, "exclusive1", test_smb2_oplock_exclusive1);
-       torture_suite_add_2smb2_test(suite, "exclusive2", test_smb2_oplock_exclusive2);
-       torture_suite_add_2smb2_test(suite, "exclusive3", test_smb2_oplock_exclusive3);
-       torture_suite_add_2smb2_test(suite, "exclusive4", test_smb2_oplock_exclusive4);
-       torture_suite_add_2smb2_test(suite, "exclusive5", test_smb2_oplock_exclusive5);
-       torture_suite_add_2smb2_test(suite, "exclusive6", test_smb2_oplock_exclusive6);
-       torture_suite_add_2smb2_test(suite, "batch1", test_smb2_oplock_batch1);
-       torture_suite_add_2smb2_test(suite, "batch2", test_smb2_oplock_batch2);
-       torture_suite_add_2smb2_test(suite, "batch3", test_smb2_oplock_batch3);
-       torture_suite_add_2smb2_test(suite, "batch4", test_smb2_oplock_batch4);
-       torture_suite_add_2smb2_test(suite, "batch5", test_smb2_oplock_batch5);
-       torture_suite_add_2smb2_test(suite, "batch6", test_smb2_oplock_batch6);
-       torture_suite_add_2smb2_test(suite, "batch7", test_smb2_oplock_batch7);
-       torture_suite_add_2smb2_test(suite, "batch8", test_smb2_oplock_batch8);
-       torture_suite_add_2smb2_test(suite, "batch9", test_smb2_oplock_batch9);
-       torture_suite_add_2smb2_test(suite, "batch10", test_smb2_oplock_batch10);
-       torture_suite_add_2smb2_test(suite, "batch11", test_smb2_oplock_batch11);
-       torture_suite_add_2smb2_test(suite, "batch12", test_smb2_oplock_batch12);
-       torture_suite_add_2smb2_test(suite, "batch13", test_smb2_oplock_batch13);
-       torture_suite_add_2smb2_test(suite, "batch14", test_smb2_oplock_batch14);
-       torture_suite_add_2smb2_test(suite, "batch15", test_smb2_oplock_batch15);
-       torture_suite_add_2smb2_test(suite, "batch16", test_smb2_oplock_batch16);
+       state = talloc(tctx, struct levelII501_state);
+       state->tctx = tctx;
+       state->done = false;
+       state->tree1 = tree1;
+       state->tree2 = tree2;
+
+       if (!torture_smb2_connection(tctx, &state->tree3)) {
+               torture_fail(tctx, "Establishing SMB2 connection failed\n");
+               return false;
+       }
+
+       status = torture_smb2_testdir(tree1, BASEDIR, &state->h);
+       torture_assert_ntstatus_ok(tctx, status, "Error creating directory");
+
+       /* cleanup */
+       smb2_util_unlink(tree1, fname);
+
+       /*
+         base ntcreatex parms
+       */
+       ZERO_STRUCT(state->io.smb2);
+       state->io.generic.level = RAW_OPEN_SMB2;
+       state->io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       state->io.smb2.in.alloc_size = 0;
+       state->io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       state->io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       state->io.smb2.in.create_options = 0;
+       state->io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       state->io.smb2.in.security_flags = 0;
+       state->io.smb2.in.fname = fname;
+
+       torture_comment(tctx, "LEVELII501: Test double break sequence\n");
+       ZERO_STRUCT(break_info);
+
+       state->io.smb2.in.desired_access = SEC_RIGHTS_FILE_READ |
+                               SEC_RIGHTS_FILE_WRITE;
+       state->io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
+                               NTCREATEX_SHARE_ACCESS_WRITE;
+       state->io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
+       state->io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+
+       tree1->session->transport->oplock.handler = torture_oplock_break_delay;
+       tree1->session->transport->oplock.private_data = state;
+
+       status = smb2_create(tree1, tctx, &(state->io.smb2));
+       torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
+       state->h1 = state->io.smb2.out.file.handle;
+       CHECK_VAL(state->io.smb2.out.oplock_level, SMB2_OPLOCK_LEVEL_EXCLUSIVE);
+
+       /*
+        * Trigger a break to level2
+        */
+
+       req = smb2_create_send(tree2, &state->io.smb2);
+       req->async.fn = levelII501_open1_done;
+       req->async.private_data = state;
+
+       te = tevent_add_timer(
+               tctx->ev, tctx, tevent_timeval_current_ofs(0, 200000),
+               levelII501_2ndopen_cb, state);
+       torture_assert(tctx, te != NULL, "tevent_add_timer failed\n");
+
+       te = tevent_add_timer(
+               tctx->ev, tctx, tevent_timeval_current_ofs(2, 0),
+               levelII501_timeout_cb, state);
+       torture_assert(tctx, te != NULL, "tevent_add_timer failed\n");
+
+       while (!state->done) {
+               if (tevent_loop_once(tctx->ev) != 0) {
+                       torture_comment(tctx, "tevent_loop_once failed\n");
+               }
+       }
+
+       return ret;
+}
+
+/*
+ * Fire off a second open after a little timeout
+ */
+
+static void levelII501_2ndopen_cb(struct tevent_context *ev,
+                                 struct tevent_timer *te,
+                                 struct timeval current_time,
+                                 void *private_data)
+{
+       struct levelII501_state *state = talloc_get_type_abort(
+               private_data, struct levelII501_state);
+       struct smb2_request *req;
+
+       state->io.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE_IF;
+       req = smb2_create_send(state->tree3, &state->io.smb2);
+       req->async.fn = levelII501_open2_done;
+       req->async.private_data = state;
+}
+
+/*
+ * Postpone the break response by 500 msec
+ */
+static bool torture_oplock_break_delay(struct smb2_transport *transport,
+                                      const struct smb2_handle *handle,
+                                      uint8_t level, void *private_data)
+{
+       struct levelII501_state *state = talloc_get_type_abort(
+               private_data, struct levelII501_state);
+       const char *name;
+       struct tevent_timer *te;
+
+       break_info.handle       = *handle;
+       break_info.level        = level;
+       break_info.count++;
+
+       state->break_handle = *handle;
+       state->break_to = level;
+
+       switch(level) {
+       case SMB2_OPLOCK_LEVEL_II:
+               name = "level II";
+               break;
+       case SMB2_OPLOCK_LEVEL_NONE:
+               name = "none";
+               break;
+       default:
+               name = "unknown";
+               break;
+       }
+       printf("Got break to %s [0x%02X] in oplock handler, postponing "
+              "break response for 500msec\n", name, level);
+
+       te = tevent_add_timer(
+               state->tctx->ev, state->tctx,
+               tevent_timeval_current_ofs(0, 500000),
+               levelII501_break_timeout_cb, state);
+       torture_assert(state->tctx, te != NULL, "tevent_add_timer failed\n");
+
+       return true;
+}
+
+static void levelII501_break_timeout_cb(struct tevent_context *ev,
+                                       struct tevent_timer *te,
+                                       struct timeval current_time,
+                                       void *private_data)
+{
+       struct levelII501_state *state = talloc_get_type_abort(
+               private_data, struct levelII501_state);
+       struct smb2_request *req;
+
+       talloc_free(te);
+
+       ZERO_STRUCT(state->br);
+       state->br.in.file.handle = state->break_handle;
+       state->br.in.oplock_level = state->break_to;
+
+       req = smb2_break_send(state->tree1, &state->br);
+       req->async.fn = levelII501_break_done;
+       req->async.private_data = state;
+}
+
+static void levelII501_break_done(struct smb2_request *req)
+{
+       struct smb2_break io;
+       NTSTATUS status;
+
+       status = smb2_break_recv(req, &io);
+       printf("break done: %s\n", nt_errstr(status));
+}
+
+static void levelII501_open1_done(struct smb2_request *req)
+{
+       struct levelII501_state *state = talloc_get_type_abort(
+               req->async.private_data, struct levelII501_state);
+       struct smb2_create io;
+       NTSTATUS status;
+
+       status = smb2_create_recv(req, state, &io);
+       printf("open1 done: %s\n", nt_errstr(status));
+}
+
+static void levelII501_open2_done(struct smb2_request *req)
+{
+       struct levelII501_state *state = talloc_get_type_abort(
+               req->async.private_data, struct levelII501_state);
+       struct smb2_create io;
+       NTSTATUS status;
+
+       status = smb2_create_recv(req, state, &io);
+       printf("open2 done: %s\n", nt_errstr(status));
+}
+
+static void levelII501_timeout_cb(struct tevent_context *ev,
+                                 struct tevent_timer *te,
+                                 struct timeval current_time,
+                                 void *private_data)
+{
+       struct levelII501_state *state = talloc_get_type_abort(
+               private_data, struct levelII501_state);
+       talloc_free(te);
+       state->done = true;
+}
+
+static bool test_smb2_oplock_levelII502(struct torture_context *tctx,
+                                       struct smb2_tree *tree1,
+                                       struct smb2_tree *tree2)
+
+{
+       const char *fname = BASEDIR "\\test_levelII502.dat";
+       NTSTATUS status;
+       union smb_open io;
+       struct smb2_close closeio;
+       struct smb2_handle h;
+
+       status = torture_smb2_testdir(tree1, BASEDIR, &h);
+       torture_assert_ntstatus_ok(tctx, status, "Error creating directory");
+
+       /* cleanup */
+       smb2_util_unlink(tree1, fname);
+
+       /*
+         base ntcreatex parms
+       */
+       ZERO_STRUCT(io.smb2);
+       io.generic.level = RAW_OPEN_SMB2;
+       io.smb2.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       io.smb2.in.alloc_size = 0;
+       io.smb2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       io.smb2.in.create_options = 0;
+       io.smb2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       io.smb2.in.security_flags = 0;
+       io.smb2.in.fname = fname;
+
+       torture_comment(
+               tctx,
+               "LEVELII502: Open a stale LEVEL2 oplock with OVERWRITE");
+
+       io.smb2.in.desired_access = SEC_RIGHTS_FILE_READ |
+                               SEC_RIGHTS_FILE_WRITE;
+       io.smb2.in.share_access = NTCREATEX_SHARE_ACCESS_READ |
+                               NTCREATEX_SHARE_ACCESS_WRITE;
+       io.smb2.in.create_flags = NTCREATEX_FLAGS_EXTENDED;
+       io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_II;
+       status = smb2_create(tree1, tctx, &(io.smb2));
+       torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
+       torture_assert(tctx,
+                      io.smb2.out.oplock_level==SMB2_OPLOCK_LEVEL_II,
+                      "Did not get LEVEL_II oplock\n");
+
+       status = smbXcli_conn_samba_suicide(
+               tree1->session->transport->conn, 93);
+       torture_assert_ntstatus_ok(tctx, status, "suicide failed");
+
+       sleep(1);
+
+       io.smb2.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+       io.smb2.in.create_disposition = NTCREATEX_DISP_OVERWRITE;
+
+       status = smb2_create(tree2, tctx, &(io.smb2));
+       torture_assert_ntstatus_ok(tctx, status, "Error opening the file");
+       torture_assert(tctx,
+                      io.smb2.out.oplock_level==SMB2_OPLOCK_LEVEL_BATCH,
+                      "Did not get BATCH oplock\n");
+
+       closeio = (struct smb2_close) {
+               .in.file.handle = io.smb2.out.file.handle,
+       };
+       status = smb2_close(tree2, &closeio);
+       torture_assert_ntstatus_equal(
+               tctx, status, NT_STATUS_OK, "close failed");
+
+       return true;
+}
+
+struct torture_suite *torture_smb2_oplocks_init(TALLOC_CTX *ctx)
+{
+       struct torture_suite *suite =
+           torture_suite_create(ctx, "oplock");
+
+       torture_suite_add_2smb2_test(suite, "exclusive1", test_smb2_oplock_exclusive1);
+       torture_suite_add_2smb2_test(suite, "exclusive2", test_smb2_oplock_exclusive2);
+       torture_suite_add_2smb2_test(suite, "exclusive3", test_smb2_oplock_exclusive3);
+       torture_suite_add_2smb2_test(suite, "exclusive4", test_smb2_oplock_exclusive4);
+       torture_suite_add_2smb2_test(suite, "exclusive5", test_smb2_oplock_exclusive5);
+       torture_suite_add_2smb2_test(suite, "exclusive6", test_smb2_oplock_exclusive6);
+       torture_suite_add_2smb2_test(suite, "exclusive9",
+                                    test_smb2_oplock_exclusive9);
+       torture_suite_add_2smb2_test(suite, "batch1", test_smb2_oplock_batch1);
+       torture_suite_add_2smb2_test(suite, "batch2", test_smb2_oplock_batch2);
+       torture_suite_add_2smb2_test(suite, "batch3", test_smb2_oplock_batch3);
+       torture_suite_add_2smb2_test(suite, "batch4", test_smb2_oplock_batch4);
+       torture_suite_add_2smb2_test(suite, "batch5", test_smb2_oplock_batch5);
+       torture_suite_add_2smb2_test(suite, "batch6", test_smb2_oplock_batch6);
+       torture_suite_add_2smb2_test(suite, "batch7", test_smb2_oplock_batch7);
+       torture_suite_add_2smb2_test(suite, "batch8", test_smb2_oplock_batch8);
+       torture_suite_add_2smb2_test(suite, "batch9", test_smb2_oplock_batch9);
+       torture_suite_add_2smb2_test(suite, "batch9a", test_smb2_oplock_batch9a);
+       torture_suite_add_2smb2_test(suite, "batch10", test_smb2_oplock_batch10);
+       torture_suite_add_2smb2_test(suite, "batch11", test_smb2_oplock_batch11);
+       torture_suite_add_2smb2_test(suite, "batch12", test_smb2_oplock_batch12);
+       torture_suite_add_2smb2_test(suite, "batch13", test_smb2_oplock_batch13);
+       torture_suite_add_2smb2_test(suite, "batch14", test_smb2_oplock_batch14);
+       torture_suite_add_2smb2_test(suite, "batch15", test_smb2_oplock_batch15);
+       torture_suite_add_2smb2_test(suite, "batch16", test_smb2_oplock_batch16);
        torture_suite_add_1smb2_test(suite, "batch19", test_smb2_oplock_batch19);
        torture_suite_add_2smb2_test(suite, "batch20", test_smb2_oplock_batch20);
        torture_suite_add_1smb2_test(suite, "batch21", test_smb2_oplock_batch21);
@@ -3430,13 +4009,17 @@ struct torture_suite *torture_smb2_oplocks_init(void)
        torture_suite_add_2smb2_test(suite, "batch23", test_smb2_oplock_batch23);
        torture_suite_add_2smb2_test(suite, "batch24", test_smb2_oplock_batch24);
        torture_suite_add_1smb2_test(suite, "batch25", test_smb2_oplock_batch25);
+       torture_suite_add_1smb2_test(suite, "batch26", test_smb2_oplock_batch26);
        torture_suite_add_2smb2_test(suite, "stream1", test_raw_oplock_stream1);
-       torture_suite_add_1smb2_test(suite, "doc", test_smb2_oplock_doc);
+       torture_suite_add_2smb2_test(suite, "doc", test_smb2_oplock_doc);
        torture_suite_add_2smb2_test(suite, "brl1", test_smb2_oplock_brl1);
        torture_suite_add_1smb2_test(suite, "brl2", test_smb2_oplock_brl2);
        torture_suite_add_1smb2_test(suite, "brl3", test_smb2_oplock_brl3);
        torture_suite_add_1smb2_test(suite, "levelii500", test_smb2_oplock_levelII500);
-
+       torture_suite_add_2smb2_test(suite, "levelii501",
+                                    test_smb2_oplock_levelII501);
+       torture_suite_add_2smb2_test(suite, "levelii502",
+                                    test_smb2_oplock_levelII502);
        suite->description = talloc_strdup(suite, "SMB2-OPLOCK tests");
 
        return suite;
@@ -3528,14 +4111,26 @@ static struct hold_oplock_info {
        uint32_t share_access;
        struct smb2_handle handle;
 } hold_info[] = {
-       { BASEDIR "\\notshared_close", true,
-         NTCREATEX_SHARE_ACCESS_NONE, },
-       { BASEDIR "\\notshared_noclose", false,
-         NTCREATEX_SHARE_ACCESS_NONE, },
-       { BASEDIR "\\shared_close", true,
-         NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, },
-       { BASEDIR "\\shared_noclose", false,
-         NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE, },
+       {
+               .fname          = BASEDIR "\\notshared_close",
+               .close_on_break = true,
+               .share_access   = NTCREATEX_SHARE_ACCESS_NONE,
+       },
+       {
+               .fname          = BASEDIR "\\notshared_noclose",
+               .close_on_break = false,
+               .share_access   = NTCREATEX_SHARE_ACCESS_NONE,
+       },
+       {
+               .fname          = BASEDIR "\\shared_close",
+               .close_on_break = true,
+               .share_access   = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,
+       },
+       {
+               .fname          = BASEDIR "\\shared_noclose",
+               .close_on_break = false,
+               .share_access   = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE|NTCREATEX_SHARE_ACCESS_DELETE,
+       },
 };
 
 static bool torture_oplock_handler_hold(struct smb2_transport *transport,
@@ -3648,3 +4243,929 @@ bool test_smb2_hold_oplock(struct torture_context *tctx,
        talloc_free(mem_ctx);
        return true;
 }
+
+
+static bool test_smb2_kernel_oplocks1(struct torture_context *tctx,
+                                     struct smb2_tree *tree)
+{
+       const char *fname = "test_kernel_oplock1.dat";
+       NTSTATUS status;
+       bool ret = true;
+       struct smb2_create create;
+       struct smb2_handle h1 = {{0}}, h2 = {{0}};
+
+       smb2_util_unlink(tree, fname);
+
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+       ZERO_STRUCT(break_info);
+
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = fname;
+       create.in.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h1 = create.out.file.handle;
+
+       torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE, ret, done,
+                           "Oplock level is not SMB2_OPLOCK_LEVEL_EXCLUSIVE\n");
+
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = fname;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_equal_goto(tctx, status, NT_STATUS_SHARING_VIOLATION, ret, done,
+                                          "Open didn't return NT_STATUS_SHARING_VIOLATION\n");
+       h2 = create.out.file.handle;
+
+       torture_wait_for_oplock_break(tctx);
+       if (break_info.count != 0) {
+               torture_warning(tctx, "Open caused oplock break\n");
+       }
+
+       smb2_util_close(tree, h1);
+       smb2_util_close(tree, h2);
+
+done:
+       if (!smb2_util_handle_empty(h1)) {
+               smb2_util_close(tree, h1);
+       }
+       if (!smb2_util_handle_empty(h2)) {
+               smb2_util_close(tree, h2);
+       }
+       smb2_util_unlink(tree, fname);
+       return ret;
+}
+
+static bool test_smb2_kernel_oplocks2(struct torture_context *tctx,
+                                     struct smb2_tree *tree)
+{
+       const char *fname = "test_kernel_oplock2.dat";
+       const char *sname = "test_kernel_oplock2.dat:foo";
+       NTSTATUS status;
+       bool ret = true;
+       struct smb2_create create;
+       struct smb2_handle h1 = {{0}}, h2 = {{0}};
+
+       smb2_util_unlink(tree, fname);
+
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+       ZERO_STRUCT(break_info);
+
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_NONE;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = fname;
+       create.in.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h1 = create.out.file.handle;
+
+       torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE, ret, done,
+                           "Oplock level is not SMB2_OPLOCK_LEVEL_EXCLUSIVE\n");
+
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = sname;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h2 = create.out.file.handle;
+
+       torture_wait_for_oplock_break(tctx);
+       if (break_info.count != 0) {
+               torture_warning(tctx, "Stream open caused oplock break\n");
+       }
+
+       smb2_util_close(tree, h1);
+       smb2_util_close(tree, h2);
+
+done:
+       if (!smb2_util_handle_empty(h1)) {
+               smb2_util_close(tree, h1);
+       }
+       if (!smb2_util_handle_empty(h2)) {
+               smb2_util_close(tree, h2);
+       }
+       smb2_util_unlink(tree, fname);
+       return ret;
+}
+
+/**
+ * 1. 1st client opens file with oplock
+ * 2. 2nd client opens file
+ *
+ * Verify 2 triggers an oplock break
+ **/
+static bool test_smb2_kernel_oplocks3(struct torture_context *tctx,
+                                     struct smb2_tree *tree,
+                                     struct smb2_tree *tree2)
+{
+       const char *fname = "test_kernel_oplock3.dat";
+       NTSTATUS status;
+       bool ret = true;
+       struct smb2_create create;
+       struct smb2_handle h1 = {{0}}, h2 = {{0}};
+
+       smb2_util_unlink(tree, fname);
+       status = torture_smb2_testfile(tree, fname, &h1);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "Error creating testfile\n");
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+       ZERO_STRUCT(break_info);
+
+       /* 1 */
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = fname;
+       create.in.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h1 = create.out.file.handle;
+
+       torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE, ret, done,
+                           "Oplock level is not SMB2_OPLOCK_LEVEL_EXCLUSIVE\n");
+
+       /* 2 */
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_READ;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = fname;
+
+       status = smb2_create(tree2, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h2 = create.out.file.handle;
+
+       torture_wait_for_oplock_break(tctx);
+       torture_assert_goto(tctx, break_info.count == 1, ret, done, "Expected 1 oplock break\n");
+
+done:
+       if (!smb2_util_handle_empty(h1)) {
+               smb2_util_close(tree, h1);
+       }
+       if (!smb2_util_handle_empty(h2)) {
+               smb2_util_close(tree, h2);
+       }
+       smb2_util_unlink(tree, fname);
+       return ret;
+}
+
+/**
+ * 1) create testfile with stream
+ * 2) open file r/w with batch oplock, sharing read/delete
+ * 3) open stream on file for reading
+ *
+ * Verify 3) doesn't trigger an oplock break
+ **/
+static bool test_smb2_kernel_oplocks4(struct torture_context *tctx,
+                                     struct smb2_tree *tree)
+{
+       const char *fname = "test_kernel_oplock4.dat";
+       const char *sname = "test_kernel_oplock4.dat:foo";
+       NTSTATUS status;
+       bool ret = true;
+       struct smb2_create create;
+       struct smb2_handle h1 = {{0}}, h2 = {{0}};
+
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+       ZERO_STRUCT(break_info);
+       smb2_util_unlink(tree, fname);
+
+       /* 1 */
+       status = torture_smb2_testfile(tree, fname, &h1);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "Error creating testfile\n");
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_READ;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = sname;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h1 = create.out.file.handle;
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       /* 2 */
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = fname;
+       create.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h1 = create.out.file.handle;
+
+       torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_BATCH, ret, done,
+                           "Oplock level is not SMB2_OPLOCK_LEVEL_BATCH\n");
+
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_READ;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_DELETE;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = sname;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h2 = create.out.file.handle;
+
+       torture_wait_for_oplock_break(tctx);
+       if (break_info.count != 0) {
+               torture_warning(tctx, "Stream open caused oplock break\n");
+       }
+
+done:
+       if (!smb2_util_handle_empty(h1)) {
+               smb2_util_close(tree, h1);
+       }
+       if (!smb2_util_handle_empty(h2)) {
+               smb2_util_close(tree, h2);
+       }
+       smb2_util_unlink(tree, fname);
+       return ret;
+}
+
+/**
+ * 1) create testfile with stream
+ * 2) open stream r/w with batch oplock -> batch oplock granted
+ * 3) open stream r/o with batch oplock
+ *
+ * Verify 3) does trigger an oplock break
+ **/
+static bool test_smb2_kernel_oplocks5(struct torture_context *tctx,
+                                     struct smb2_tree *tree)
+{
+       const char *fname = "test_kernel_oplock4.dat";
+       const char *sname = "test_kernel_oplock4.dat:foo";
+       NTSTATUS status;
+       bool ret = true;
+       struct smb2_create create;
+       struct smb2_handle h1 = {{0}}, h2 = {{0}};
+
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+       ZERO_STRUCT(break_info);
+       smb2_util_unlink(tree, fname);
+
+       /* 1 */
+       status = torture_smb2_testfile(tree, fname, &h1);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "Error creating testfile\n");
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_READ;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = sname;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h1 = create.out.file.handle;
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       /* 2 */
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = sname;
+       create.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h1 = create.out.file.handle;
+
+       torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_BATCH, ret, done,
+                           "Oplock level is not SMB2_OPLOCK_LEVEL_BATCH\n");
+
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_READ;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = sname;
+       create.in.oplock_level = SMB2_OPLOCK_LEVEL_BATCH;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h2 = create.out.file.handle;
+
+       torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_NONE, ret, done,
+                           "Oplock level is not SMB2_OPLOCK_LEVEL_NONE\n");
+
+       torture_wait_for_oplock_break(tctx);
+       if (break_info.count != 1) {
+               torture_warning(tctx, "Stream open didn't cause oplock break\n");
+       }
+
+done:
+       if (!smb2_util_handle_empty(h1)) {
+               smb2_util_close(tree, h1);
+       }
+       if (!smb2_util_handle_empty(h2)) {
+               smb2_util_close(tree, h2);
+       }
+       smb2_util_unlink(tree, fname);
+       return ret;
+}
+
+/**
+ * 1) create testfile with stream
+ * 2) 1st client opens stream r/w with batch oplock -> batch oplock granted
+ * 3) 2nd client opens stream r/o with batch oplock
+ *
+ * Verify 3) does trigger an oplock break
+ **/
+static bool test_smb2_kernel_oplocks6(struct torture_context *tctx,
+                                     struct smb2_tree *tree,
+                                     struct smb2_tree *tree2)
+{
+       const char *fname = "test_kernel_oplock6.dat";
+       const char *sname = "test_kernel_oplock6.dat:foo";
+       NTSTATUS status;
+       bool ret = true;
+       struct smb2_create create;
+       struct smb2_handle h1 = {{0}}, h2 = {{0}};
+
+       smb2_util_unlink(tree, fname);
+       status = torture_smb2_testfile(tree, fname, &h1);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "Error creating testfile\n");
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+       tree->session->transport->oplock.private_data = tree;
+       ZERO_STRUCT(break_info);
+
+       /* 1 */
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_READ;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = sname;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h1 = create.out.file.handle;
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       /* 2 */
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN_IF;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = fname;
+       create.in.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h1 = create.out.file.handle;
+
+       torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_EXCLUSIVE, ret, done,
+                           "Oplock level is not SMB2_OPLOCK_LEVEL_EXCLUSIVE\n");
+
+       /* 3 */
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_READ;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = fname;
+
+       status = smb2_create(tree2, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done, "Error opening the file\n");
+       h2 = create.out.file.handle;
+
+       torture_assert_goto(tctx, create.out.oplock_level == SMB2_OPLOCK_LEVEL_NONE, ret, done,
+                           "Oplock level is not SMB2_OPLOCK_LEVEL_NONE\n");
+
+       torture_wait_for_oplock_break(tctx);
+       torture_assert_goto(tctx, break_info.count == 1, ret, done, "Expected 1 oplock break\n");
+
+done:
+       if (!smb2_util_handle_empty(h1)) {
+               smb2_util_close(tree, h1);
+       }
+       if (!smb2_util_handle_empty(h2)) {
+               smb2_util_close(tree, h2);
+       }
+       smb2_util_unlink(tree, fname);
+       return ret;
+}
+
+/**
+ * Recreate regression test from bug:
+ *
+ * https://bugzilla.samba.org/show_bug.cgi?id=13058
+ *
+ * 1. smbd-1 opens the file and sets the oplock
+ * 2. smbd-2 tries to open the file. open() fails(EAGAIN) and open is deferred.
+ * 3. smbd-1 sends oplock break request to the client.
+ * 4. smbd-1 closes the file.
+ * 5. smbd-1 opens the file and sets the oplock.
+ * 6. smbd-2 calls defer_open_done(), and should re-break the oplock.
+ **/
+
+static bool test_smb2_kernel_oplocks7(struct torture_context *tctx,
+                                     struct smb2_tree *tree,
+                                     struct smb2_tree *tree2)
+{
+       const char *fname = "test_kernel_oplock7.dat";
+       NTSTATUS status;
+       bool ret = true;
+       struct smb2_create create;
+       struct smb2_handle h1 = {{0}}, h2 = {{0}};
+       struct smb2_create create_2;
+        struct smb2_create io;
+       struct smb2_request *req;
+
+       smb2_util_unlink(tree, fname);
+       status = torture_smb2_testfile(tree, fname, &h1);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "Error creating testfile\n");
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       /* Close the open file on break. */
+       tree->session->transport->oplock.handler = torture_oplock_handler_close;
+       tree->session->transport->oplock.private_data = tree;
+       ZERO_STRUCT(break_info);
+
+       /* 1 - open file with oplock */
+       ZERO_STRUCT(create);
+       create.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       create.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create.in.create_disposition = NTCREATEX_DISP_OPEN;
+       create.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create.in.fname = fname;
+       create.in.oplock_level = SMB2_OPLOCK_LEVEL_EXCLUSIVE;
+
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                       "Error opening the file\n");
+       CHECK_VAL(create.out.oplock_level, SMB2_OPLOCK_LEVEL_EXCLUSIVE);
+
+       /* 2 - open file to break oplock */
+       ZERO_STRUCT(create_2);
+       create_2.in.desired_access = SEC_RIGHTS_FILE_ALL;
+       create_2.in.file_attributes = FILE_ATTRIBUTE_NORMAL;
+       create_2.in.share_access = NTCREATEX_SHARE_ACCESS_MASK;
+       create_2.in.create_disposition = NTCREATEX_DISP_OPEN;
+       create_2.in.impersonation_level = SMB2_IMPERSONATION_ANONYMOUS;
+       create_2.in.fname = fname;
+       create_2.in.oplock_level = SMB2_OPLOCK_LEVEL_NONE;
+
+       /* Open on tree2 - should cause a break on tree */
+       req = smb2_create_send(tree2, &create_2);
+       torture_assert(tctx, req != NULL, "smb2_create_send");
+
+       /* The oplock break handler should close the file. */
+       /* Steps 3 & 4. */
+       torture_wait_for_oplock_break(tctx);
+
+       tree->session->transport->oplock.handler = torture_oplock_handler;
+
+       /*
+        * 5 - re-open on tree. NB. There is a race here
+        * depending on which smbd goes first. We either get
+        * an oplock level of SMB2_OPLOCK_LEVEL_EXCLUSIVE if
+        * the close and re-open on tree is processed first, or
+        * SMB2_OPLOCK_LEVEL_NONE if the pending create on
+        * tree2 is processed first.
+        */
+       status = smb2_create(tree, tctx, &create);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                       "Error opening the file\n");
+
+       h1 = create.out.file.handle;
+       if (create.out.oplock_level != SMB2_OPLOCK_LEVEL_EXCLUSIVE &&
+           create.out.oplock_level != SMB2_OPLOCK_LEVEL_NONE) {
+               torture_result(tctx,
+                       TORTURE_FAIL,
+                       "(%s): wrong value for oplock got 0x%x\n",
+                       __location__,
+                       (unsigned int)create.out.oplock_level);
+                ret = false;
+               goto done;
+
+       }
+
+       /* 6 - retrieve the second open. */
+       status = smb2_create_recv(req, tctx, &io);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                       "Error opening the file\n");
+       h2 = io.out.file.handle;
+       CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_NONE);
+
+done:
+       if (!smb2_util_handle_empty(h1)) {
+               smb2_util_close(tree, h1);
+       }
+       if (!smb2_util_handle_empty(h2)) {
+               smb2_util_close(tree2, h2);
+       }
+       smb2_util_unlink(tree, fname);
+       return ret;
+}
+
+#ifdef HAVE_KERNEL_OPLOCKS_LINUX
+
+#ifndef F_SETLEASE
+#define F_SETLEASE      1024
+#endif
+
+#ifndef RT_SIGNAL_LEASE
+#define RT_SIGNAL_LEASE (SIGRTMIN+1)
+#endif
+
+#ifndef F_SETSIG
+#define F_SETSIG 10
+#endif
+
+static int got_break;
+
+/*
+ * Signal handler.
+ */
+
+static void got_rt_break(int sig)
+{
+       got_break = 1;
+}
+
+static int got_alarm;
+
+/*
+ * Signal handler.
+ */
+
+static void got_alarm_fn(int sig)
+{
+       got_alarm = 1;
+}
+
+/*
+ * Child process function.
+ */
+
+static int do_child_process(int pipefd, const char *name)
+{
+       int ret = 0;
+       int fd = -1;
+       char c = 0;
+       struct sigaction act;
+       sigset_t set;
+       sigset_t empty_set;
+
+       /* Block RT_SIGNAL_LEASE and SIGALRM. */
+       sigemptyset(&set);
+       sigemptyset(&empty_set);
+       sigaddset(&set, RT_SIGNAL_LEASE);
+       sigaddset(&set, SIGALRM);
+       ret = sigprocmask(SIG_SETMASK, &set, NULL);
+       if (ret == -1) {
+               return 11;
+       }
+
+       /* Set up a signal handler for RT_SIGNAL_LEASE. */
+       ZERO_STRUCT(act);
+       act.sa_handler = got_rt_break;
+       ret = sigaction(RT_SIGNAL_LEASE, &act, NULL);
+       if (ret == -1) {
+               return 1;
+       }
+       /* Set up a signal handler for SIGALRM. */
+       ZERO_STRUCT(act);
+       act.sa_handler = got_alarm_fn;
+       ret = sigaction(SIGALRM, &act, NULL);
+       if (ret == -1) {
+               return 1;
+       }
+       /* Open the passed in file and get a kernel oplock. */
+       fd = open(name, O_RDWR, 0666);
+       if (fd == -1) {
+               return 2;
+       }
+
+       ret = fcntl(fd, F_SETSIG, RT_SIGNAL_LEASE);
+       if (ret == -1) {
+               close(fd);
+               return 3;
+       }
+
+       ret = fcntl(fd, F_SETLEASE, F_WRLCK);
+       if (ret == -1) {
+               close(fd);
+               return 4;
+       }
+
+       /* Tell the parent we're ready. */
+       ret = sys_write(pipefd, &c, 1);
+       if (ret != 1) {
+               close(fd);
+               return 5;
+       }
+
+       /* Ensure the pause doesn't hang forever. */
+       alarm(5);
+
+       /* Wait for RT_SIGNAL_LEASE or SIGALRM. */
+       ret = sigsuspend(&empty_set);
+       if (ret != -1 || errno != EINTR) {
+               close(fd);
+               return 6;
+       }
+
+       if (got_alarm == 1) {
+               close(fd);
+               return 10;
+       }
+
+       if (got_break != 1) {
+               close(fd);
+               return 7;
+       }
+
+       /* Cancel any pending alarm. */
+       alarm(0);
+
+       /* Force the server to wait for 3 seconds. */
+       sleep(3);
+
+       /* Remove our lease. */
+       ret = fcntl(fd, F_SETLEASE, F_UNLCK);
+       if (ret == -1) {
+               close(fd);
+               return 8;
+       }
+
+       ret = close(fd);
+       if (ret == -1) {
+               return 9;
+       }
+
+       /* All is well. */
+       return 0;
+}
+
+static bool wait_for_child_oplock(struct torture_context *tctx,
+                               const char *localdir,
+                               const char *fname)
+{
+       int fds[2];
+       int ret;
+       pid_t pid;
+       char *name = talloc_asprintf(tctx,
+                               "%s/%s",
+                               localdir,
+                               fname);
+
+       torture_assert(tctx, name != NULL, "talloc failed");
+
+       ret = pipe(fds);
+       torture_assert(tctx, ret != -1, "pipe failed");
+
+       pid = fork();
+       torture_assert(tctx, pid != (pid_t)-1, "fork failed");
+
+       if (pid != (pid_t)0) {
+               char c;
+               /* Parent. */
+               TALLOC_FREE(name);
+               close(fds[1]);
+               ret = sys_read(fds[0], &c, 1);
+               torture_assert(tctx, ret == 1, "read failed");
+               return true;
+       }
+
+       /* Child process. */
+       close(fds[0]);
+       ret = do_child_process(fds[1], name);
+       _exit(ret);
+       /* Notreached. */
+}
+#else
+static bool wait_for_child_oplock(struct torture_context *tctx,
+                               const char *localdir,
+                               const char *fname)
+{
+       return false;
+}
+#endif
+
+static void child_sig_term_handler(struct tevent_context *ev,
+                               struct tevent_signal *se,
+                               int signum,
+                               int count,
+                               void *siginfo,
+                               void *private_data)
+{
+       int *pstatus = (int *)private_data;
+       int status = 0;
+
+       wait(&status);
+       if (WIFEXITED(status)) {
+               *pstatus = WEXITSTATUS(status);
+       } else {
+               *pstatus = status;
+       }
+}
+
+/*
+ * Deal with a non-smbd process holding a kernel oplock.
+ */
+
+static bool test_smb2_kernel_oplocks8(struct torture_context *tctx,
+                                     struct smb2_tree *tree)
+{
+       const char *fname = "test_kernel_oplock8.dat";
+       const char *fname1 = "tmp_test_kernel_oplock8.dat";
+       NTSTATUS status;
+       bool ret = true;
+       struct smb2_create io;
+       struct smb2_request *req = NULL;
+       struct smb2_handle h1 = {{0}};
+       struct smb2_handle h2 = {{0}};
+       const char *localdir = torture_setting_string(tctx, "localdir", NULL);
+       struct tevent_signal *se = NULL;
+       int child_exit_code = -1;
+       time_t start;
+       time_t end;
+
+#ifndef HAVE_KERNEL_OPLOCKS_LINUX
+       torture_skip(tctx, "Need kernel oplocks for test");
+#endif
+
+       if (localdir == NULL) {
+               torture_skip(tctx, "Need localdir for test");
+       }
+
+       smb2_util_unlink(tree, fname);
+       smb2_util_unlink(tree, fname1);
+       status = torture_smb2_testfile(tree, fname, &h1);
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "Error creating testfile\n");
+       smb2_util_close(tree, h1);
+       ZERO_STRUCT(h1);
+
+       se = tevent_add_signal(tctx->ev,
+                               tctx,
+                               SIGCHLD,
+                               0,
+                               child_sig_term_handler,
+                               &child_exit_code);
+       torture_assert(tctx, se != NULL, "tevent_add_signal failed\n");
+
+       /* Take the oplock locally in a sub-process. */
+       ret = wait_for_child_oplock(tctx, localdir, fname);
+       torture_assert_goto(tctx, ret, ret, done,
+               "Wait for child process failed.\n");
+
+       /*
+        * Now try and open. This should block for 3 seconds.
+        * while the child process is still alive.
+        */
+
+       ZERO_STRUCT(io);
+       io.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED;
+       io.in.file_attributes   = FILE_ATTRIBUTE_NORMAL;
+       io.in.create_disposition = NTCREATEX_DISP_OPEN;
+       io.in.share_access =
+               NTCREATEX_SHARE_ACCESS_DELETE|
+               NTCREATEX_SHARE_ACCESS_READ|
+               NTCREATEX_SHARE_ACCESS_WRITE;
+       io.in.create_options = 0;
+       io.in.fname = fname;
+
+       req = smb2_create_send(tree, &io);
+       torture_assert_goto(tctx, req != NULL,
+                           ret, done, "smb2_create_send");
+
+       /* Ensure while the open is blocked the smbd is
+          still serving other requests. */
+       io.in.fname = fname1;
+       io.in.create_disposition = NTCREATEX_DISP_CREATE;
+
+       /* Time the start -> end of the request. */
+       start = time(NULL);
+       status = smb2_create(tree, tctx, &io);
+       end = time(NULL);
+
+       /* Should succeed. */
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                       "Error opening the second file\n");
+       h1 = io.out.file.handle;
+
+       /* in less than 2 seconds. Otherwise the server blocks. */
+       torture_assert_goto(tctx, end - start < 2,
+                           ret, done, "server was blocked !");
+
+       /* Pick up the return for the initial blocking open. */
+       status = smb2_create_recv(req, tctx, &io);
+
+       /* Which should also have succeeded. */
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                       "Error opening the file\n");
+       h2 = io.out.file.handle;
+
+       /* Wait for the exit code from the child. */
+       while (child_exit_code == -1) {
+               int rval = tevent_loop_once(tctx->ev);
+               torture_assert_goto(tctx, rval == 0, ret,
+                                   done, "tevent_loop_once error\n");
+       }
+
+       torture_assert_int_equal_goto(tctx, child_exit_code, 0,
+                                     ret, done, "Bad child exit code");
+
+done:
+       if (!smb2_util_handle_empty(h1)) {
+               smb2_util_close(tree, h1);
+       }
+       if (!smb2_util_handle_empty(h2)) {
+               smb2_util_close(tree, h2);
+       }
+       smb2_util_unlink(tree, fname);
+       smb2_util_unlink(tree, fname1);
+       return ret;
+}
+
+struct torture_suite *torture_smb2_kernel_oplocks_init(TALLOC_CTX *ctx)
+{
+       struct torture_suite *suite =
+           torture_suite_create(ctx, "kernel-oplocks");
+
+       torture_suite_add_1smb2_test(suite, "kernel_oplocks1", test_smb2_kernel_oplocks1);
+       torture_suite_add_1smb2_test(suite, "kernel_oplocks2", test_smb2_kernel_oplocks2);
+       torture_suite_add_2smb2_test(suite, "kernel_oplocks3", test_smb2_kernel_oplocks3);
+       torture_suite_add_1smb2_test(suite, "kernel_oplocks4", test_smb2_kernel_oplocks4);
+       torture_suite_add_1smb2_test(suite, "kernel_oplocks5", test_smb2_kernel_oplocks5);
+       torture_suite_add_2smb2_test(suite, "kernel_oplocks6", test_smb2_kernel_oplocks6);
+       torture_suite_add_2smb2_test(suite, "kernel_oplocks7", test_smb2_kernel_oplocks7);
+       torture_suite_add_1smb2_test(suite, "kernel_oplocks8", test_smb2_kernel_oplocks8);
+
+       suite->description = talloc_strdup(suite, "SMB2-KERNEL-OPLOCK tests");
+
+       return suite;
+}