2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include <common/defaults.hpp>
9 #include <common/error.hpp>
10 #include <common/hashtable/utils.hpp>
11 #include <common/macros.hpp>
12 #include <common/optional.hpp>
13 #include <common/string-utils/format.hpp>
14 #include <common/utils.hpp>
16 #include <lttng/constant.h>
18 #include <sys/types.h>
21 #include <urcu/rculfhash.h>
33 struct lttng_inode_registry
{
34 /* Hashtable of inode_id to lttng_inode. */
35 struct cds_lfht
*inodes
;
40 /* Node in the lttng_inode_registry's ht. */
41 struct cds_lfht_node registry_node
;
42 /* Weak reference to ht containing the node. */
43 struct cds_lfht
*registry_ht
;
45 struct rcu_head rcu_head
;
46 /* Location from which this file can be opened. */
48 struct lttng_directory_handle
*directory_handle
;
51 /* Unlink the underlying file at the release of the inode. */
53 LTTNG_OPTIONAL(unsigned int) unlinked_id
;
55 struct lttng_unlinked_file_pool
*unlinked_file_pool
;
58 struct lttng_unlinked_file_pool
{
59 struct lttng_directory_handle
*unlink_directory_handle
;
60 char *unlink_directory_path
;
61 unsigned int file_count
;
71 .lock
= PTHREAD_MUTEX_INITIALIZER
,
77 static unsigned long lttng_inode_id_hash(const struct inode_id
*id
)
79 uint64_t device
= id
->device
, inode_no
= id
->inode
;
81 return hash_key_u64(&device
, seed
.value
) ^
82 hash_key_u64(&inode_no
, seed
.value
);
85 static int lttng_inode_match(struct cds_lfht_node
*node
, const void *key
)
87 const struct inode_id
*id
= (inode_id
*) key
;
88 const struct lttng_inode
*inode
= lttng::utils::container_of(
89 node
, <tng_inode::registry_node
);
91 return inode
->id
.device
== id
->device
&& inode
->id
.inode
== id
->inode
;
94 static void lttng_inode_free(struct rcu_head
*head
)
96 struct lttng_inode
*inode
=
97 lttng::utils::container_of(head
, <tng_inode::rcu_head
);
102 static int lttng_unlinked_file_pool_add_inode(
103 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",
112 inode
->location
.path
, unlinked_id
);
113 ret
= asprintf(&inode_unlinked_name
, "%u", unlinked_id
);
115 ERR("Failed to format unlinked inode name");
120 if (pool
->file_count
== 0) {
121 DBG("Creating unlinked files directory at %s",
122 pool
->unlink_directory_path
);
123 LTTNG_ASSERT(!pool
->unlink_directory_handle
);
124 ret
= utils_mkdir(pool
->unlink_directory_path
,
125 S_IRWXU
| S_IRWXG
, -1, -1);
127 if (errno
== EEXIST
) {
129 * Unexpected (previous crash?), but not an
132 DBG("Unlinked file directory \"%s\" already exists",
133 pool
->unlink_directory_path
);
135 PERROR("Failed to create unlinked files directory at %s",
136 pool
->unlink_directory_path
);
140 pool
->unlink_directory_handle
= lttng_directory_handle_create(
141 pool
->unlink_directory_path
);
142 if (!pool
->unlink_directory_handle
) {
143 ERR("Failed to create directory handle to unlinked file pool at %s",
144 pool
->unlink_directory_path
);
150 ret
= lttng_directory_handle_rename(inode
->location
.directory_handle
,
151 inode
->location
.path
, pool
->unlink_directory_handle
,
152 inode_unlinked_name
);
157 lttng_directory_handle_put(inode
->location
.directory_handle
);
158 inode
->location
.directory_handle
= NULL
;
159 reference_acquired
= lttng_directory_handle_get(
160 pool
->unlink_directory_handle
);
161 LTTNG_ASSERT(reference_acquired
);
162 inode
->location
.directory_handle
= pool
->unlink_directory_handle
;
164 free(inode
->location
.path
);
165 inode
->location
.path
= inode_unlinked_name
;
166 inode_unlinked_name
= NULL
;
167 LTTNG_OPTIONAL_SET(&inode
->unlinked_id
, unlinked_id
);
170 free(inode_unlinked_name
);
174 static int lttng_unlinked_file_pool_remove_inode(
175 struct lttng_unlinked_file_pool
*pool
,
176 struct lttng_inode
*inode
)
180 DBG("Removing inode with unlinked id %u from unlinked file pool",
181 LTTNG_OPTIONAL_GET(inode
->unlinked_id
));
183 ret
= lttng_directory_handle_unlink_file(
184 inode
->location
.directory_handle
, inode
->location
.path
);
186 PERROR("Failed to unlink file %s from unlinked file directory",
187 inode
->location
.path
);
190 free(inode
->location
.path
);
191 inode
->location
.path
= NULL
;
192 lttng_directory_handle_put(inode
->location
.directory_handle
);
193 inode
->location
.directory_handle
= NULL
;
196 if (pool
->file_count
== 0) {
197 ret
= utils_recursive_rmdir(pool
->unlink_directory_path
);
200 * There is nothing the caller can do, don't report an
201 * error except through logging.
203 PERROR("Failed to remove unlinked files directory at %s",
204 pool
->unlink_directory_path
);
206 lttng_directory_handle_put(pool
->unlink_directory_handle
);
207 pool
->unlink_directory_handle
= NULL
;
213 static void lttng_inode_destroy(struct lttng_inode
*inode
)
220 cds_lfht_del(inode
->registry_ht
, &inode
->registry_node
);
223 if (inode
->unlink_pending
) {
226 LTTNG_ASSERT(inode
->location
.directory_handle
);
227 LTTNG_ASSERT(inode
->location
.path
);
228 DBG("Removing %s from unlinked file pool",
229 inode
->location
.path
);
230 ret
= lttng_unlinked_file_pool_remove_inode(inode
->unlinked_file_pool
, inode
);
232 PERROR("Failed to unlink %s", inode
->location
.path
);
236 lttng_directory_handle_put(
237 inode
->location
.directory_handle
);
238 inode
->location
.directory_handle
= NULL
;
239 free(inode
->location
.path
);
240 inode
->location
.path
= NULL
;
241 call_rcu(&inode
->rcu_head
, lttng_inode_free
);
244 static void lttng_inode_release(struct urcu_ref
*ref
)
246 lttng_inode_destroy(lttng::utils::container_of(ref
, <tng_inode::ref
));
249 static void lttng_inode_get(struct lttng_inode
*inode
)
251 urcu_ref_get(&inode
->ref
);
254 struct lttng_unlinked_file_pool
*lttng_unlinked_file_pool_create(
257 struct lttng_unlinked_file_pool
*pool
= zmalloc
<lttng_unlinked_file_pool
>();
263 if (!path
|| *path
!= '/') {
264 ERR("Unlinked file pool must be created with an absolute path, path = \"%s\"",
265 path
? path
: "NULL");
269 pool
->unlink_directory_path
= strdup(path
);
270 if (!pool
->unlink_directory_path
) {
271 PERROR("Failed to allocation unlinked file pool path");
274 DBG("Unlinked file pool created at: %s", path
);
277 lttng_unlinked_file_pool_destroy(pool
);
281 void lttng_unlinked_file_pool_destroy(
282 struct lttng_unlinked_file_pool
*pool
)
288 LTTNG_ASSERT(pool
->file_count
== 0);
289 lttng_directory_handle_put(pool
->unlink_directory_handle
);
290 free(pool
->unlink_directory_path
);
294 void lttng_inode_put(struct lttng_inode
*inode
)
296 urcu_ref_put(&inode
->ref
, lttng_inode_release
);
299 struct lttng_directory_handle
*
300 lttng_inode_get_location_directory_handle(
301 struct lttng_inode
*inode
)
303 if (inode
->location
.directory_handle
) {
304 const bool reference_acquired
= lttng_directory_handle_get(
305 inode
->location
.directory_handle
);
307 LTTNG_ASSERT(reference_acquired
);
309 return inode
->location
.directory_handle
;
312 void lttng_inode_borrow_location(struct lttng_inode
*inode
,
313 const struct lttng_directory_handle
**out_directory_handle
,
314 const char **out_path
)
316 if (out_directory_handle
) {
317 *out_directory_handle
= inode
->location
.directory_handle
;
320 *out_path
= inode
->location
.path
;
324 int lttng_inode_rename(
325 struct lttng_inode
*inode
,
326 struct lttng_directory_handle
*old_directory_handle
,
327 const char *old_path
,
328 struct lttng_directory_handle
*new_directory_handle
,
329 const char *new_path
,
333 char *new_path_copy
= strdup(new_path
);
334 bool reference_acquired
;
336 DBG("Performing rename of inode from %s to %s with %s directory handles",
338 lttng_directory_handle_equals(old_directory_handle
,
339 new_directory_handle
) ?
343 if (!new_path_copy
) {
348 if (inode
->unlink_pending
) {
349 WARN("An attempt to rename an unlinked file from %s to %s has been performed",
356 /* Verify that file doesn't exist. */
359 ret
= lttng_directory_handle_stat(
360 new_directory_handle
, new_path
, &statbuf
);
362 ERR("Refusing to rename %s as the destination already exists",
366 } else if (ret
< 0 && errno
!= ENOENT
) {
367 PERROR("Failed to stat() %s", new_path
);
373 ret
= lttng_directory_handle_rename(old_directory_handle
, old_path
,
374 new_directory_handle
, new_path
);
376 PERROR("Failed to rename file %s to %s", old_path
, new_path
);
381 reference_acquired
= lttng_directory_handle_get(new_directory_handle
);
382 LTTNG_ASSERT(reference_acquired
);
383 lttng_directory_handle_put(inode
->location
.directory_handle
);
384 free(inode
->location
.path
);
385 inode
->location
.directory_handle
= new_directory_handle
;
386 /* Ownership transferred. */
387 inode
->location
.path
= new_path_copy
;
388 new_path_copy
= NULL
;
394 int lttng_inode_unlink(struct lttng_inode
*inode
)
398 DBG("Attempting unlink of inode %s", inode
->location
.path
);
400 if (inode
->unlink_pending
) {
401 WARN("An attempt to re-unlink %s has been performed, ignoring.",
402 inode
->location
.path
);
408 * Move to the temporary "deleted" directory until all
409 * references are released.
411 ret
= lttng_unlinked_file_pool_add_inode(
412 inode
->unlinked_file_pool
, inode
);
414 PERROR("Failed to add inode \"%s\" to the unlinked file pool",
415 inode
->location
.path
);
418 inode
->unlink_pending
= true;
423 static struct lttng_inode
*lttng_inode_create(const struct inode_id
*id
,
425 struct lttng_unlinked_file_pool
*unlinked_file_pool
,
426 struct lttng_directory_handle
*directory_handle
,
429 struct lttng_inode
*inode
= NULL
;
431 bool reference_acquired
;
433 path_copy
= strdup(path
);
438 reference_acquired
= lttng_directory_handle_get(directory_handle
);
439 LTTNG_ASSERT(reference_acquired
);
441 inode
= zmalloc
<lttng_inode
>();
446 urcu_ref_init(&inode
->ref
);
447 cds_lfht_node_init(&inode
->registry_node
);
449 inode
->registry_ht
= ht
;
450 inode
->unlinked_file_pool
= unlinked_file_pool
;
451 /* Ownership of path copy is transferred to inode. */
452 inode
->location
.path
= path_copy
;
454 inode
->location
.directory_handle
= directory_handle
;
460 struct lttng_inode_registry
*lttng_inode_registry_create(void)
462 struct lttng_inode_registry
*registry
= zmalloc
<lttng_inode_registry
>();
468 pthread_mutex_lock(&seed
.lock
);
469 if (!seed
.initialized
) {
470 seed
.value
= (unsigned long) time(NULL
);
471 seed
.initialized
= true;
473 pthread_mutex_unlock(&seed
.lock
);
475 registry
->inodes
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
476 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
477 if (!registry
->inodes
) {
483 lttng_inode_registry_destroy(registry
);
487 void lttng_inode_registry_destroy(
488 struct lttng_inode_registry
*registry
)
493 if (registry
->inodes
) {
494 int ret
= cds_lfht_destroy(registry
->inodes
, NULL
);
501 struct lttng_inode
*lttng_inode_registry_get_inode(
502 struct lttng_inode_registry
*registry
,
503 struct lttng_directory_handle
*handle
,
506 struct lttng_unlinked_file_pool
*unlinked_file_pool
)
511 struct cds_lfht_iter iter
;
512 struct cds_lfht_node
*node
;
513 struct lttng_inode
*inode
= NULL
;
515 ret
= fstat(fd
, &statbuf
);
517 PERROR("stat() failed on fd %i", fd
);
521 id
.device
= statbuf
.st_dev
;
522 id
.inode
= statbuf
.st_ino
;
525 cds_lfht_lookup(registry
->inodes
, lttng_inode_id_hash(&id
),
526 lttng_inode_match
, &id
, &iter
);
527 node
= cds_lfht_iter_get_node(&iter
);
529 inode
= lttng::utils::container_of(
530 node
, <tng_inode::registry_node
);
531 lttng_inode_get(inode
);
535 inode
= lttng_inode_create(&id
, registry
->inodes
, unlinked_file_pool
,
541 node
= cds_lfht_add_unique(registry
->inodes
,
542 lttng_inode_id_hash(&inode
->id
), lttng_inode_match
,
543 &inode
->id
, &inode
->registry_node
);
544 LTTNG_ASSERT(node
== &inode
->registry_node
);