2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include <common/defaults.hpp>
11 #include <common/error.hpp>
12 #include <common/hashtable/utils.hpp>
13 #include <common/macros.hpp>
14 #include <common/optional.hpp>
15 #include <common/string-utils/format.hpp>
16 #include <common/urcu.hpp>
17 #include <common/utils.hpp>
19 #include <lttng/constant.h>
23 #include <sys/types.h>
26 #include <urcu/rculfhash.h>
36 struct lttng_inode_registry
{
37 /* Hashtable of inode_id to lttng_inode. */
38 struct cds_lfht
*inodes
;
43 /* Node in the lttng_inode_registry's ht. */
44 struct cds_lfht_node registry_node
;
45 /* Weak reference to ht containing the node. */
46 struct cds_lfht
*registry_ht
;
48 struct rcu_head rcu_head
;
49 /* Location from which this file can be opened. */
51 struct lttng_directory_handle
*directory_handle
;
54 /* Unlink the underlying file at the release of the inode. */
56 LTTNG_OPTIONAL(unsigned int) unlinked_id
;
58 struct lttng_unlinked_file_pool
*unlinked_file_pool
;
61 struct lttng_unlinked_file_pool
{
62 struct lttng_directory_handle
*unlink_directory_handle
;
63 char *unlink_directory_path
;
64 unsigned int file_count
;
74 .lock
= PTHREAD_MUTEX_INITIALIZER
,
80 static unsigned long lttng_inode_id_hash(const struct inode_id
*id
)
82 uint64_t device
= id
->device
, inode_no
= id
->inode
;
84 return hash_key_u64(&device
, seed
.value
) ^ hash_key_u64(&inode_no
, seed
.value
);
87 static int lttng_inode_match(struct cds_lfht_node
*node
, const void *key
)
89 const struct inode_id
*id
= (inode_id
*) key
;
90 const struct lttng_inode
*inode
=
91 lttng::utils::container_of(node
, <tng_inode::registry_node
);
93 return inode
->id
.device
== id
->device
&& inode
->id
.inode
== id
->inode
;
96 static void lttng_inode_free(struct rcu_head
*head
)
98 struct lttng_inode
*inode
= lttng::utils::container_of(head
, <tng_inode::rcu_head
);
103 static int lttng_unlinked_file_pool_add_inode(struct lttng_unlinked_file_pool
*pool
,
104 struct lttng_inode
*inode
)
107 const unsigned int unlinked_id
= pool
->next_id
++;
108 char *inode_unlinked_name
;
109 bool reference_acquired
;
111 DBG("Adding inode of %s to unlinked file pool as id %u", inode
->location
.path
, unlinked_id
);
112 ret
= asprintf(&inode_unlinked_name
, "%u", unlinked_id
);
114 ERR("Failed to format unlinked inode name");
119 if (pool
->file_count
== 0) {
120 DBG("Creating unlinked files directory at %s", pool
->unlink_directory_path
);
121 LTTNG_ASSERT(!pool
->unlink_directory_handle
);
122 ret
= utils_mkdir(pool
->unlink_directory_path
, S_IRWXU
| S_IRWXG
, -1, -1);
124 if (errno
== EEXIST
) {
126 * Unexpected (previous crash?), but not an
129 DBG("Unlinked file directory \"%s\" already exists",
130 pool
->unlink_directory_path
);
132 PERROR("Failed to create unlinked files directory at %s",
133 pool
->unlink_directory_path
);
137 pool
->unlink_directory_handle
=
138 lttng_directory_handle_create(pool
->unlink_directory_path
);
139 if (!pool
->unlink_directory_handle
) {
140 ERR("Failed to create directory handle to unlinked file pool at %s",
141 pool
->unlink_directory_path
);
147 ret
= lttng_directory_handle_rename(inode
->location
.directory_handle
,
148 inode
->location
.path
,
149 pool
->unlink_directory_handle
,
150 inode_unlinked_name
);
155 lttng_directory_handle_put(inode
->location
.directory_handle
);
156 inode
->location
.directory_handle
= nullptr;
157 reference_acquired
= lttng_directory_handle_get(pool
->unlink_directory_handle
);
158 LTTNG_ASSERT(reference_acquired
);
159 inode
->location
.directory_handle
= pool
->unlink_directory_handle
;
161 free(inode
->location
.path
);
162 inode
->location
.path
= inode_unlinked_name
;
163 inode_unlinked_name
= nullptr;
164 LTTNG_OPTIONAL_SET(&inode
->unlinked_id
, unlinked_id
);
167 free(inode_unlinked_name
);
171 static int lttng_unlinked_file_pool_remove_inode(struct lttng_unlinked_file_pool
*pool
,
172 struct lttng_inode
*inode
)
176 DBG("Removing inode with unlinked id %u from unlinked file pool",
177 LTTNG_OPTIONAL_GET(inode
->unlinked_id
));
179 ret
= lttng_directory_handle_unlink_file(inode
->location
.directory_handle
,
180 inode
->location
.path
);
182 PERROR("Failed to unlink file %s from unlinked file directory",
183 inode
->location
.path
);
186 free(inode
->location
.path
);
187 inode
->location
.path
= nullptr;
188 lttng_directory_handle_put(inode
->location
.directory_handle
);
189 inode
->location
.directory_handle
= nullptr;
192 if (pool
->file_count
== 0) {
193 ret
= utils_recursive_rmdir(pool
->unlink_directory_path
);
196 * There is nothing the caller can do, don't report an
197 * error except through logging.
199 PERROR("Failed to remove unlinked files directory at %s",
200 pool
->unlink_directory_path
);
202 lttng_directory_handle_put(pool
->unlink_directory_handle
);
203 pool
->unlink_directory_handle
= nullptr;
209 static void lttng_inode_destroy(struct lttng_inode
*inode
)
215 lttng::urcu::read_lock_guard read_lock
;
216 cds_lfht_del(inode
->registry_ht
, &inode
->registry_node
);
218 if (inode
->unlink_pending
) {
221 LTTNG_ASSERT(inode
->location
.directory_handle
);
222 LTTNG_ASSERT(inode
->location
.path
);
223 DBG("Removing %s from unlinked file pool", inode
->location
.path
);
224 ret
= lttng_unlinked_file_pool_remove_inode(inode
->unlinked_file_pool
, inode
);
226 PERROR("Failed to unlink %s", inode
->location
.path
);
230 lttng_directory_handle_put(inode
->location
.directory_handle
);
231 inode
->location
.directory_handle
= nullptr;
232 free(inode
->location
.path
);
233 inode
->location
.path
= nullptr;
234 call_rcu(&inode
->rcu_head
, lttng_inode_free
);
237 static void lttng_inode_release(struct urcu_ref
*ref
)
239 lttng_inode_destroy(lttng::utils::container_of(ref
, <tng_inode::ref
));
242 static void lttng_inode_get(struct lttng_inode
*inode
)
244 urcu_ref_get(&inode
->ref
);
247 struct lttng_unlinked_file_pool
*lttng_unlinked_file_pool_create(const char *path
)
249 struct lttng_unlinked_file_pool
*pool
= zmalloc
<lttng_unlinked_file_pool
>();
255 if (!path
|| *path
!= '/') {
256 ERR("Unlinked file pool must be created with an absolute path, path = \"%s\"",
257 path
? path
: "NULL");
261 pool
->unlink_directory_path
= strdup(path
);
262 if (!pool
->unlink_directory_path
) {
263 PERROR("Failed to allocation unlinked file pool path");
266 DBG("Unlinked file pool created at: %s", path
);
269 lttng_unlinked_file_pool_destroy(pool
);
273 void lttng_unlinked_file_pool_destroy(struct lttng_unlinked_file_pool
*pool
)
279 LTTNG_ASSERT(pool
->file_count
== 0);
280 lttng_directory_handle_put(pool
->unlink_directory_handle
);
281 free(pool
->unlink_directory_path
);
285 void lttng_inode_put(struct lttng_inode
*inode
)
287 urcu_ref_put(&inode
->ref
, lttng_inode_release
);
290 struct lttng_directory_handle
*lttng_inode_get_location_directory_handle(struct lttng_inode
*inode
)
292 if (inode
->location
.directory_handle
) {
293 const bool reference_acquired
=
294 lttng_directory_handle_get(inode
->location
.directory_handle
);
296 LTTNG_ASSERT(reference_acquired
);
298 return inode
->location
.directory_handle
;
301 void lttng_inode_borrow_location(struct lttng_inode
*inode
,
302 const struct lttng_directory_handle
**out_directory_handle
,
303 const char **out_path
)
305 if (out_directory_handle
) {
306 *out_directory_handle
= inode
->location
.directory_handle
;
309 *out_path
= inode
->location
.path
;
313 int lttng_inode_rename(struct lttng_inode
*inode
,
314 struct lttng_directory_handle
*old_directory_handle
,
315 const char *old_path
,
316 struct lttng_directory_handle
*new_directory_handle
,
317 const char *new_path
,
321 char *new_path_copy
= strdup(new_path
);
322 bool reference_acquired
;
324 DBG("Performing rename of inode from %s to %s with %s directory handles",
327 lttng_directory_handle_equals(old_directory_handle
, new_directory_handle
) ?
331 if (!new_path_copy
) {
336 if (inode
->unlink_pending
) {
337 WARN("An attempt to rename an unlinked file from %s to %s has been performed",
345 /* Verify that file doesn't exist. */
348 ret
= lttng_directory_handle_stat(new_directory_handle
, new_path
, &statbuf
);
350 ERR("Refusing to rename %s as the destination already exists", old_path
);
353 } else if (ret
< 0 && errno
!= ENOENT
) {
354 PERROR("Failed to stat() %s", new_path
);
360 ret
= lttng_directory_handle_rename(
361 old_directory_handle
, old_path
, new_directory_handle
, new_path
);
363 PERROR("Failed to rename file %s to %s", old_path
, new_path
);
368 reference_acquired
= lttng_directory_handle_get(new_directory_handle
);
369 LTTNG_ASSERT(reference_acquired
);
370 lttng_directory_handle_put(inode
->location
.directory_handle
);
371 free(inode
->location
.path
);
372 inode
->location
.directory_handle
= new_directory_handle
;
373 /* Ownership transferred. */
374 inode
->location
.path
= new_path_copy
;
375 new_path_copy
= nullptr;
381 int lttng_inode_unlink(struct lttng_inode
*inode
)
385 DBG("Attempting unlink of inode %s", inode
->location
.path
);
387 if (inode
->unlink_pending
) {
388 WARN("An attempt to re-unlink %s has been performed, ignoring.",
389 inode
->location
.path
);
395 * Move to the temporary "deleted" directory until all
396 * references are released.
398 ret
= lttng_unlinked_file_pool_add_inode(inode
->unlinked_file_pool
, inode
);
400 PERROR("Failed to add inode \"%s\" to the unlinked file pool",
401 inode
->location
.path
);
404 inode
->unlink_pending
= true;
409 static struct lttng_inode
*lttng_inode_create(const struct inode_id
*id
,
411 struct lttng_unlinked_file_pool
*unlinked_file_pool
,
412 struct lttng_directory_handle
*directory_handle
,
415 struct lttng_inode
*inode
= nullptr;
417 bool reference_acquired
;
419 path_copy
= strdup(path
);
424 reference_acquired
= lttng_directory_handle_get(directory_handle
);
425 LTTNG_ASSERT(reference_acquired
);
427 inode
= zmalloc
<lttng_inode
>();
432 urcu_ref_init(&inode
->ref
);
433 cds_lfht_node_init(&inode
->registry_node
);
435 inode
->registry_ht
= ht
;
436 inode
->unlinked_file_pool
= unlinked_file_pool
;
437 /* Ownership of path copy is transferred to inode. */
438 inode
->location
.path
= path_copy
;
440 inode
->location
.directory_handle
= directory_handle
;
446 struct lttng_inode_registry
*lttng_inode_registry_create()
448 struct lttng_inode_registry
*registry
= zmalloc
<lttng_inode_registry
>();
454 pthread_mutex_lock(&seed
.lock
);
455 if (!seed
.initialized
) {
456 seed
.value
= (unsigned long) time(nullptr);
457 seed
.initialized
= true;
459 pthread_mutex_unlock(&seed
.lock
);
461 registry
->inodes
= cds_lfht_new(
462 DEFAULT_HT_SIZE
, 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, nullptr);
463 if (!registry
->inodes
) {
469 lttng_inode_registry_destroy(registry
);
473 void lttng_inode_registry_destroy(struct lttng_inode_registry
*registry
)
478 if (registry
->inodes
) {
479 int ret
= cds_lfht_destroy(registry
->inodes
, nullptr);
487 lttng_inode_registry_get_inode(struct lttng_inode_registry
*registry
,
488 struct lttng_directory_handle
*handle
,
491 struct lttng_unlinked_file_pool
*unlinked_file_pool
)
496 struct cds_lfht_iter iter
;
497 struct cds_lfht_node
*node
;
498 struct lttng_inode
*inode
= nullptr;
499 lttng::urcu::read_lock_guard read_lock
;
501 ret
= fstat(fd
, &statbuf
);
503 PERROR("stat() failed on fd %i", fd
);
507 id
.device
= statbuf
.st_dev
;
508 id
.inode
= statbuf
.st_ino
;
510 cds_lfht_lookup(registry
->inodes
, lttng_inode_id_hash(&id
), lttng_inode_match
, &id
, &iter
);
511 node
= cds_lfht_iter_get_node(&iter
);
513 inode
= lttng::utils::container_of(node
, <tng_inode::registry_node
);
514 lttng_inode_get(inode
);
518 inode
= lttng_inode_create(&id
, registry
->inodes
, unlinked_file_pool
, handle
, path
);
523 node
= cds_lfht_add_unique(registry
->inodes
,
524 lttng_inode_id_hash(&inode
->id
),
527 &inode
->registry_node
);
528 LTTNG_ASSERT(node
== &inode
->registry_node
);