Cleanup: rotation-thread: enforce conding standard following fix
[lttng-tools.git] / src / common / fd-tracker / inode.cpp
CommitLineData
e0e72660 1/*
ab5be9fa 2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
e0e72660 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
e0e72660 5 *
e0e72660
JG
6 */
7
28ab034a
JG
8#include "inode.hpp"
9
c9e313bc
SM
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>
56047f5a 16#include <common/urcu.hpp>
c9e313bc 17#include <common/utils.hpp>
28ab034a 18
5c1f54d1 19#include <lttng/constant.h>
28ab034a
JG
20
21#include <inttypes.h>
e0e72660 22#include <sys/stat.h>
5c1f54d1 23#include <sys/types.h>
e0e72660 24#include <unistd.h>
e0e72660 25#include <urcu.h>
e0e72660 26#include <urcu/rculfhash.h>
5c1f54d1 27#include <urcu/ref.h>
e0e72660 28
f1494934 29namespace {
e0e72660
JG
30struct inode_id {
31 dev_t device;
32 ino_t inode;
33};
f1494934 34} /* namespace */
e0e72660
JG
35
36struct lttng_inode_registry {
37 /* Hashtable of inode_id to lttng_inode. */
38 struct cds_lfht *inodes;
39};
40
41struct lttng_inode {
42 struct inode_id id;
e0e72660
JG
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;
47 struct urcu_ref ref;
48 struct rcu_head rcu_head;
f7c3ffd7
JG
49 /* Location from which this file can be opened. */
50 struct {
51 struct lttng_directory_handle *directory_handle;
52 char *path;
53 } location;
54 /* Unlink the underlying file at the release of the inode. */
55 bool unlink_pending;
56 LTTNG_OPTIONAL(unsigned int) unlinked_id;
57 /* Weak reference. */
58 struct lttng_unlinked_file_pool *unlinked_file_pool;
59};
60
61struct lttng_unlinked_file_pool {
62 struct lttng_directory_handle *unlink_directory_handle;
63 char *unlink_directory_path;
64 unsigned int file_count;
65 unsigned int next_id;
e0e72660
JG
66};
67
f1494934
JG
68namespace {
69struct {
e0e72660
JG
70 pthread_mutex_t lock;
71 bool initialized;
72 unsigned long value;
73} seed = {
28ab034a
JG
74 .lock = PTHREAD_MUTEX_INITIALIZER,
75 .initialized = false,
76 .value = 0,
e0e72660 77};
f1494934 78} /* namespace */
e0e72660 79
f7c3ffd7 80static unsigned long lttng_inode_id_hash(const struct inode_id *id)
e0e72660
JG
81{
82 uint64_t device = id->device, inode_no = id->inode;
83
28ab034a 84 return hash_key_u64(&device, seed.value) ^ hash_key_u64(&inode_no, seed.value);
e0e72660
JG
85}
86
5c1f54d1 87static int lttng_inode_match(struct cds_lfht_node *node, const void *key)
e0e72660 88{
e032c6fd 89 const struct inode_id *id = (inode_id *) key;
28ab034a
JG
90 const struct lttng_inode *inode =
91 lttng::utils::container_of(node, &lttng_inode::registry_node);
e0e72660
JG
92
93 return inode->id.device == id->device && inode->id.inode == id->inode;
94}
95
f7c3ffd7 96static void lttng_inode_free(struct rcu_head *head)
e0e72660 97{
28ab034a 98 struct lttng_inode *inode = lttng::utils::container_of(head, &lttng_inode::rcu_head);
e0e72660 99
e0e72660
JG
100 free(inode);
101}
102
28ab034a
JG
103static int lttng_unlinked_file_pool_add_inode(struct lttng_unlinked_file_pool *pool,
104 struct lttng_inode *inode)
f7c3ffd7
JG
105{
106 int ret;
107 const unsigned int unlinked_id = pool->next_id++;
108 char *inode_unlinked_name;
109 bool reference_acquired;
110
28ab034a 111 DBG("Adding inode of %s to unlinked file pool as id %u", inode->location.path, unlinked_id);
f7c3ffd7
JG
112 ret = asprintf(&inode_unlinked_name, "%u", unlinked_id);
113 if (ret < 0) {
114 ERR("Failed to format unlinked inode name");
115 ret = -1;
116 goto end;
117 }
118
119 if (pool->file_count == 0) {
28ab034a 120 DBG("Creating unlinked files directory at %s", pool->unlink_directory_path);
a0377dfe 121 LTTNG_ASSERT(!pool->unlink_directory_handle);
28ab034a 122 ret = utils_mkdir(pool->unlink_directory_path, S_IRWXU | S_IRWXG, -1, -1);
f7c3ffd7
JG
123 if (ret) {
124 if (errno == EEXIST) {
125 /*
126 * Unexpected (previous crash?), but not an
127 * error.
128 */
129 DBG("Unlinked file directory \"%s\" already exists",
28ab034a 130 pool->unlink_directory_path);
f7c3ffd7
JG
131 } else {
132 PERROR("Failed to create unlinked files directory at %s",
28ab034a 133 pool->unlink_directory_path);
f7c3ffd7
JG
134 goto end;
135 }
136 }
28ab034a
JG
137 pool->unlink_directory_handle =
138 lttng_directory_handle_create(pool->unlink_directory_path);
d05a3d93
JG
139 if (!pool->unlink_directory_handle) {
140 ERR("Failed to create directory handle to unlinked file pool at %s",
28ab034a 141 pool->unlink_directory_path);
d05a3d93
JG
142 ret = -1;
143 goto end;
144 }
f7c3ffd7
JG
145 }
146
147 ret = lttng_directory_handle_rename(inode->location.directory_handle,
28ab034a
JG
148 inode->location.path,
149 pool->unlink_directory_handle,
150 inode_unlinked_name);
f7c3ffd7
JG
151 if (ret) {
152 goto end;
153 }
154
155 lttng_directory_handle_put(inode->location.directory_handle);
cd9adb8b 156 inode->location.directory_handle = nullptr;
28ab034a 157 reference_acquired = lttng_directory_handle_get(pool->unlink_directory_handle);
a0377dfe 158 LTTNG_ASSERT(reference_acquired);
f7c3ffd7
JG
159 inode->location.directory_handle = pool->unlink_directory_handle;
160
161 free(inode->location.path);
162 inode->location.path = inode_unlinked_name;
cd9adb8b 163 inode_unlinked_name = nullptr;
f7c3ffd7
JG
164 LTTNG_OPTIONAL_SET(&inode->unlinked_id, unlinked_id);
165 pool->file_count++;
166end:
167 free(inode_unlinked_name);
168 return ret;
169}
170
28ab034a
JG
171static int lttng_unlinked_file_pool_remove_inode(struct lttng_unlinked_file_pool *pool,
172 struct lttng_inode *inode)
f7c3ffd7
JG
173{
174 int ret;
175
176 DBG("Removing inode with unlinked id %u from unlinked file pool",
28ab034a 177 LTTNG_OPTIONAL_GET(inode->unlinked_id));
f7c3ffd7 178
28ab034a
JG
179 ret = lttng_directory_handle_unlink_file(inode->location.directory_handle,
180 inode->location.path);
f7c3ffd7
JG
181 if (ret) {
182 PERROR("Failed to unlink file %s from unlinked file directory",
28ab034a 183 inode->location.path);
f7c3ffd7
JG
184 goto end;
185 }
186 free(inode->location.path);
cd9adb8b 187 inode->location.path = nullptr;
f7c3ffd7 188 lttng_directory_handle_put(inode->location.directory_handle);
cd9adb8b 189 inode->location.directory_handle = nullptr;
f7c3ffd7
JG
190
191 pool->file_count--;
192 if (pool->file_count == 0) {
193 ret = utils_recursive_rmdir(pool->unlink_directory_path);
194 if (ret) {
195 /*
196 * There is nothing the caller can do, don't report an
197 * error except through logging.
198 */
199 PERROR("Failed to remove unlinked files directory at %s",
28ab034a 200 pool->unlink_directory_path);
f7c3ffd7
JG
201 }
202 lttng_directory_handle_put(pool->unlink_directory_handle);
cd9adb8b 203 pool->unlink_directory_handle = nullptr;
f7c3ffd7
JG
204 }
205end:
206 return ret;
207}
208
5c1f54d1 209static void lttng_inode_destroy(struct lttng_inode *inode)
e0e72660
JG
210{
211 if (!inode) {
212 return;
213 }
f7c3ffd7 214
56047f5a 215 lttng::urcu::read_lock_guard read_lock;
f7c3ffd7 216 cds_lfht_del(inode->registry_ht, &inode->registry_node);
f7c3ffd7 217
e0e72660 218 if (inode->unlink_pending) {
f7c3ffd7 219 int ret;
e0e72660 220
a0377dfe
FD
221 LTTNG_ASSERT(inode->location.directory_handle);
222 LTTNG_ASSERT(inode->location.path);
28ab034a 223 DBG("Removing %s from unlinked file pool", inode->location.path);
f7c3ffd7 224 ret = lttng_unlinked_file_pool_remove_inode(inode->unlinked_file_pool, inode);
e0e72660 225 if (ret) {
f7c3ffd7 226 PERROR("Failed to unlink %s", inode->location.path);
e0e72660
JG
227 }
228 }
f7c3ffd7 229
28ab034a 230 lttng_directory_handle_put(inode->location.directory_handle);
cd9adb8b 231 inode->location.directory_handle = nullptr;
f7c3ffd7 232 free(inode->location.path);
cd9adb8b 233 inode->location.path = nullptr;
f7c3ffd7 234 call_rcu(&inode->rcu_head, lttng_inode_free);
e0e72660
JG
235}
236
5c1f54d1 237static void lttng_inode_release(struct urcu_ref *ref)
e0e72660 238{
0114db0e 239 lttng_inode_destroy(lttng::utils::container_of(ref, &lttng_inode::ref));
e0e72660
JG
240}
241
5c1f54d1 242static void lttng_inode_get(struct lttng_inode *inode)
e0e72660
JG
243{
244 urcu_ref_get(&inode->ref);
245}
246
28ab034a 247struct lttng_unlinked_file_pool *lttng_unlinked_file_pool_create(const char *path)
f7c3ffd7 248{
64803277 249 struct lttng_unlinked_file_pool *pool = zmalloc<lttng_unlinked_file_pool>();
f7c3ffd7 250
9604bd17
JG
251 if (!pool) {
252 goto error;
253 }
254
f7c3ffd7
JG
255 if (!path || *path != '/') {
256 ERR("Unlinked file pool must be created with an absolute path, path = \"%s\"",
28ab034a 257 path ? path : "NULL");
f7c3ffd7
JG
258 goto error;
259 }
260
261 pool->unlink_directory_path = strdup(path);
262 if (!pool->unlink_directory_path) {
263 PERROR("Failed to allocation unlinked file pool path");
264 goto error;
265 }
266 DBG("Unlinked file pool created at: %s", path);
267 return pool;
268error:
269 lttng_unlinked_file_pool_destroy(pool);
cd9adb8b 270 return nullptr;
f7c3ffd7
JG
271}
272
28ab034a 273void lttng_unlinked_file_pool_destroy(struct lttng_unlinked_file_pool *pool)
f7c3ffd7
JG
274{
275 if (!pool) {
276 return;
277 }
278
a0377dfe 279 LTTNG_ASSERT(pool->file_count == 0);
f7c3ffd7
JG
280 lttng_directory_handle_put(pool->unlink_directory_handle);
281 free(pool->unlink_directory_path);
282 free(pool);
283}
284
ca806b0b 285void lttng_inode_put(struct lttng_inode *inode)
e0e72660
JG
286{
287 urcu_ref_put(&inode->ref, lttng_inode_release);
288}
289
28ab034a 290struct lttng_directory_handle *lttng_inode_get_location_directory_handle(struct lttng_inode *inode)
dd95933f
JG
291{
292 if (inode->location.directory_handle) {
28ab034a
JG
293 const bool reference_acquired =
294 lttng_directory_handle_get(inode->location.directory_handle);
dd95933f 295
a0377dfe 296 LTTNG_ASSERT(reference_acquired);
dd95933f
JG
297 }
298 return inode->location.directory_handle;
299}
300
ca806b0b 301void lttng_inode_borrow_location(struct lttng_inode *inode,
28ab034a
JG
302 const struct lttng_directory_handle **out_directory_handle,
303 const char **out_path)
e0e72660 304{
f7c3ffd7
JG
305 if (out_directory_handle) {
306 *out_directory_handle = inode->location.directory_handle;
307 }
308 if (out_path) {
309 *out_path = inode->location.path;
310 }
e0e72660
JG
311}
312
28ab034a
JG
313int 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,
318 bool overwrite)
e0e72660
JG
319{
320 int ret = 0;
f7c3ffd7
JG
321 char *new_path_copy = strdup(new_path);
322 bool reference_acquired;
323
324 DBG("Performing rename of inode from %s to %s with %s directory handles",
28ab034a
JG
325 old_path,
326 new_path,
327 lttng_directory_handle_equals(old_directory_handle, new_directory_handle) ?
328 "identical" :
329 "different");
f7c3ffd7
JG
330
331 if (!new_path_copy) {
332 ret = -ENOMEM;
333 goto end;
334 }
e0e72660
JG
335
336 if (inode->unlink_pending) {
f7c3ffd7 337 WARN("An attempt to rename an unlinked file from %s to %s has been performed",
28ab034a
JG
338 old_path,
339 new_path);
e0e72660
JG
340 ret = -ENOENT;
341 goto end;
342 }
343
344 if (!overwrite) {
f7c3ffd7 345 /* Verify that file doesn't exist. */
e0e72660
JG
346 struct stat statbuf;
347
28ab034a 348 ret = lttng_directory_handle_stat(new_directory_handle, new_path, &statbuf);
e0e72660 349 if (ret == 0) {
28ab034a 350 ERR("Refusing to rename %s as the destination already exists", old_path);
e0e72660
JG
351 ret = -EEXIST;
352 goto end;
353 } else if (ret < 0 && errno != ENOENT) {
354 PERROR("Failed to stat() %s", new_path);
355 ret = -errno;
356 goto end;
357 }
358 }
359
28ab034a
JG
360 ret = lttng_directory_handle_rename(
361 old_directory_handle, old_path, new_directory_handle, new_path);
e0e72660 362 if (ret) {
f7c3ffd7 363 PERROR("Failed to rename file %s to %s", old_path, new_path);
e0e72660
JG
364 ret = -errno;
365 goto end;
366 }
367
f7c3ffd7 368 reference_acquired = lttng_directory_handle_get(new_directory_handle);
a0377dfe 369 LTTNG_ASSERT(reference_acquired);
f7c3ffd7
JG
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;
cd9adb8b 375 new_path_copy = nullptr;
e0e72660
JG
376end:
377 free(new_path_copy);
378 return ret;
379}
380
ca806b0b 381int lttng_inode_unlink(struct lttng_inode *inode)
e0e72660
JG
382{
383 int ret = 0;
f7c3ffd7
JG
384
385 DBG("Attempting unlink of inode %s", inode->location.path);
e0e72660
JG
386
387 if (inode->unlink_pending) {
388 WARN("An attempt to re-unlink %s has been performed, ignoring.",
28ab034a 389 inode->location.path);
e0e72660
JG
390 ret = -ENOENT;
391 goto end;
392 }
393
f7c3ffd7
JG
394 /*
395 * Move to the temporary "deleted" directory until all
396 * references are released.
397 */
28ab034a 398 ret = lttng_unlinked_file_pool_add_inode(inode->unlinked_file_pool, inode);
f7c3ffd7
JG
399 if (ret) {
400 PERROR("Failed to add inode \"%s\" to the unlinked file pool",
28ab034a 401 inode->location.path);
e0e72660
JG
402 goto end;
403 }
f7c3ffd7 404 inode->unlink_pending = true;
e0e72660
JG
405end:
406 return ret;
407}
408
5c1f54d1 409static struct lttng_inode *lttng_inode_create(const struct inode_id *id,
28ab034a
JG
410 struct cds_lfht *ht,
411 struct lttng_unlinked_file_pool *unlinked_file_pool,
412 struct lttng_directory_handle *directory_handle,
413 const char *path)
e0e72660 414{
cd9adb8b 415 struct lttng_inode *inode = nullptr;
f7c3ffd7
JG
416 char *path_copy;
417 bool reference_acquired;
418
419 path_copy = strdup(path);
420 if (!path_copy) {
421 goto end;
422 }
e0e72660 423
f7c3ffd7 424 reference_acquired = lttng_directory_handle_get(directory_handle);
a0377dfe 425 LTTNG_ASSERT(reference_acquired);
f7c3ffd7 426
64803277 427 inode = zmalloc<lttng_inode>();
e0e72660
JG
428 if (!inode) {
429 goto end;
430 }
431
432 urcu_ref_init(&inode->ref);
433 cds_lfht_node_init(&inode->registry_node);
434 inode->id = *id;
e0e72660 435 inode->registry_ht = ht;
f7c3ffd7
JG
436 inode->unlinked_file_pool = unlinked_file_pool;
437 /* Ownership of path copy is transferred to inode. */
438 inode->location.path = path_copy;
cd9adb8b 439 path_copy = nullptr;
f7c3ffd7 440 inode->location.directory_handle = directory_handle;
e0e72660 441end:
f7c3ffd7 442 free(path_copy);
e0e72660 443 return inode;
e0e72660
JG
444}
445
cd9adb8b 446struct lttng_inode_registry *lttng_inode_registry_create()
e0e72660 447{
64803277 448 struct lttng_inode_registry *registry = zmalloc<lttng_inode_registry>();
e0e72660
JG
449
450 if (!registry) {
451 goto end;
452 }
453
454 pthread_mutex_lock(&seed.lock);
455 if (!seed.initialized) {
cd9adb8b 456 seed.value = (unsigned long) time(nullptr);
e0e72660
JG
457 seed.initialized = true;
458 }
459 pthread_mutex_unlock(&seed.lock);
460
28ab034a 461 registry->inodes = cds_lfht_new(
cd9adb8b 462 DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr);
e0e72660
JG
463 if (!registry->inodes) {
464 goto error;
465 }
466end:
467 return registry;
468error:
469 lttng_inode_registry_destroy(registry);
cd9adb8b 470 return nullptr;
e0e72660
JG
471}
472
28ab034a 473void lttng_inode_registry_destroy(struct lttng_inode_registry *registry)
e0e72660
JG
474{
475 if (!registry) {
476 return;
477 }
478 if (registry->inodes) {
cd9adb8b 479 int ret = cds_lfht_destroy(registry->inodes, nullptr);
e0e72660 480
a0377dfe 481 LTTNG_ASSERT(!ret);
e0e72660
JG
482 }
483 free(registry);
484}
485
28ab034a
JG
486struct lttng_inode *
487lttng_inode_registry_get_inode(struct lttng_inode_registry *registry,
488 struct lttng_directory_handle *handle,
489 const char *path,
490 int fd,
491 struct lttng_unlinked_file_pool *unlinked_file_pool)
e0e72660
JG
492{
493 int ret;
494 struct stat statbuf;
495 struct inode_id id;
496 struct cds_lfht_iter iter;
497 struct cds_lfht_node *node;
cd9adb8b 498 struct lttng_inode *inode = nullptr;
56047f5a 499 lttng::urcu::read_lock_guard read_lock;
e0e72660
JG
500
501 ret = fstat(fd, &statbuf);
502 if (ret < 0) {
f7c3ffd7 503 PERROR("stat() failed on fd %i", fd);
e0e72660
JG
504 goto end;
505 }
506
507 id.device = statbuf.st_dev;
508 id.inode = statbuf.st_ino;
509
28ab034a 510 cds_lfht_lookup(registry->inodes, lttng_inode_id_hash(&id), lttng_inode_match, &id, &iter);
e0e72660
JG
511 node = cds_lfht_iter_get_node(&iter);
512 if (node) {
28ab034a 513 inode = lttng::utils::container_of(node, &lttng_inode::registry_node);
e0e72660 514 lttng_inode_get(inode);
56047f5a 515 goto end;
e0e72660
JG
516 }
517
28ab034a 518 inode = lttng_inode_create(&id, registry->inodes, unlinked_file_pool, handle, path);
640b9481 519 if (!inode) {
56047f5a 520 goto end;
640b9481
JR
521 }
522
e0e72660 523 node = cds_lfht_add_unique(registry->inodes,
28ab034a
JG
524 lttng_inode_id_hash(&inode->id),
525 lttng_inode_match,
526 &inode->id,
527 &inode->registry_node);
a0377dfe 528 LTTNG_ASSERT(node == &inode->registry_node);
e0e72660
JG
529end:
530 return inode;
531}
This page took 0.07187 seconds and 4 git commands to generate.