From a62eef8c4051f9b3ebcf98d6ababbab805e17ec6 Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Sun, 15 Mar 2020 15:51:18 +0100 Subject: [PATCH 1/5] smbd: remove stat call from mark_file_modified() This stat dates back to d03453864ab1bc5fd3b4a3abaf96176a006c102b where the call to trigger_write_time_update() had been to the file IO codepath. It was present there for other reasons: to setup the write-cache based on the file's size. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14320 Signed-off-by: Ralph Boehme Reviewed-by: Jeremy Allison (cherry picked from commit 2c19d27113036d607850f370bb9afd62856d671e) --- source3/smbd/fileio.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/source3/smbd/fileio.c b/source3/smbd/fileio.c index 029965282f1..2487baeb369 100644 --- a/source3/smbd/fileio.c +++ b/source3/smbd/fileio.c @@ -220,9 +220,6 @@ void mark_file_modified(files_struct *fsp) fsp->modified = true; - if (SMB_VFS_FSTAT(fsp, &fsp->fsp_name->st) != 0) { - return; - } trigger_write_time_update(fsp); if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) { -- 2.20.1 From 21bf905e838a8710348dc0907241c4d724dc197b Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Sat, 14 Mar 2020 16:43:48 +0100 Subject: [PATCH 2/5] torture/smb2: delayed timestamp update test: single write Verify close only updates write-time when a delayed update is actually pending. This scenario is not covered by basic.delaywrite. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14320 Signed-off-by: Ralph Boehme Reviewed-by: Jeremy Allison (cherry picked from commit 58fa7b4fd7b53d3100459a0c9c7ef4ca7481b58a) --- selftest/knownfail.d/samba3.smb2.timestamps | 1 + source4/torture/smb2/timestamps.c | 86 +++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 selftest/knownfail.d/samba3.smb2.timestamps diff --git a/selftest/knownfail.d/samba3.smb2.timestamps b/selftest/knownfail.d/samba3.smb2.timestamps new file mode 100644 index 00000000000..dd4aeb59e2e --- /dev/null +++ b/selftest/knownfail.d/samba3.smb2.timestamps @@ -0,0 +1 @@ +^samba3.smb2.timestamps.delayed-1write\(.*\)$ diff --git a/source4/torture/smb2/timestamps.c b/source4/torture/smb2/timestamps.c index 7f1a54c8ad0..87137d65414 100644 --- a/source4/torture/smb2/timestamps.c +++ b/source4/torture/smb2/timestamps.c @@ -700,6 +700,91 @@ done: return ret; } +static bool test_delayed_1write(struct torture_context *tctx, + struct smb2_tree *tree) +{ + struct smb2_create cr; + struct smb2_handle h1 = {{0}}; + union smb_fileinfo finfo; + struct smb2_close c; + NTTIME create_time; + NTTIME write_time; + NTTIME close_time; + NTSTATUS status; + bool ret = true; + + smb2_deltree(tree, BASEDIR); + status = torture_smb2_testdir(tree, BASEDIR, &h1); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "create failed\n"); + status = smb2_util_close(tree, h1); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "close failed\n"); + + torture_comment(tctx, "Open file-handle 1\n"); + + cr = (struct smb2_create) { + .in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED, + .in.create_disposition = NTCREATEX_DISP_OPEN_IF, + .in.share_access = NTCREATEX_SHARE_ACCESS_MASK, + .in.fname = BASEDIR "\\" FNAME, + }; + status = smb2_create(tree, tctx, &cr); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "create failed\n"); + h1 = cr.out.file.handle; + create_time = cr.out.create_time; + sleep(1); + + torture_comment(tctx, "Write to file-handle 1\n"); + + status = smb2_util_write(tree, h1, "s", 0, 1); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "write failed\n"); + sleep(3); + + torture_comment(tctx, "Check writetime has been updated\n"); + + finfo = (union smb_fileinfo) { + .generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION, + .generic.in.file.handle = h1, + }; + status = smb2_getinfo_file(tree, tree, &finfo); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "getinfo failed\n"); + write_time = finfo.all_info.out.write_time; + + if (!(write_time > create_time)) { + ret = false; + torture_fail_goto(tctx, done, + "Write-time not updated (wrong!)\n"); + } + + torture_comment(tctx, "Close file-handle 1\n"); + sleep(1); + + c = (struct smb2_close) { + .in.file.handle = h1, + .in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION, + }; + + status = smb2_close(tree, &c); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "close failed\n"); + ZERO_STRUCT(h1); + close_time = c.out.write_time; + + torture_assert_nttime_equal(tctx, close_time, write_time, + "Writetime != close_time (wrong!)\n"); + +done: + if (!smb2_util_handle_empty(h1)) { + smb2_util_close(tree, h1); + } + smb2_deltree(tree, BASEDIR); + return ret; +} + /* basic testing of SMB2 timestamps */ @@ -722,6 +807,7 @@ struct torture_suite *torture_smb2_timestamps_init(TALLOC_CTX *ctx) torture_suite_add_1smb2_test(suite, "delayed-write-vs-seteof", test_delayed_write_vs_seteof); torture_suite_add_1smb2_test(suite, "delayed-write-vs-flush", test_delayed_write_vs_flush); torture_suite_add_1smb2_test(suite, "delayed-write-vs-setbasic", test_delayed_write_vs_setbasic); + torture_suite_add_1smb2_test(suite, "delayed-1write", test_delayed_1write); suite->description = talloc_strdup(suite, "SMB2 timestamp tests"); -- 2.20.1 From eb2eeda8e4dad59c8f0065e310314ecf11669938 Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Sun, 15 Mar 2020 16:46:16 +0100 Subject: [PATCH 3/5] torture/smb2: delayed timestamp updates test: more then one write Verify a close updates the write-time for subsequent writes after an initial write started the delayed update logic. This covers a scenario that will become relevant with the two subsequent commits. The next commit: smbd: let mark_file_modified() always call trigger_write_time_update() ensures that trigger_write_time_update() is not only called for the first write on a file. Without that preaparatory change, the second commit: smbd: let delayed update handler also update on-disk timestamps alone would cause this test to fail. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14320 Signed-off-by: Ralph Boehme Reviewed-by: Jeremy Allison (cherry picked from commit 60aecca9a727555847aa1412415c5bbd927df4ff) --- source4/torture/smb2/timestamps.c | 116 ++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/source4/torture/smb2/timestamps.c b/source4/torture/smb2/timestamps.c index 87137d65414..89b64886e4d 100644 --- a/source4/torture/smb2/timestamps.c +++ b/source4/torture/smb2/timestamps.c @@ -785,6 +785,121 @@ done: return ret; } +static bool test_delayed_2write(struct torture_context *tctx, + struct smb2_tree *tree) +{ + struct smb2_create cr; + struct smb2_handle h1 = {{0}}; + union smb_fileinfo finfo; + struct smb2_close c; + NTTIME create_time; + NTTIME write_time; + NTTIME write_time2; + struct timespec now; + NTTIME send_close_time; + NTTIME close_time; + NTSTATUS status; + bool ret = true; + + smb2_deltree(tree, BASEDIR); + status = torture_smb2_testdir(tree, BASEDIR, &h1); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "create failed\n"); + status = smb2_util_close(tree, h1); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "close failed\n"); + + torture_comment(tctx, "Open file\n"); + + cr = (struct smb2_create) { + .in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED, + .in.create_disposition = NTCREATEX_DISP_OPEN_IF, + .in.share_access = NTCREATEX_SHARE_ACCESS_MASK, + .in.fname = BASEDIR "\\" FNAME, + }; + status = smb2_create(tree, tctx, &cr); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "create failed\n"); + h1 = cr.out.file.handle; + create_time = cr.out.create_time; + sleep(1); + + torture_comment(tctx, "Write to file\n"); + + status = smb2_util_write(tree, h1, "s", 0, 1); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "write failed\n"); + sleep(3); + + torture_comment(tctx, "Check writetime has been updated\n"); + + finfo = (union smb_fileinfo) { + .generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION, + .generic.in.file.handle = h1, + }; + status = smb2_getinfo_file(tree, tree, &finfo); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "getinfo failed\n"); + write_time = finfo.all_info.out.write_time; + + if (!(write_time > create_time)) { + ret = false; + torture_fail_goto(tctx, done, + "Write-time not updated (wrong!)\n"); + } + + torture_comment(tctx, "Write a second time\n"); + + status = smb2_util_write(tree, h1, "s", 0, 1); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "write failed\n"); + sleep(3); + + torture_comment(tctx, "Check writetime has NOT been updated\n"); + + finfo = (union smb_fileinfo) { + .generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION, + .generic.in.file.handle = h1, + }; + status = smb2_getinfo_file(tree, tree, &finfo); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "getinfo failed\n"); + write_time2 = finfo.all_info.out.write_time; + + torture_assert_nttime_equal(tctx, write_time2, write_time, + "second write updated write-time (wrong!)\n"); + + torture_comment(tctx, "Close file-handle 1\n"); + sleep(2); + + now = timespec_current(); + send_close_time = full_timespec_to_nt_time(&now); + + c = (struct smb2_close) { + .in.file.handle = h1, + .in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION, + }; + + status = smb2_close(tree, &c); + torture_assert_ntstatus_ok_goto(tctx, status, ret, done, + "close failed\n"); + ZERO_STRUCT(h1); + close_time = c.out.write_time; + + if (!(close_time > send_close_time)) { + ret = false; + torture_fail_goto(tctx, done, + "Write-time not updated (wrong!)\n"); + } + +done: + if (!smb2_util_handle_empty(h1)) { + smb2_util_close(tree, h1); + } + smb2_deltree(tree, BASEDIR); + return ret; +} + /* basic testing of SMB2 timestamps */ @@ -808,6 +923,7 @@ struct torture_suite *torture_smb2_timestamps_init(TALLOC_CTX *ctx) torture_suite_add_1smb2_test(suite, "delayed-write-vs-flush", test_delayed_write_vs_flush); torture_suite_add_1smb2_test(suite, "delayed-write-vs-setbasic", test_delayed_write_vs_setbasic); torture_suite_add_1smb2_test(suite, "delayed-1write", test_delayed_1write); + torture_suite_add_1smb2_test(suite, "delayed-2write", test_delayed_2write); suite->description = talloc_strdup(suite, "SMB2 timestamp tests"); -- 2.20.1 From 7232e349d54b4c2d4413315a0a305549c699533c Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Sun, 15 Mar 2020 08:30:21 +0100 Subject: [PATCH 4/5] smbd: let mark_file_modified() always call trigger_write_time_update() Preperatory change: the next commit will reset fsp->update_write_time_on_close in the event handler, so this change ensures it gets set again for any subsequent write. This will NOT always result in a write-time update because trigger_write_time_update() has its own only-once logic using the internal variable fsp->update_write_time_triggered. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14320 Signed-off-by: Ralph Boehme Reviewed-by: Jeremy Allison (cherry picked from commit 53de2da7acfc24513082190502d93306c12b7434) --- source3/smbd/fileio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source3/smbd/fileio.c b/source3/smbd/fileio.c index 2487baeb369..aba7f878b17 100644 --- a/source3/smbd/fileio.c +++ b/source3/smbd/fileio.c @@ -214,14 +214,14 @@ void mark_file_modified(files_struct *fsp) { int dosmode; + trigger_write_time_update(fsp); + if (fsp->modified) { return; } fsp->modified = true; - trigger_write_time_update(fsp); - if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) { return; } -- 2.20.1 From ac178219ed0ab710b79ab14604bbade234cf483b Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Sun, 15 Mar 2020 08:30:21 +0100 Subject: [PATCH 5/5] smbd: let delayed update handler also update on-disk timestamps Let delayed update handler also update on-disk timestamps by calling trigger_write_time_update_immediate(). trigger_write_time_update_immediate() sets fsp->update_write_time_on_close to false which prevents updating the write-time on close if there was ever only one write to the file. Besides resetting fsp->update_write_time_on_close and setting the on-disk timestamps trigger_write_time_update_immediate() takes the same steps as the removed code. BUG: https://bugzilla.samba.org/show_bug.cgi?id=14320 Signed-off-by: Ralph Boehme Reviewed-by: Jeremy Allison Autobuild-User(master): Jeremy Allison Autobuild-Date(master): Thu Mar 19 03:05:40 UTC 2020 on sn-devel-184 (cherry picked from commit 81c1a14e3271aeed7ed4fe6311171b19ba963555) --- selftest/knownfail.d/samba3.smb2.timestamps | 1 - source3/smbd/fileio.c | 10 +--------- 2 files changed, 1 insertion(+), 10 deletions(-) delete mode 100644 selftest/knownfail.d/samba3.smb2.timestamps diff --git a/selftest/knownfail.d/samba3.smb2.timestamps b/selftest/knownfail.d/samba3.smb2.timestamps deleted file mode 100644 index dd4aeb59e2e..00000000000 --- a/selftest/knownfail.d/samba3.smb2.timestamps +++ /dev/null @@ -1 +0,0 @@ -^samba3.smb2.timestamps.delayed-1write\(.*\)$ diff --git a/source3/smbd/fileio.c b/source3/smbd/fileio.c index aba7f878b17..31d5b7510b7 100644 --- a/source3/smbd/fileio.c +++ b/source3/smbd/fileio.c @@ -104,15 +104,7 @@ void fsp_flush_write_time_update(struct files_struct *fsp) DEBUG(5, ("Update write time on %s\n", fsp_str_dbg(fsp))); - /* change the write time in the open file db. */ - (void)set_write_time(fsp->file_id, timespec_current()); - - /* And notify. */ - notify_fname(fsp->conn, NOTIFY_ACTION_MODIFIED, - FILE_NOTIFY_CHANGE_LAST_WRITE, fsp->fsp_name->base_name); - - /* Remove the timed event handler. */ - TALLOC_FREE(fsp->update_write_time_event); + trigger_write_time_update_immediate(fsp); } static void update_write_time_handler(struct tevent_context *ctx, -- 2.20.1