]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
smbd: avoid clobbering the file size, use info instead
authorRalph Boehme <slow@samba.org>
Wed, 24 Sep 2025 17:30:08 +0000 (19:30 +0200)
committerVolker Lendecke <vl@samba.org>
Fri, 10 Oct 2025 15:59:35 +0000 (15:59 +0000)
This is a better implementation of 215b2c741a93023d13e8a9f82739ac3e91b64a66 and
also prepares for future changes where I'll need the current size.

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
source3/smbd/open.c

index 00bfa3e003ed8ecd7f0c76086de8605cbfab6a83..91abe9a5c24af8374525862234603a0ac8c6e79f 100644 (file)
@@ -2130,6 +2130,7 @@ struct delay_for_oplock_state {
        bool will_overwrite;
        uint32_t delay_mask;
        bool first_open_attempt;
+       int info;
        bool got_handle_lease;
        bool got_oplock;
        bool disallow_write_lease;
@@ -2373,6 +2374,7 @@ static NTSTATUS delay_for_oplock(files_struct *fsp,
                                 bool have_sharing_violation,
                                 uint32_t create_disposition,
                                 bool first_open_attempt,
+                                int info,
                                 int *poplock_type,
                                 uint32_t *pgranted,
                                 struct blocker_debug_state **blocker_debug_state)
@@ -2381,6 +2383,7 @@ static NTSTATUS delay_for_oplock(files_struct *fsp,
                .fsp = fsp,
                .lease = lease,
                .first_open_attempt = first_open_attempt,
+               .info = info,
        };
        uint32_t requested;
        uint32_t granted;
@@ -2486,7 +2489,7 @@ grant:
                }
        }
 
-       if (file_has_brlocks(fsp)) {
+       if (info != FILE_WAS_OVERWRITTEN && file_has_brlocks(fsp)) {
                DBG_DEBUG("file %s has byte range locks\n",
                          fsp_str_dbg(fsp));
                granted &= ~(SMB2_LEASE_READ | SMB2_LEASE_HANDLE);
@@ -2571,6 +2574,7 @@ static NTSTATUS handle_share_mode_lease(
        int oplock_request,
        const struct smb2_lease *lease,
        bool first_open_attempt,
+       int info,
        int *poplock_type,
        uint32_t *pgranted,
        struct blocker_debug_state **blocker_debug_state)
@@ -2623,6 +2627,7 @@ static NTSTATUS handle_share_mode_lease(
                sharing_violation,
                create_disposition,
                first_open_attempt,
+               info,
                poplock_type,
                pgranted,
                blocker_debug_state);
@@ -3071,7 +3076,8 @@ static NTSTATUS check_and_store_share_mode(
        uint32_t share_access,
        int oplock_request,
        const struct smb2_lease *lease,
-       bool first_open_attempt)
+       bool first_open_attempt,
+       int info)
 {
        NTSTATUS status;
        int oplock_type = NO_OPLOCK;
@@ -3099,6 +3105,7 @@ static NTSTATUS check_and_store_share_mode(
                                         oplock_request,
                                         lease,
                                         first_open_attempt,
+                                        info,
                                         &oplock_type,
                                         &granted_lease,
                                         &blocker_debug_state);
@@ -3429,6 +3436,7 @@ struct open_ntcreate_lock_state {
        int oplock_request;
        const struct smb2_lease *lease;
        bool first_open_attempt;
+       int info;
        bool keep_locked;
        NTSTATUS status;
        share_mode_entry_prepare_unlock_fn_t cleanup_fn;
@@ -3456,7 +3464,8 @@ static void open_ntcreate_lock_add_entry(struct share_mode_lock *lck,
                                                   state->share_access,
                                                   state->oplock_request,
                                                   state->lease,
-                                                  state->first_open_attempt);
+                                                  state->first_open_attempt,
+                                                  state->info);
        if (!NT_STATUS_IS_OK(state->status)) {
                return;
        }
@@ -4063,15 +4072,6 @@ static NTSTATUS open_file_ntcreate(connection_struct *conn,
        } else {
                if (flags & O_TRUNC) {
                        info = FILE_WAS_OVERWRITTEN;
-                       /*
-                        * We did not truncate the file yet, we're doing that
-                        * explicitly with SMB_VFS_FTRUNCATE() below under the
-                        * sharemode glock. For correct handling of RH leases in
-                        * the presence of byterange locks, the leases code
-                        * needs the "correct" filesize which should be 0 at
-                        * this place if we did the O_TRUNC at open() time.
-                        */
-                       fsp->fsp_name->st.st_ex_size = 0;
                } else {
                        info = FILE_WAS_OPENED;
                }
@@ -4100,6 +4100,7 @@ static NTSTATUS open_file_ntcreate(connection_struct *conn,
                .oplock_request         = oplock_request,
                .lease                  = lease,
                .first_open_attempt     = first_open_attempt,
+               .info                   = info,
                .keep_locked            = keep_locked,
        };