2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include <common/defaults.h>
9 #include <common/error.h>
10 #include <common/hashtable/utils.h>
11 #include <common/macros.h>
12 #include <common/optional.h>
13 #include <common/string-utils/format.h>
14 #include <common/utils.h>
16 #include <lttng/constant.h>
18 #include <sys/types.h>
21 #include <urcu/rculfhash.h>
31 struct lttng_inode_registry
{
32 /* Hashtable of inode_id to lttng_inode. */
33 struct cds_lfht
*inodes
;
38 /* Node in the lttng_inode_registry's ht. */
39 struct cds_lfht_node registry_node
;
40 /* Weak reference to ht containing the node. */
41 struct cds_lfht
*registry_ht
;
43 struct rcu_head rcu_head
;
44 /* Location from which this file can be opened. */
46 struct lttng_directory_handle
*directory_handle
;
49 /* Unlink the underlying file at the release of the inode. */
51 LTTNG_OPTIONAL(unsigned int) unlinked_id
;
53 struct lttng_unlinked_file_pool
*unlinked_file_pool
;
56 struct lttng_unlinked_file_pool
{
57 struct lttng_directory_handle
*unlink_directory_handle
;
58 char *unlink_directory_path
;
59 unsigned int file_count
;
68 .lock
= PTHREAD_MUTEX_INITIALIZER
,
71 static unsigned long lttng_inode_id_hash(const struct inode_id
*id
)
73 uint64_t device
= id
->device
, inode_no
= id
->inode
;
75 return hash_key_u64(&device
, seed
.value
) ^
76 hash_key_u64(&inode_no
, seed
.value
);
79 static int lttng_inode_match(struct cds_lfht_node
*node
, const void *key
)
81 const struct inode_id
*id
= key
;
82 const struct lttng_inode
*inode
= caa_container_of(
83 node
, struct lttng_inode
, registry_node
);
85 return inode
->id
.device
== id
->device
&& inode
->id
.inode
== id
->inode
;
88 static void lttng_inode_free(struct rcu_head
*head
)
90 struct lttng_inode
*inode
=
91 caa_container_of(head
, struct lttng_inode
, rcu_head
);
96 static int lttng_unlinked_file_pool_add_inode(
97 struct lttng_unlinked_file_pool
*pool
,
98 struct lttng_inode
*inode
)
101 const unsigned int unlinked_id
= pool
->next_id
++;
102 char *inode_unlinked_name
;
103 bool reference_acquired
;
105 DBG("Adding inode of %s to unlinked file pool as id %u",
106 inode
->location
.path
, unlinked_id
);
107 ret
= asprintf(&inode_unlinked_name
, "%u", unlinked_id
);
109 ERR("Failed to format unlinked inode name");
114 if (pool
->file_count
== 0) {
115 DBG("Creating unlinked files directory at %s",
116 pool
->unlink_directory_path
);
117 assert(!pool
->unlink_directory_handle
);
118 ret
= utils_mkdir(pool
->unlink_directory_path
,
119 S_IRWXU
| S_IRWXG
, -1, -1);
121 if (errno
== EEXIST
) {
123 * Unexpected (previous crash?), but not an
126 DBG("Unlinked file directory \"%s\" already exists",
127 pool
->unlink_directory_path
);
129 PERROR("Failed to create unlinked files directory at %s",
130 pool
->unlink_directory_path
);
134 pool
->unlink_directory_handle
= lttng_directory_handle_create(
135 pool
->unlink_directory_path
);
138 ret
= lttng_directory_handle_rename(inode
->location
.directory_handle
,
139 inode
->location
.path
, pool
->unlink_directory_handle
,
140 inode_unlinked_name
);
145 lttng_directory_handle_put(inode
->location
.directory_handle
);
146 inode
->location
.directory_handle
= NULL
;
147 reference_acquired
= lttng_directory_handle_get(
148 pool
->unlink_directory_handle
);
149 assert(reference_acquired
);
150 inode
->location
.directory_handle
= pool
->unlink_directory_handle
;
152 free(inode
->location
.path
);
153 inode
->location
.path
= inode_unlinked_name
;
154 inode_unlinked_name
= NULL
;
155 LTTNG_OPTIONAL_SET(&inode
->unlinked_id
, unlinked_id
);
158 free(inode_unlinked_name
);
162 static int lttng_unlinked_file_pool_remove_inode(
163 struct lttng_unlinked_file_pool
*pool
,
164 struct lttng_inode
*inode
)
168 DBG("Removing inode with unlinked id %u from unlinked file pool",
169 LTTNG_OPTIONAL_GET(inode
->unlinked_id
));
171 ret
= lttng_directory_handle_unlink_file(
172 inode
->location
.directory_handle
, inode
->location
.path
);
174 PERROR("Failed to unlink file %s from unlinked file directory",
175 inode
->location
.path
);
178 free(inode
->location
.path
);
179 inode
->location
.path
= NULL
;
180 lttng_directory_handle_put(inode
->location
.directory_handle
);
181 inode
->location
.directory_handle
= NULL
;
184 if (pool
->file_count
== 0) {
185 ret
= utils_recursive_rmdir(pool
->unlink_directory_path
);
188 * There is nothing the caller can do, don't report an
189 * error except through logging.
191 PERROR("Failed to remove unlinked files directory at %s",
192 pool
->unlink_directory_path
);
194 lttng_directory_handle_put(pool
->unlink_directory_handle
);
195 pool
->unlink_directory_handle
= NULL
;
201 static void lttng_inode_destroy(struct lttng_inode
*inode
)
208 cds_lfht_del(inode
->registry_ht
, &inode
->registry_node
);
211 if (inode
->unlink_pending
) {
214 assert(inode
->location
.directory_handle
);
215 assert(inode
->location
.path
);
216 DBG("Removing %s from unlinked file pool",
217 inode
->location
.path
);
218 ret
= lttng_unlinked_file_pool_remove_inode(inode
->unlinked_file_pool
, inode
);
220 PERROR("Failed to unlink %s", inode
->location
.path
);
224 lttng_directory_handle_put(
225 inode
->location
.directory_handle
);
226 inode
->location
.directory_handle
= NULL
;
227 free(inode
->location
.path
);
228 inode
->location
.path
= NULL
;
229 call_rcu(&inode
->rcu_head
, lttng_inode_free
);
232 static void lttng_inode_release(struct urcu_ref
*ref
)
234 lttng_inode_destroy(caa_container_of(ref
, struct lttng_inode
, ref
));
237 static void lttng_inode_get(struct lttng_inode
*inode
)
239 urcu_ref_get(&inode
->ref
);
242 struct lttng_unlinked_file_pool
*lttng_unlinked_file_pool_create(
245 struct lttng_unlinked_file_pool
*pool
= zmalloc(sizeof(*pool
));
247 if (!path
|| *path
!= '/') {
248 ERR("Unlinked file pool must be created with an absolute path, path = \"%s\"",
249 path
? path
: "NULL");
253 pool
->unlink_directory_path
= strdup(path
);
254 if (!pool
->unlink_directory_path
) {
255 PERROR("Failed to allocation unlinked file pool path");
258 DBG("Unlinked file pool created at: %s", path
);
261 lttng_unlinked_file_pool_destroy(pool
);
265 void lttng_unlinked_file_pool_destroy(
266 struct lttng_unlinked_file_pool
*pool
)
272 assert(pool
->file_count
== 0);
273 lttng_directory_handle_put(pool
->unlink_directory_handle
);
274 free(pool
->unlink_directory_path
);
278 void lttng_inode_put(struct lttng_inode
*inode
)
280 urcu_ref_put(&inode
->ref
, lttng_inode_release
);
283 void lttng_inode_get_location(struct lttng_inode
*inode
,
284 const struct lttng_directory_handle
**out_directory_handle
,
285 const char **out_path
)
287 if (out_directory_handle
) {
288 *out_directory_handle
= inode
->location
.directory_handle
;
291 *out_path
= inode
->location
.path
;
295 int lttng_inode_rename(
296 struct lttng_inode
*inode
,
297 struct lttng_directory_handle
*old_directory_handle
,
298 const char *old_path
,
299 struct lttng_directory_handle
*new_directory_handle
,
300 const char *new_path
,
304 char *new_path_copy
= strdup(new_path
);
305 bool reference_acquired
;
307 DBG("Performing rename of inode from %s to %s with %s directory handles",
309 lttng_directory_handle_equals(old_directory_handle
,
310 new_directory_handle
) ?
314 if (!new_path_copy
) {
319 if (inode
->unlink_pending
) {
320 WARN("An attempt to rename an unlinked file from %s to %s has been performed",
327 /* Verify that file doesn't exist. */
330 ret
= lttng_directory_handle_stat(
331 new_directory_handle
, new_path
, &statbuf
);
333 ERR("Refusing to rename %s as the destination already exists",
337 } else if (ret
< 0 && errno
!= ENOENT
) {
338 PERROR("Failed to stat() %s", new_path
);
344 ret
= lttng_directory_handle_rename(old_directory_handle
, old_path
,
345 new_directory_handle
, new_path
);
347 PERROR("Failed to rename file %s to %s", old_path
, new_path
);
352 reference_acquired
= lttng_directory_handle_get(new_directory_handle
);
353 assert(reference_acquired
);
354 lttng_directory_handle_put(inode
->location
.directory_handle
);
355 free(inode
->location
.path
);
356 inode
->location
.directory_handle
= new_directory_handle
;
357 /* Ownership transferred. */
358 inode
->location
.path
= new_path_copy
;
359 new_path_copy
= NULL
;
365 int lttng_inode_unlink(struct lttng_inode
*inode
)
369 DBG("Attempting unlink of inode %s", inode
->location
.path
);
371 if (inode
->unlink_pending
) {
372 WARN("An attempt to re-unlink %s has been performed, ignoring.",
373 inode
->location
.path
);
379 * Move to the temporary "deleted" directory until all
380 * references are released.
382 ret
= lttng_unlinked_file_pool_add_inode(
383 inode
->unlinked_file_pool
, inode
);
385 PERROR("Failed to add inode \"%s\" to the unlinked file pool",
386 inode
->location
.path
);
389 inode
->unlink_pending
= true;
394 static struct lttng_inode
*lttng_inode_create(const struct inode_id
*id
,
396 struct lttng_unlinked_file_pool
*unlinked_file_pool
,
397 struct lttng_directory_handle
*directory_handle
,
400 struct lttng_inode
*inode
= NULL
;
402 bool reference_acquired
;
404 path_copy
= strdup(path
);
409 reference_acquired
= lttng_directory_handle_get(directory_handle
);
410 assert(reference_acquired
);
412 inode
= zmalloc(sizeof(*inode
));
417 urcu_ref_init(&inode
->ref
);
418 cds_lfht_node_init(&inode
->registry_node
);
420 inode
->registry_ht
= ht
;
421 inode
->unlinked_file_pool
= unlinked_file_pool
;
422 /* Ownership of path copy is transferred to inode. */
423 inode
->location
.path
= path_copy
;
425 inode
->location
.directory_handle
= directory_handle
;
431 struct lttng_inode_registry
*lttng_inode_registry_create(void)
433 struct lttng_inode_registry
*registry
= zmalloc(sizeof(*registry
));
439 pthread_mutex_lock(&seed
.lock
);
440 if (!seed
.initialized
) {
441 seed
.value
= (unsigned long) time(NULL
);
442 seed
.initialized
= true;
444 pthread_mutex_unlock(&seed
.lock
);
446 registry
->inodes
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
447 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
448 if (!registry
->inodes
) {
454 lttng_inode_registry_destroy(registry
);
458 void lttng_inode_registry_destroy(struct lttng_inode_registry
*registry
)
463 if (registry
->inodes
) {
464 int ret
= cds_lfht_destroy(registry
->inodes
, NULL
);
471 struct lttng_inode
*lttng_inode_registry_get_inode(
472 struct lttng_inode_registry
*registry
,
473 struct lttng_directory_handle
*handle
,
476 struct lttng_unlinked_file_pool
*unlinked_file_pool
)
481 struct cds_lfht_iter iter
;
482 struct cds_lfht_node
*node
;
483 struct lttng_inode
*inode
= NULL
;
485 ret
= fstat(fd
, &statbuf
);
487 PERROR("stat() failed on fd %i", fd
);
491 id
.device
= statbuf
.st_dev
;
492 id
.inode
= statbuf
.st_ino
;
495 cds_lfht_lookup(registry
->inodes
, lttng_inode_id_hash(&id
),
496 lttng_inode_match
, &id
, &iter
);
497 node
= cds_lfht_iter_get_node(&iter
);
499 inode
= caa_container_of(
500 node
, struct lttng_inode
, registry_node
);
501 lttng_inode_get(inode
);
505 inode
= lttng_inode_create(&id
, registry
->inodes
, unlinked_file_pool
,
507 node
= cds_lfht_add_unique(registry
->inodes
,
508 lttng_inode_id_hash(&inode
->id
), lttng_inode_match
,
509 &inode
->id
, &inode
->registry_node
);
510 assert(node
== &inode
->registry_node
);