2 * Copyright (C) 2020 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 #include <common/defaults.h>
19 #include <common/error.h>
20 #include <common/hashtable/utils.h>
21 #include <common/macros.h>
22 #include <common/optional.h>
23 #include <common/string-utils/format.h>
24 #include <common/utils.h>
26 #include <lttng/constant.h>
28 #include <sys/types.h>
31 #include <urcu/rculfhash.h>
41 struct lttng_inode_registry
{
42 /* Hashtable of inode_id to lttng_inode. */
43 struct cds_lfht
*inodes
;
48 /* Node in the lttng_inode_registry's ht. */
49 struct cds_lfht_node registry_node
;
50 /* Weak reference to ht containing the node. */
51 struct cds_lfht
*registry_ht
;
53 struct rcu_head rcu_head
;
54 /* Location from which this file can be opened. */
56 struct lttng_directory_handle
*directory_handle
;
59 /* Unlink the underlying file at the release of the inode. */
61 LTTNG_OPTIONAL(unsigned int) unlinked_id
;
63 struct lttng_unlinked_file_pool
*unlinked_file_pool
;
66 struct lttng_unlinked_file_pool
{
67 struct lttng_directory_handle
*unlink_directory_handle
;
68 char *unlink_directory_path
;
69 unsigned int file_count
;
78 .lock
= PTHREAD_MUTEX_INITIALIZER
,
81 static unsigned long lttng_inode_id_hash(const struct inode_id
*id
)
83 uint64_t device
= id
->device
, inode_no
= id
->inode
;
85 return hash_key_u64(&device
, seed
.value
) ^
86 hash_key_u64(&inode_no
, seed
.value
);
89 static int lttng_inode_match(struct cds_lfht_node
*node
, const void *key
)
91 const struct inode_id
*id
= key
;
92 const struct lttng_inode
*inode
= caa_container_of(
93 node
, struct lttng_inode
, registry_node
);
95 return inode
->id
.device
== id
->device
&& inode
->id
.inode
== id
->inode
;
98 static void lttng_inode_free(struct rcu_head
*head
)
100 struct lttng_inode
*inode
=
101 caa_container_of(head
, struct lttng_inode
, rcu_head
);
106 static int lttng_unlinked_file_pool_add_inode(
107 struct lttng_unlinked_file_pool
*pool
,
108 struct lttng_inode
*inode
)
111 const unsigned int unlinked_id
= pool
->next_id
++;
112 char *inode_unlinked_name
;
113 bool reference_acquired
;
115 DBG("Adding inode of %s to unlinked file pool as id %u",
116 inode
->location
.path
, unlinked_id
);
117 ret
= asprintf(&inode_unlinked_name
, "%u", unlinked_id
);
119 ERR("Failed to format unlinked inode name");
124 if (pool
->file_count
== 0) {
125 DBG("Creating unlinked files directory at %s",
126 pool
->unlink_directory_path
);
127 assert(!pool
->unlink_directory_handle
);
128 ret
= utils_mkdir(pool
->unlink_directory_path
,
129 S_IRWXU
| S_IRWXG
, -1, -1);
131 if (errno
== EEXIST
) {
133 * Unexpected (previous crash?), but not an
136 DBG("Unlinked file directory \"%s\" already exists",
137 pool
->unlink_directory_path
);
139 PERROR("Failed to create unlinked files directory at %s",
140 pool
->unlink_directory_path
);
144 pool
->unlink_directory_handle
= lttng_directory_handle_create(
145 pool
->unlink_directory_path
);
148 ret
= lttng_directory_handle_rename(inode
->location
.directory_handle
,
149 inode
->location
.path
, pool
->unlink_directory_handle
,
150 inode_unlinked_name
);
155 lttng_directory_handle_put(inode
->location
.directory_handle
);
156 inode
->location
.directory_handle
= NULL
;
157 reference_acquired
= lttng_directory_handle_get(
158 pool
->unlink_directory_handle
);
159 assert(reference_acquired
);
160 inode
->location
.directory_handle
= pool
->unlink_directory_handle
;
162 free(inode
->location
.path
);
163 inode
->location
.path
= inode_unlinked_name
;
164 inode_unlinked_name
= NULL
;
165 LTTNG_OPTIONAL_SET(&inode
->unlinked_id
, unlinked_id
);
168 free(inode_unlinked_name
);
172 static int lttng_unlinked_file_pool_remove_inode(
173 struct lttng_unlinked_file_pool
*pool
,
174 struct lttng_inode
*inode
)
178 DBG("Removing inode with unlinked id %u from unlinked file pool",
179 LTTNG_OPTIONAL_GET(inode
->unlinked_id
));
181 ret
= lttng_directory_handle_unlink_file(
182 inode
->location
.directory_handle
, inode
->location
.path
);
184 PERROR("Failed to unlink file %s from unlinked file directory",
185 inode
->location
.path
);
188 free(inode
->location
.path
);
189 inode
->location
.path
= NULL
;
190 lttng_directory_handle_put(inode
->location
.directory_handle
);
191 inode
->location
.directory_handle
= NULL
;
194 if (pool
->file_count
== 0) {
195 ret
= utils_recursive_rmdir(pool
->unlink_directory_path
);
198 * There is nothing the caller can do, don't report an
199 * error except through logging.
201 PERROR("Failed to remove unlinked files directory at %s",
202 pool
->unlink_directory_path
);
204 lttng_directory_handle_put(pool
->unlink_directory_handle
);
205 pool
->unlink_directory_handle
= NULL
;
211 static void lttng_inode_destroy(struct lttng_inode
*inode
)
218 cds_lfht_del(inode
->registry_ht
, &inode
->registry_node
);
221 if (inode
->unlink_pending
) {
224 assert(inode
->location
.directory_handle
);
225 assert(inode
->location
.path
);
226 DBG("Removing %s from unlinked file pool",
227 inode
->location
.path
);
228 ret
= lttng_unlinked_file_pool_remove_inode(inode
->unlinked_file_pool
, inode
);
230 PERROR("Failed to unlink %s", inode
->location
.path
);
234 lttng_directory_handle_put(
235 inode
->location
.directory_handle
);
236 inode
->location
.directory_handle
= NULL
;
237 free(inode
->location
.path
);
238 inode
->location
.path
= NULL
;
239 call_rcu(&inode
->rcu_head
, lttng_inode_free
);
242 static void lttng_inode_release(struct urcu_ref
*ref
)
244 lttng_inode_destroy(caa_container_of(ref
, struct lttng_inode
, ref
));
247 static void lttng_inode_get(struct lttng_inode
*inode
)
249 urcu_ref_get(&inode
->ref
);
252 struct lttng_unlinked_file_pool
*lttng_unlinked_file_pool_create(
255 struct lttng_unlinked_file_pool
*pool
= zmalloc(sizeof(*pool
));
257 if (!path
|| *path
!= '/') {
258 ERR("Unlinked file pool must be created with an absolute path, path = \"%s\"",
259 path
? path
: "NULL");
263 pool
->unlink_directory_path
= strdup(path
);
264 if (!pool
->unlink_directory_path
) {
265 PERROR("Failed to allocation unlinked file pool path");
268 DBG("Unlinked file pool created at: %s", path
);
271 lttng_unlinked_file_pool_destroy(pool
);
275 void lttng_unlinked_file_pool_destroy(
276 struct lttng_unlinked_file_pool
*pool
)
282 assert(pool
->file_count
== 0);
283 lttng_directory_handle_put(pool
->unlink_directory_handle
);
284 free(pool
->unlink_directory_path
);
288 void lttng_inode_put(struct lttng_inode
*inode
)
290 urcu_ref_put(&inode
->ref
, lttng_inode_release
);
293 void lttng_inode_get_location(struct lttng_inode
*inode
,
294 const struct lttng_directory_handle
**out_directory_handle
,
295 const char **out_path
)
297 if (out_directory_handle
) {
298 *out_directory_handle
= inode
->location
.directory_handle
;
301 *out_path
= inode
->location
.path
;
305 int lttng_inode_rename(
306 struct lttng_inode
*inode
,
307 struct lttng_directory_handle
*old_directory_handle
,
308 const char *old_path
,
309 struct lttng_directory_handle
*new_directory_handle
,
310 const char *new_path
,
314 char *new_path_copy
= strdup(new_path
);
315 bool reference_acquired
;
317 DBG("Performing rename of inode from %s to %s with %s directory handles",
319 lttng_directory_handle_equals(old_directory_handle
,
320 new_directory_handle
) ?
324 if (!new_path_copy
) {
329 if (inode
->unlink_pending
) {
330 WARN("An attempt to rename an unlinked file from %s to %s has been performed",
337 /* Verify that file doesn't exist. */
340 ret
= lttng_directory_handle_stat(
341 new_directory_handle
, new_path
, &statbuf
);
343 ERR("Refusing to rename %s as the destination already exists",
347 } else if (ret
< 0 && errno
!= ENOENT
) {
348 PERROR("Failed to stat() %s", new_path
);
354 ret
= lttng_directory_handle_rename(old_directory_handle
, old_path
,
355 new_directory_handle
, new_path
);
357 PERROR("Failed to rename file %s to %s", old_path
, new_path
);
362 reference_acquired
= lttng_directory_handle_get(new_directory_handle
);
363 assert(reference_acquired
);
364 lttng_directory_handle_put(inode
->location
.directory_handle
);
365 free(inode
->location
.path
);
366 inode
->location
.directory_handle
= new_directory_handle
;
367 /* Ownership transferred. */
368 inode
->location
.path
= new_path_copy
;
369 new_path_copy
= NULL
;
375 int lttng_inode_unlink(struct lttng_inode
*inode
)
379 DBG("Attempting unlink of inode %s", inode
->location
.path
);
381 if (inode
->unlink_pending
) {
382 WARN("An attempt to re-unlink %s has been performed, ignoring.",
383 inode
->location
.path
);
389 * Move to the temporary "deleted" directory until all
390 * references are released.
392 ret
= lttng_unlinked_file_pool_add_inode(
393 inode
->unlinked_file_pool
, inode
);
395 PERROR("Failed to add inode \"%s\" to the unlinked file pool",
396 inode
->location
.path
);
399 inode
->unlink_pending
= true;
404 static struct lttng_inode
*lttng_inode_create(const struct inode_id
*id
,
406 struct lttng_unlinked_file_pool
*unlinked_file_pool
,
407 struct lttng_directory_handle
*directory_handle
,
410 struct lttng_inode
*inode
= NULL
;
412 bool reference_acquired
;
414 path_copy
= strdup(path
);
419 reference_acquired
= lttng_directory_handle_get(directory_handle
);
420 assert(reference_acquired
);
422 inode
= zmalloc(sizeof(*inode
));
427 urcu_ref_init(&inode
->ref
);
428 cds_lfht_node_init(&inode
->registry_node
);
430 inode
->registry_ht
= ht
;
431 inode
->unlinked_file_pool
= unlinked_file_pool
;
432 /* Ownership of path copy is transferred to inode. */
433 inode
->location
.path
= path_copy
;
435 inode
->location
.directory_handle
= directory_handle
;
441 struct lttng_inode_registry
*lttng_inode_registry_create(void)
443 struct lttng_inode_registry
*registry
= zmalloc(sizeof(*registry
));
449 pthread_mutex_lock(&seed
.lock
);
450 if (!seed
.initialized
) {
451 seed
.value
= (unsigned long) time(NULL
);
452 seed
.initialized
= true;
454 pthread_mutex_unlock(&seed
.lock
);
456 registry
->inodes
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
457 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
458 if (!registry
->inodes
) {
464 lttng_inode_registry_destroy(registry
);
468 void lttng_inode_registry_destroy(struct lttng_inode_registry
*registry
)
473 if (registry
->inodes
) {
474 int ret
= cds_lfht_destroy(registry
->inodes
, NULL
);
481 struct lttng_inode
*lttng_inode_registry_get_inode(
482 struct lttng_inode_registry
*registry
,
483 struct lttng_directory_handle
*handle
,
486 struct lttng_unlinked_file_pool
*unlinked_file_pool
)
491 struct cds_lfht_iter iter
;
492 struct cds_lfht_node
*node
;
493 struct lttng_inode
*inode
= NULL
;
495 ret
= fstat(fd
, &statbuf
);
497 PERROR("stat() failed on fd %i", fd
);
501 id
.device
= statbuf
.st_dev
;
502 id
.inode
= statbuf
.st_ino
;
505 cds_lfht_lookup(registry
->inodes
, lttng_inode_id_hash(&id
),
506 lttng_inode_match
, &id
, &iter
);
507 node
= cds_lfht_iter_get_node(&iter
);
509 inode
= caa_container_of(
510 node
, struct lttng_inode
, registry_node
);
511 lttng_inode_get(inode
);
515 inode
= lttng_inode_create(&id
, registry
->inodes
, unlinked_file_pool
,
517 node
= cds_lfht_add_unique(registry
->inodes
,
518 lttng_inode_id_hash(&inode
->id
), lttng_inode_match
,
519 &inode
->id
, &inode
->registry_node
);
520 assert(node
== &inode
->registry_node
);