]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s3/locking: add share_mode_forall() and share_mode_forall_read() varients
authorRalph Boehme <slow@samba.org>
Sat, 12 Oct 2024 15:32:01 +0000 (17:32 +0200)
committerRalph Boehme <slow@samba.org>
Tue, 5 Nov 2024 14:39:30 +0000 (14:39 +0000)
All existing callers use share_mode_forall_read(), so no change in behaviour.

Note: doing the indirection via the function pointers "ro_fn" and "rw_fn" in a
single state "struct share_mode_forall_state" avoids duplicating
share_mode_forall_dump_fn() and share_mode_forall_fn() and has the benefit of
code sharing of these functions for both read-only and read-write cases.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15608

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
source3/locking/share_mode_lock.c
source3/locking/share_mode_lock.h
source3/smbd/dir.c

index 9bd341ce8aa1484f3f23e25668069517bdaad14c..d0dc0cb465e382a76157bc335bfbce60c771167b 100644 (file)
@@ -1743,9 +1743,12 @@ NTSTATUS fetch_share_mode_recv(struct tevent_req *req,
 
 struct share_mode_forall_state {
        TDB_DATA key;
-       int (*fn)(struct file_id fid,
-                 const struct share_mode_data *data,
-                 void *private_data);
+       int (*ro_fn)(struct file_id fid,
+                    const struct share_mode_data *data,
+                    void *private_data);
+       int (*rw_fn)(struct file_id fid,
+                    struct share_mode_data *data,
+                    void *private_data);
        void *private_data;
 };
 
@@ -1785,7 +1788,11 @@ static void share_mode_forall_dump_fn(
                return;
        }
 
-       state->fn(fid, d, state->private_data);
+       if (state->ro_fn != NULL) {
+               state->ro_fn(fid, d, state->private_data);
+       } else {
+               state->rw_fn(fid, d, state->private_data);
+       }
        TALLOC_FREE(d);
 }
 
@@ -1806,13 +1813,36 @@ static int share_mode_forall_fn(TDB_DATA key, void *private_data)
        return 0;
 }
 
+int share_mode_forall_read(int (*fn)(struct file_id fid,
+                                    const struct share_mode_data *data,
+                                    void *private_data),
+                          void *private_data)
+{
+       struct share_mode_forall_state state = {
+               .ro_fn = fn,
+               .private_data = private_data
+       };
+       int ret;
+
+       if (lock_ctx == NULL) {
+               return 0;
+       }
+
+       ret = g_lock_locks_read(
+               lock_ctx, share_mode_forall_fn, &state);
+       if (ret < 0) {
+               DBG_ERR("g_lock_locks failed\n");
+       }
+       return ret;
+}
+
 int share_mode_forall(int (*fn)(struct file_id fid,
-                               const struct share_mode_data *data,
+                               struct share_mode_data *data,
                                void *private_data),
                      void *private_data)
 {
        struct share_mode_forall_state state = {
-               .fn = fn,
+               .rw_fn = fn,
                .private_data = private_data
        };
        int ret;
@@ -1821,7 +1851,7 @@ int share_mode_forall(int (*fn)(struct file_id fid,
                return 0;
        }
 
-       ret = g_lock_locks_read(
+       ret = g_lock_locks(
                lock_ctx, share_mode_forall_fn, &state);
        if (ret < 0) {
                DBG_ERR("g_lock_locks failed\n");
@@ -1890,7 +1920,7 @@ int share_entry_forall(int (*fn)(struct file_id fid,
        struct share_entry_forall_state state = {
                .fn = fn, .private_data = private_data };
 
-       return share_mode_forall(share_entry_traverse_fn, &state);
+       return share_mode_forall_read(share_entry_traverse_fn, &state);
 }
 
 static int share_mode_entry_cmp(
index 75ebdf937febf82c7c3012403b0bdcf15f0fac4b..d24ba5b780e8131f80816da703e27ea694b21a78 100644 (file)
@@ -93,9 +93,13 @@ int share_entry_forall(
 NTSTATUS share_mode_count_entries(struct file_id fid, size_t *num_share_modes);
 int share_mode_forall(
        int (*fn)(struct file_id fid,
-                 const struct share_mode_data *data,
+                 struct share_mode_data *data,
                  void *private_data),
        void *private_data);
+int share_mode_forall_read(int (*fn)(struct file_id fid,
+                                    const struct share_mode_data *data,
+                                    void *private_data),
+                          void *private_data);
 bool share_mode_forall_entries(
        struct share_mode_lock *lck,
        bool (*fn)(struct share_mode_entry *e,
index 8fb2a3fc016c7aa3347aeb67daa457ab4543f476..17e7d0f4ac54c469f3d928c7e515514325b6de93 100644 (file)
@@ -1331,7 +1331,7 @@ static int files_below_forall(connection_struct *conn,
                return -1;
        }
 
-       ret = share_mode_forall(files_below_forall_fn, &state);
+       ret = share_mode_forall_read(files_below_forall_fn, &state);
        TALLOC_FREE(to_free);
        return ret;
 }