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