X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Ffd-tracker%2Finode.cpp;h=6555e97a311b10067bed7a495d49f17f58628dd5;hb=cd9adb8b829564212158943a0d279bb35322ab30;hp=b5cdcf1b27b583dd3e67f004db62ba214057281d;hpb=c9e313bc594f40a86eed237dce222c0fc99c957f;p=lttng-tools.git diff --git a/src/common/fd-tracker/inode.cpp b/src/common/fd-tracker/inode.cpp index b5cdcf1b2..6555e97a3 100644 --- a/src/common/fd-tracker/inode.cpp +++ b/src/common/fd-tracker/inode.cpp @@ -5,6 +5,8 @@ * */ +#include "inode.hpp" + #include #include #include @@ -12,8 +14,10 @@ #include #include #include -#include + #include + +#include #include #include #include @@ -21,12 +25,12 @@ #include #include -#include "inode.hpp" - +namespace { struct inode_id { dev_t device; ino_t inode; }; +} /* namespace */ struct lttng_inode_registry { /* Hashtable of inode_id to lttng_inode. */ @@ -60,52 +64,50 @@ struct lttng_unlinked_file_pool { unsigned int next_id; }; -static struct { +namespace { +struct { pthread_mutex_t lock; bool initialized; unsigned long value; } seed = { - .lock = PTHREAD_MUTEX_INITIALIZER, - .initialized = false, - .value = 0, + .lock = PTHREAD_MUTEX_INITIALIZER, + .initialized = false, + .value = 0, }; +} /* namespace */ static unsigned long lttng_inode_id_hash(const struct inode_id *id) { uint64_t device = id->device, inode_no = id->inode; - return hash_key_u64(&device, seed.value) ^ - hash_key_u64(&inode_no, seed.value); + return hash_key_u64(&device, seed.value) ^ hash_key_u64(&inode_no, seed.value); } static int lttng_inode_match(struct cds_lfht_node *node, const void *key) { const struct inode_id *id = (inode_id *) key; - const struct lttng_inode *inode = caa_container_of( - node, struct lttng_inode, registry_node); + const struct lttng_inode *inode = + lttng::utils::container_of(node, <tng_inode::registry_node); return inode->id.device == id->device && inode->id.inode == id->inode; } static void lttng_inode_free(struct rcu_head *head) { - struct lttng_inode *inode = - caa_container_of(head, struct lttng_inode, rcu_head); + struct lttng_inode *inode = lttng::utils::container_of(head, <tng_inode::rcu_head); free(inode); } -static int lttng_unlinked_file_pool_add_inode( - struct lttng_unlinked_file_pool *pool, - struct lttng_inode *inode) +static int lttng_unlinked_file_pool_add_inode(struct lttng_unlinked_file_pool *pool, + struct lttng_inode *inode) { int ret; const unsigned int unlinked_id = pool->next_id++; char *inode_unlinked_name; bool reference_acquired; - DBG("Adding inode of %s to unlinked file pool as id %u", - inode->location.path, unlinked_id); + DBG("Adding inode of %s to unlinked file pool as id %u", inode->location.path, unlinked_id); ret = asprintf(&inode_unlinked_name, "%u", unlinked_id); if (ret < 0) { ERR("Failed to format unlinked inode name"); @@ -114,11 +116,9 @@ static int lttng_unlinked_file_pool_add_inode( } if (pool->file_count == 0) { - DBG("Creating unlinked files directory at %s", - pool->unlink_directory_path); + DBG("Creating unlinked files directory at %s", pool->unlink_directory_path); LTTNG_ASSERT(!pool->unlink_directory_handle); - ret = utils_mkdir(pool->unlink_directory_path, - S_IRWXU | S_IRWXG, -1, -1); + ret = utils_mkdir(pool->unlink_directory_path, S_IRWXU | S_IRWXG, -1, -1); if (ret) { if (errno == EEXIST) { /* @@ -126,40 +126,40 @@ static int lttng_unlinked_file_pool_add_inode( * error. */ DBG("Unlinked file directory \"%s\" already exists", - pool->unlink_directory_path); + pool->unlink_directory_path); } else { PERROR("Failed to create unlinked files directory at %s", - pool->unlink_directory_path); + pool->unlink_directory_path); goto end; } } - pool->unlink_directory_handle = lttng_directory_handle_create( - pool->unlink_directory_path); + pool->unlink_directory_handle = + lttng_directory_handle_create(pool->unlink_directory_path); if (!pool->unlink_directory_handle) { ERR("Failed to create directory handle to unlinked file pool at %s", - pool->unlink_directory_path); + pool->unlink_directory_path); ret = -1; goto end; } } ret = lttng_directory_handle_rename(inode->location.directory_handle, - inode->location.path, pool->unlink_directory_handle, - inode_unlinked_name); + inode->location.path, + pool->unlink_directory_handle, + inode_unlinked_name); if (ret) { goto end; } lttng_directory_handle_put(inode->location.directory_handle); - inode->location.directory_handle = NULL; - reference_acquired = lttng_directory_handle_get( - pool->unlink_directory_handle); + inode->location.directory_handle = nullptr; + reference_acquired = lttng_directory_handle_get(pool->unlink_directory_handle); LTTNG_ASSERT(reference_acquired); inode->location.directory_handle = pool->unlink_directory_handle; free(inode->location.path); inode->location.path = inode_unlinked_name; - inode_unlinked_name = NULL; + inode_unlinked_name = nullptr; LTTNG_OPTIONAL_SET(&inode->unlinked_id, unlinked_id); pool->file_count++; end: @@ -167,26 +167,25 @@ end: return ret; } -static int lttng_unlinked_file_pool_remove_inode( - struct lttng_unlinked_file_pool *pool, - struct lttng_inode *inode) +static int lttng_unlinked_file_pool_remove_inode(struct lttng_unlinked_file_pool *pool, + struct lttng_inode *inode) { int ret; DBG("Removing inode with unlinked id %u from unlinked file pool", - LTTNG_OPTIONAL_GET(inode->unlinked_id)); + LTTNG_OPTIONAL_GET(inode->unlinked_id)); - ret = lttng_directory_handle_unlink_file( - inode->location.directory_handle, inode->location.path); + ret = lttng_directory_handle_unlink_file(inode->location.directory_handle, + inode->location.path); if (ret) { PERROR("Failed to unlink file %s from unlinked file directory", - inode->location.path); + inode->location.path); goto end; } free(inode->location.path); - inode->location.path = NULL; + inode->location.path = nullptr; lttng_directory_handle_put(inode->location.directory_handle); - inode->location.directory_handle = NULL; + inode->location.directory_handle = nullptr; pool->file_count--; if (pool->file_count == 0) { @@ -197,10 +196,10 @@ static int lttng_unlinked_file_pool_remove_inode( * error except through logging. */ PERROR("Failed to remove unlinked files directory at %s", - pool->unlink_directory_path); + pool->unlink_directory_path); } lttng_directory_handle_put(pool->unlink_directory_handle); - pool->unlink_directory_handle = NULL; + pool->unlink_directory_handle = nullptr; } end: return ret; @@ -221,25 +220,23 @@ static void lttng_inode_destroy(struct lttng_inode *inode) LTTNG_ASSERT(inode->location.directory_handle); LTTNG_ASSERT(inode->location.path); - DBG("Removing %s from unlinked file pool", - inode->location.path); + DBG("Removing %s from unlinked file pool", inode->location.path); ret = lttng_unlinked_file_pool_remove_inode(inode->unlinked_file_pool, inode); if (ret) { PERROR("Failed to unlink %s", inode->location.path); } } - lttng_directory_handle_put( - inode->location.directory_handle); - inode->location.directory_handle = NULL; + lttng_directory_handle_put(inode->location.directory_handle); + inode->location.directory_handle = nullptr; free(inode->location.path); - inode->location.path = NULL; + inode->location.path = nullptr; call_rcu(&inode->rcu_head, lttng_inode_free); } static void lttng_inode_release(struct urcu_ref *ref) { - lttng_inode_destroy(caa_container_of(ref, struct lttng_inode, ref)); + lttng_inode_destroy(lttng::utils::container_of(ref, <tng_inode::ref)); } static void lttng_inode_get(struct lttng_inode *inode) @@ -247,10 +244,9 @@ static void lttng_inode_get(struct lttng_inode *inode) urcu_ref_get(&inode->ref); } -struct lttng_unlinked_file_pool *lttng_unlinked_file_pool_create( - const char *path) +struct lttng_unlinked_file_pool *lttng_unlinked_file_pool_create(const char *path) { - struct lttng_unlinked_file_pool *pool = (lttng_unlinked_file_pool *) zmalloc(sizeof(*pool)); + struct lttng_unlinked_file_pool *pool = zmalloc(); if (!pool) { goto error; @@ -258,7 +254,7 @@ struct lttng_unlinked_file_pool *lttng_unlinked_file_pool_create( if (!path || *path != '/') { ERR("Unlinked file pool must be created with an absolute path, path = \"%s\"", - path ? path : "NULL"); + path ? path : "NULL"); goto error; } @@ -271,11 +267,10 @@ struct lttng_unlinked_file_pool *lttng_unlinked_file_pool_create( return pool; error: lttng_unlinked_file_pool_destroy(pool); - return NULL; + return nullptr; } -void lttng_unlinked_file_pool_destroy( - struct lttng_unlinked_file_pool *pool) +void lttng_unlinked_file_pool_destroy(struct lttng_unlinked_file_pool *pool) { if (!pool) { return; @@ -292,13 +287,11 @@ void lttng_inode_put(struct lttng_inode *inode) urcu_ref_put(&inode->ref, lttng_inode_release); } -struct lttng_directory_handle * -lttng_inode_get_location_directory_handle( - struct lttng_inode *inode) +struct lttng_directory_handle *lttng_inode_get_location_directory_handle(struct lttng_inode *inode) { if (inode->location.directory_handle) { - const bool reference_acquired = lttng_directory_handle_get( - inode->location.directory_handle); + const bool reference_acquired = + lttng_directory_handle_get(inode->location.directory_handle); LTTNG_ASSERT(reference_acquired); } @@ -306,8 +299,8 @@ lttng_inode_get_location_directory_handle( } void lttng_inode_borrow_location(struct lttng_inode *inode, - const struct lttng_directory_handle **out_directory_handle, - const char **out_path) + const struct lttng_directory_handle **out_directory_handle, + const char **out_path) { if (out_directory_handle) { *out_directory_handle = inode->location.directory_handle; @@ -317,24 +310,23 @@ void lttng_inode_borrow_location(struct lttng_inode *inode, } } -int lttng_inode_rename( - struct lttng_inode *inode, - struct lttng_directory_handle *old_directory_handle, - const char *old_path, - struct lttng_directory_handle *new_directory_handle, - const char *new_path, - bool overwrite) +int lttng_inode_rename(struct lttng_inode *inode, + struct lttng_directory_handle *old_directory_handle, + const char *old_path, + struct lttng_directory_handle *new_directory_handle, + const char *new_path, + bool overwrite) { int ret = 0; char *new_path_copy = strdup(new_path); bool reference_acquired; DBG("Performing rename of inode from %s to %s with %s directory handles", - old_path, new_path, - lttng_directory_handle_equals(old_directory_handle, - new_directory_handle) ? - "identical" : - "different"); + old_path, + new_path, + lttng_directory_handle_equals(old_directory_handle, new_directory_handle) ? + "identical" : + "different"); if (!new_path_copy) { ret = -ENOMEM; @@ -343,7 +335,8 @@ int lttng_inode_rename( if (inode->unlink_pending) { WARN("An attempt to rename an unlinked file from %s to %s has been performed", - old_path, new_path); + old_path, + new_path); ret = -ENOENT; goto end; } @@ -352,11 +345,9 @@ int lttng_inode_rename( /* Verify that file doesn't exist. */ struct stat statbuf; - ret = lttng_directory_handle_stat( - new_directory_handle, new_path, &statbuf); + ret = lttng_directory_handle_stat(new_directory_handle, new_path, &statbuf); if (ret == 0) { - ERR("Refusing to rename %s as the destination already exists", - old_path); + ERR("Refusing to rename %s as the destination already exists", old_path); ret = -EEXIST; goto end; } else if (ret < 0 && errno != ENOENT) { @@ -366,8 +357,8 @@ int lttng_inode_rename( } } - ret = lttng_directory_handle_rename(old_directory_handle, old_path, - new_directory_handle, new_path); + ret = lttng_directory_handle_rename( + old_directory_handle, old_path, new_directory_handle, new_path); if (ret) { PERROR("Failed to rename file %s to %s", old_path, new_path); ret = -errno; @@ -381,7 +372,7 @@ int lttng_inode_rename( inode->location.directory_handle = new_directory_handle; /* Ownership transferred. */ inode->location.path = new_path_copy; - new_path_copy = NULL; + new_path_copy = nullptr; end: free(new_path_copy); return ret; @@ -395,7 +386,7 @@ int lttng_inode_unlink(struct lttng_inode *inode) if (inode->unlink_pending) { WARN("An attempt to re-unlink %s has been performed, ignoring.", - inode->location.path); + inode->location.path); ret = -ENOENT; goto end; } @@ -404,11 +395,10 @@ int lttng_inode_unlink(struct lttng_inode *inode) * Move to the temporary "deleted" directory until all * references are released. */ - ret = lttng_unlinked_file_pool_add_inode( - inode->unlinked_file_pool, inode); + ret = lttng_unlinked_file_pool_add_inode(inode->unlinked_file_pool, inode); if (ret) { PERROR("Failed to add inode \"%s\" to the unlinked file pool", - inode->location.path); + inode->location.path); goto end; } inode->unlink_pending = true; @@ -417,12 +407,12 @@ end: } static struct lttng_inode *lttng_inode_create(const struct inode_id *id, - struct cds_lfht *ht, - struct lttng_unlinked_file_pool *unlinked_file_pool, - struct lttng_directory_handle *directory_handle, - const char *path) + struct cds_lfht *ht, + struct lttng_unlinked_file_pool *unlinked_file_pool, + struct lttng_directory_handle *directory_handle, + const char *path) { - struct lttng_inode *inode = NULL; + struct lttng_inode *inode = nullptr; char *path_copy; bool reference_acquired; @@ -434,7 +424,7 @@ static struct lttng_inode *lttng_inode_create(const struct inode_id *id, reference_acquired = lttng_directory_handle_get(directory_handle); LTTNG_ASSERT(reference_acquired); - inode = (lttng_inode *) zmalloc(sizeof(*inode)); + inode = zmalloc(); if (!inode) { goto end; } @@ -446,16 +436,16 @@ static struct lttng_inode *lttng_inode_create(const struct inode_id *id, inode->unlinked_file_pool = unlinked_file_pool; /* Ownership of path copy is transferred to inode. */ inode->location.path = path_copy; - path_copy = NULL; + path_copy = nullptr; inode->location.directory_handle = directory_handle; end: free(path_copy); return inode; } -struct lttng_inode_registry *lttng_inode_registry_create(void) +struct lttng_inode_registry *lttng_inode_registry_create() { - struct lttng_inode_registry *registry = (lttng_inode_registry *) zmalloc(sizeof(*registry)); + struct lttng_inode_registry *registry = zmalloc(); if (!registry) { goto end; @@ -463,13 +453,13 @@ struct lttng_inode_registry *lttng_inode_registry_create(void) pthread_mutex_lock(&seed.lock); if (!seed.initialized) { - seed.value = (unsigned long) time(NULL); + seed.value = (unsigned long) time(nullptr); seed.initialized = true; } pthread_mutex_unlock(&seed.lock); - registry->inodes = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0, - CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); + registry->inodes = cds_lfht_new( + DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr); if (!registry->inodes) { goto error; } @@ -477,36 +467,35 @@ end: return registry; error: lttng_inode_registry_destroy(registry); - return NULL; + return nullptr; } -void lttng_inode_registry_destroy( - struct lttng_inode_registry *registry) +void lttng_inode_registry_destroy(struct lttng_inode_registry *registry) { if (!registry) { return; } if (registry->inodes) { - int ret = cds_lfht_destroy(registry->inodes, NULL); + int ret = cds_lfht_destroy(registry->inodes, nullptr); LTTNG_ASSERT(!ret); } free(registry); } -struct lttng_inode *lttng_inode_registry_get_inode( - struct lttng_inode_registry *registry, - struct lttng_directory_handle *handle, - const char *path, - int fd, - struct lttng_unlinked_file_pool *unlinked_file_pool) +struct lttng_inode * +lttng_inode_registry_get_inode(struct lttng_inode_registry *registry, + struct lttng_directory_handle *handle, + const char *path, + int fd, + struct lttng_unlinked_file_pool *unlinked_file_pool) { int ret; struct stat statbuf; struct inode_id id; struct cds_lfht_iter iter; struct cds_lfht_node *node; - struct lttng_inode *inode = NULL; + struct lttng_inode *inode = nullptr; ret = fstat(fd, &statbuf); if (ret < 0) { @@ -518,25 +507,24 @@ struct lttng_inode *lttng_inode_registry_get_inode( id.inode = statbuf.st_ino; rcu_read_lock(); - cds_lfht_lookup(registry->inodes, lttng_inode_id_hash(&id), - lttng_inode_match, &id, &iter); + cds_lfht_lookup(registry->inodes, lttng_inode_id_hash(&id), lttng_inode_match, &id, &iter); node = cds_lfht_iter_get_node(&iter); if (node) { - inode = caa_container_of( - node, struct lttng_inode, registry_node); + inode = lttng::utils::container_of(node, <tng_inode::registry_node); lttng_inode_get(inode); goto end_unlock; } - inode = lttng_inode_create(&id, registry->inodes, unlinked_file_pool, - handle, path); + inode = lttng_inode_create(&id, registry->inodes, unlinked_file_pool, handle, path); if (!inode) { goto end_unlock; } node = cds_lfht_add_unique(registry->inodes, - lttng_inode_id_hash(&inode->id), lttng_inode_match, - &inode->id, &inode->registry_node); + lttng_inode_id_hash(&inode->id), + lttng_inode_match, + &inode->id, + &inode->registry_node); LTTNG_ASSERT(node == &inode->registry_node); end_unlock: rcu_read_unlock();