From 2ad61bf01b2f3b5ca86ceccf8aeb96b7dbf6251a Mon Sep 17 00:00:00 2001 From: Jay Monkman Date: Wed, 16 Nov 2022 15:56:09 -0600 Subject: [PATCH] storageproxyd: Added support for getting max size of file This handles the STORAGE_FILE_GET_MAX_SIZE. The new behavior will return a max size of 0x10000000000 (former default value in Trusty) for a regular file and the partition size for a block device. Test: N/A Bug: 247003431 Change-Id: Ib8b8504b63496d64487cf2f96a1c0758bfafdd97 Signed-off-by: Jay Monkman --- trusty/storage/proxy/proxy.c | 4 ++++ trusty/storage/proxy/storage.c | 43 ++++++++++++++++++++++++++++++++++ trusty/storage/proxy/storage.h | 2 ++ 3 files changed, 49 insertions(+) diff --git a/trusty/storage/proxy/proxy.c b/trusty/storage/proxy/proxy.c index b970406d9..4f77fa2ea 100644 --- a/trusty/storage/proxy/proxy.c +++ b/trusty/storage/proxy/proxy.c @@ -136,6 +136,10 @@ static int handle_req(struct storage_msg* msg, const void* req, size_t req_len) rc = storage_file_set_size(msg, req, req_len); break; + case STORAGE_FILE_GET_MAX_SIZE: + rc = storage_file_get_max_size(msg, req, req_len); + break; + case STORAGE_RPMB_SEND: rc = rpmb_send(msg, req, req_len); break; diff --git a/trusty/storage/proxy/storage.c b/trusty/storage/proxy/storage.c index 033dc2117..a96ddcbd9 100644 --- a/trusty/storage/proxy/storage.c +++ b/trusty/storage/proxy/storage.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,9 @@ #define ALTERNATE_DATA_DIR "alternate/" +/* Maximum file size for filesystem backed storage (i.e. not block dev backed storage) */ +#define MAX_FILE_SIZE (0x10000000000) + enum sync_state { SS_UNUSED = -1, SS_CLEAN = 0, @@ -549,6 +553,45 @@ err_response: return ipc_respond(msg, NULL, 0); } +int storage_file_get_max_size(struct storage_msg* msg, const void* r, size_t req_len) { + const struct storage_file_get_max_size_req* req = r; + struct storage_file_get_max_size_resp resp = {0}; + uint64_t max_size = 0; + + if (req_len != sizeof(*req)) { + ALOGE("%s: invalid request length (%zd != %zd)\n", __func__, req_len, sizeof(*req)); + msg->result = STORAGE_ERR_NOT_VALID; + goto err_response; + } + + struct stat stat; + int fd = lookup_fd(req->handle, false); + int rc = fstat(fd, &stat); + if (rc < 0) { + ALOGE("%s: error stat'ing file (fd=%d): %s\n", __func__, fd, strerror(errno)); + goto err_response; + } + + if ((stat.st_mode & S_IFMT) == S_IFBLK) { + rc = ioctl(fd, BLKGETSIZE64, &max_size); + if (rc < 0) { + rc = errno; + ALOGE("%s: error calling ioctl on file (fd=%d): %s\n", __func__, fd, strerror(errno)); + msg->result = translate_errno(rc); + goto err_response; + } + } else { + max_size = MAX_FILE_SIZE; + } + + resp.max_size = max_size; + msg->result = STORAGE_NO_ERROR; + return ipc_respond(msg, &resp, sizeof(resp)); + +err_response: + return ipc_respond(msg, NULL, 0); +} + int storage_init(const char *dirname) { /* If there is an active DSU image, use the alternate fs mode. */ diff --git a/trusty/storage/proxy/storage.h b/trusty/storage/proxy/storage.h index 5a670d4b7..77bfa13fb 100644 --- a/trusty/storage/proxy/storage.h +++ b/trusty/storage/proxy/storage.h @@ -39,6 +39,8 @@ int storage_file_get_size(struct storage_msg *msg, int storage_file_set_size(struct storage_msg *msg, const void *req, size_t req_len); +int storage_file_get_max_size(struct storage_msg* msg, const void* req, size_t req_len); + int storage_init(const char *dirname); int storage_sync_checkpoint(void);