From 8c79959935ca67a0cc198e0cb690baae4c46737c Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Mon, 21 Nov 2016 19:12:12 +0000 Subject: [PATCH] storage: Reuse already open caller objects In the event that the remote crashes, or for other reasons try to open a partition that we already have open we should reuse the caller objects, so that we don't run out of them. This should likely be replaced by a working mechanism for notifications when the remote is going away. Signed-off-by: Bjorn Andersson --- storage.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/storage.c b/storage.c index 9d03ccb..997b428 100644 --- a/storage.c +++ b/storage.c @@ -19,6 +19,7 @@ struct caller { unsigned node; int fd; unsigned dev_error; + const struct partition *partition; }; static const struct partition partition_table[] = { @@ -46,7 +47,7 @@ int storage_open(void) int storage_get(unsigned node, const char *path) { const struct partition *part; - struct caller *caller; + struct caller *caller = NULL; int saved_errno; int fd; int i; @@ -60,6 +61,14 @@ int storage_get(unsigned node, const char *path) return -EPERM; found: + /* Check if this node already has the requested path open */ + for (i = 0; i < MAX_CALLERS; i++) { + if (caller_handles[i].fd != -1 && + caller_handles[i].node == node && + caller_handles[i].partition == part) + return caller_handles[i].id; + } + for (i = 0; i < MAX_CALLERS; i++) { if (caller_handles[i].fd == -1) { caller = &caller_handles[i]; @@ -81,6 +90,7 @@ found: caller->node = node; caller->fd = fd; + caller->partition = part; return caller->id; } @@ -98,6 +108,8 @@ int storage_put(unsigned node, int caller_id) close(caller->fd); caller->fd = -1; + caller->partition = NULL; + return 0; }