From bd120224dbaf531c1f028ea4a3fc8a0b200dce6a Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Sat, 12 Oct 2024 17:32:01 +0200 Subject: [PATCH] s3/locking: add share_mode_forall() and share_mode_forall_read() varients 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 Reviewed-by: Stefan Metzmacher --- source3/locking/share_mode_lock.c | 46 +++++++++++++++++++++++++------ source3/locking/share_mode_lock.h | 6 +++- source3/smbd/dir.c | 2 +- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/source3/locking/share_mode_lock.c b/source3/locking/share_mode_lock.c index 9bd341ce8aa..d0dc0cb465e 100644 --- a/source3/locking/share_mode_lock.c +++ b/source3/locking/share_mode_lock.c @@ -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( diff --git a/source3/locking/share_mode_lock.h b/source3/locking/share_mode_lock.h index 75ebdf937fe..d24ba5b780e 100644 --- a/source3/locking/share_mode_lock.h +++ b/source3/locking/share_mode_lock.h @@ -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, diff --git a/source3/smbd/dir.c b/source3/smbd/dir.c index 8fb2a3fc016..17e7d0f4ac5 100644 --- a/source3/smbd/dir.c +++ b/source3/smbd/dir.c @@ -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; } -- 2.47.3