Tests: fd-tracker: fix: leak of test paths
[lttng-tools.git] / src / common / fd-tracker / fd-tracker.c
CommitLineData
df038819 1/*
ab5be9fa 2 * Copyright (C) 2018-2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
df038819 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
df038819 5 *
df038819
JG
6 */
7
8#include <urcu.h>
9#include <urcu/list.h>
10#include <urcu/rculfhash.h>
11
df038819 12#include <fcntl.h>
df038819 13#include <inttypes.h>
5c1f54d1
JG
14#include <pthread.h>
15#include <stdbool.h>
16#include <sys/stat.h>
17#include <sys/types.h>
df038819 18
f5ea0241
JG
19#include <common/defaults.h>
20#include <common/error.h>
1e4f5625 21#include <common/fs-handle-internal.h>
f5ea0241
JG
22#include <common/hashtable/hashtable.h>
23#include <common/hashtable/utils.h>
24#include <common/macros.h>
1e4f5625 25#include <common/optional.h>
df038819
JG
26
27#include "fd-tracker.h"
874a3441 28#include "inode.h"
df038819
JG
29
30/* Tracker lock must be taken by the user. */
5c1f54d1
JG
31#define TRACKED_COUNT(tracker) \
32 (tracker->count.suspendable.active + \
33 tracker->count.suspendable.suspended + \
34 tracker->count.unsuspendable)
df038819
JG
35
36/* Tracker lock must be taken by the user. */
5c1f54d1
JG
37#define ACTIVE_COUNT(tracker) \
38 (tracker->count.suspendable.active + tracker->count.unsuspendable)
df038819
JG
39
40/* Tracker lock must be taken by the user. */
5c1f54d1 41#define SUSPENDED_COUNT(tracker) (tracker->count.suspendable.suspended)
df038819
JG
42
43/* Tracker lock must be taken by the user. */
5c1f54d1
JG
44#define SUSPENDABLE_COUNT(tracker) \
45 (tracker->count.suspendable.active + \
46 tracker->count.suspendable.suspended)
df038819
JG
47
48/* Tracker lock must be taken by the user. */
5c1f54d1 49#define UNSUSPENDABLE_COUNT(tracker) (tracker->count.unsuspendable)
df038819
JG
50
51struct fd_tracker {
52 pthread_mutex_t lock;
53 struct {
5c1f54d1 54 struct {
df038819
JG
55 unsigned int active;
56 unsigned int suspended;
57 } suspendable;
58 unsigned int unsuspendable;
59 } count;
60 unsigned int capacity;
61 struct {
62 uint64_t uses;
63 uint64_t misses;
64 /* Failures to suspend or restore fs handles. */
65 uint64_t errors;
66 } stats;
67 /*
68 * The head of the active_handles list is always the least recently
69 * used active handle. When an handle is used, it is removed from the
70 * list and added to the end. When a file has to be suspended, the
71 * first element in the list is "popped", suspended, and added to the
72 * list of suspended handles.
73 */
74 struct cds_list_head active_handles;
75 struct cds_list_head suspended_handles;
76 struct cds_lfht *unsuspendable_fds;
874a3441 77 struct lttng_inode_registry *inode_registry;
f7c3ffd7
JG
78 /* Unlinked files are moved in this directory under a unique name. */
79 struct lttng_directory_handle *unlink_directory_handle;
80 struct lttng_unlinked_file_pool *unlinked_file_pool;
df038819
JG
81};
82
83struct open_properties {
df038819 84 int flags;
1e4f5625 85 LTTNG_OPTIONAL(mode_t) mode;
df038819
JG
86};
87
88/*
f5ea0241 89 * A fs_handle_tracked is not ref-counted. Therefore, it is assumed that a
df038819
JG
90 * handle is never in-use while it is being reclaimed. It can be
91 * shared by multiple threads, but external synchronization is required
92 * to ensure it is not still being used when it is reclaimed (close method).
93 * In this respect, it is not different from a regular file descriptor.
94 *
95 * The fs_handle lock always nests _within_ the tracker's lock.
96 */
f5ea0241
JG
97struct fs_handle_tracked {
98 struct fs_handle parent;
df038819
JG
99 pthread_mutex_t lock;
100 /*
101 * Weak reference to the tracker. All fs_handles are assumed to have
102 * been closed at the moment of the destruction of the fd_tracker.
103 */
104 struct fd_tracker *tracker;
105 struct open_properties properties;
874a3441 106 struct lttng_inode *inode;
df038819
JG
107 int fd;
108 /* inode number of the file at the time of the handle's creation. */
109 uint64_t ino;
110 bool in_use;
111 /* Offset to which the file should be restored. */
112 off_t offset;
113 struct cds_list_head handles_list_node;
114};
115
116struct unsuspendable_fd {
117 /*
118 * Accesses are only performed through the tracker, which is protected
119 * by its own lock.
120 */
121 int fd;
122 char *name;
123 struct cds_lfht_node tracker_node;
124 struct rcu_head rcu_head;
125};
126
127static struct {
128 pthread_mutex_t lock;
129 bool initialized;
130 unsigned long value;
131} seed = {
132 .lock = PTHREAD_MUTEX_INITIALIZER,
133};
134
135static int match_fd(struct cds_lfht_node *node, const void *key);
136static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry);
5c1f54d1
JG
137static struct unsuspendable_fd *unsuspendable_fd_create(
138 const char *name, int fd);
f7c3ffd7 139static int open_from_properties(const struct lttng_directory_handle *dir_handle,
5c1f54d1 140 const char *path, struct open_properties *properties);
df038819 141
f5ea0241
JG
142static void fs_handle_tracked_log(struct fs_handle_tracked *handle);
143static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle);
144static int fs_handle_tracked_restore(struct fs_handle_tracked *handle);
145static int fs_handle_tracked_get_fd(struct fs_handle *_handle);
146static void fs_handle_tracked_put_fd(struct fs_handle *_handle);
147static int fs_handle_tracked_unlink(struct fs_handle *_handle);
148static int fs_handle_tracked_close(struct fs_handle *_handle);
df038819 149
5c1f54d1 150static void fd_tracker_track(
f5ea0241 151 struct fd_tracker *tracker, struct fs_handle_tracked *handle);
5c1f54d1 152static void fd_tracker_untrack(
f5ea0241 153 struct fd_tracker *tracker, struct fs_handle_tracked *handle);
5c1f54d1
JG
154static int fd_tracker_suspend_handles(
155 struct fd_tracker *tracker, unsigned int count);
156static int fd_tracker_restore_handle(
f5ea0241
JG
157 struct fd_tracker *tracker, struct fs_handle_tracked *handle);
158
df038819 159/* Match function of the tracker's unsuspendable_fds hash table. */
5c1f54d1 160static int match_fd(struct cds_lfht_node *node, const void *key)
df038819 161{
5c1f54d1
JG
162 struct unsuspendable_fd *entry = caa_container_of(
163 node, struct unsuspendable_fd, tracker_node);
df038819 164
5c1f54d1
JG
165 return hash_match_key_ulong(
166 (void *) (unsigned long) entry->fd, (void *) key);
df038819
JG
167}
168
5c1f54d1 169static void delete_unsuspendable_fd(struct rcu_head *head)
df038819 170{
5c1f54d1
JG
171 struct unsuspendable_fd *fd = caa_container_of(
172 head, struct unsuspendable_fd, rcu_head);
df038819
JG
173
174 free(fd->name);
175 free(fd);
176}
177
5c1f54d1 178static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry)
df038819
JG
179{
180 if (!entry) {
181 return;
182 }
183 call_rcu(&entry->rcu_head, delete_unsuspendable_fd);
184}
185
5c1f54d1
JG
186static struct unsuspendable_fd *unsuspendable_fd_create(
187 const char *name, int fd)
df038819 188{
5c1f54d1 189 struct unsuspendable_fd *entry = zmalloc(sizeof(*entry));
df038819
JG
190
191 if (!entry) {
192 goto error;
193 }
194 if (name) {
195 entry->name = strdup(name);
196 if (!entry->name) {
197 goto error;
198 }
199 }
200 cds_lfht_node_init(&entry->tracker_node);
201 entry->fd = fd;
202 return entry;
203error:
204 unsuspendable_fd_destroy(entry);
205 return NULL;
206}
207
f5ea0241 208static void fs_handle_tracked_log(struct fs_handle_tracked *handle)
df038819 209{
874a3441
JG
210 const char *path;
211
df038819 212 pthread_mutex_lock(&handle->lock);
dd95933f 213 lttng_inode_borrow_location(handle->inode, NULL, &path);
874a3441 214
df038819 215 if (handle->fd >= 0) {
5c1f54d1 216 DBG_NO_LOC(" %s [active, fd %d%s]", path, handle->fd,
df038819
JG
217 handle->in_use ? ", in use" : "");
218 } else {
874a3441 219 DBG_NO_LOC(" %s [suspended]", path);
df038819
JG
220 }
221 pthread_mutex_unlock(&handle->lock);
222}
223
874a3441 224/* Tracker lock must be held by the caller. */
f5ea0241 225static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle)
df038819
JG
226{
227 int ret = 0;
228 struct stat fs_stat;
874a3441 229 const char *path;
f7c3ffd7 230 const struct lttng_directory_handle *node_directory_handle;
df038819
JG
231
232 pthread_mutex_lock(&handle->lock);
dd95933f
JG
233 lttng_inode_borrow_location(
234 handle->inode, &node_directory_handle, &path);
df038819
JG
235 assert(handle->fd >= 0);
236 if (handle->in_use) {
237 /* This handle can't be suspended as it is currently in use. */
238 ret = -EAGAIN;
239 goto end;
240 }
241
f7c3ffd7
JG
242 ret = lttng_directory_handle_stat(
243 node_directory_handle, path, &fs_stat);
df038819 244 if (ret) {
5c1f54d1 245 PERROR("Filesystem handle to %s cannot be suspended as stat() failed",
874a3441 246 path);
df038819
JG
247 ret = -errno;
248 goto end;
249 }
250
251 if (fs_stat.st_ino != handle->ino) {
252 /* Don't suspend as the handle would not be restorable. */
253 WARN("Filesystem handle to %s cannot be suspended as its inode changed",
874a3441 254 path);
df038819
JG
255 ret = -ENOENT;
256 goto end;
257 }
258
5c1f54d1 259 handle->offset = lseek(handle->fd, 0, SEEK_CUR);
df038819
JG
260 if (handle->offset == -1) {
261 WARN("Filesystem handle to %s cannot be suspended as lseek() failed to sample its current position",
874a3441 262 path);
df038819
JG
263 ret = -errno;
264 goto end;
265 }
266
267 ret = close(handle->fd);
268 if (ret) {
5c1f54d1 269 PERROR("Filesystem handle to %s cannot be suspended as close() failed",
874a3441 270 path);
df038819
JG
271 ret = -errno;
272 goto end;
273 }
274 DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64,
874a3441 275 path, handle->fd, handle->offset);
df038819
JG
276 handle->fd = -1;
277end:
278 if (ret) {
279 handle->tracker->stats.errors++;
280 }
281 pthread_mutex_unlock(&handle->lock);
282 return ret;
283}
284
285/* Caller must hold the tracker and handle's locks. */
f5ea0241 286static int fs_handle_tracked_restore(struct fs_handle_tracked *handle)
df038819
JG
287{
288 int ret, fd = -1;
f7c3ffd7
JG
289 const char *path;
290 const struct lttng_directory_handle *node_directory_handle;
291
dd95933f
JG
292 lttng_inode_borrow_location(
293 handle->inode, &node_directory_handle, &path);
df038819
JG
294
295 assert(handle->fd == -1);
874a3441 296 assert(path);
f7c3ffd7
JG
297 ret = open_from_properties(
298 node_directory_handle, path, &handle->properties);
df038819 299 if (ret < 0) {
5c1f54d1 300 PERROR("Failed to restore filesystem handle to %s, open() failed",
874a3441 301 path);
df038819
JG
302 ret = -errno;
303 goto end;
304 }
305 fd = ret;
306
307 ret = lseek(fd, handle->offset, SEEK_SET);
308 if (ret < 0) {
5c1f54d1 309 PERROR("Failed to restore filesystem handle to %s, lseek() failed",
874a3441 310 path);
df038819
JG
311 ret = -errno;
312 goto end;
313 }
314 DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64,
874a3441 315 path, fd, handle->offset);
df038819
JG
316 ret = 0;
317 handle->fd = fd;
318 fd = -1;
319end:
320 if (fd >= 0) {
321 (void) close(fd);
322 }
323 return ret;
324}
325
f7c3ffd7 326static int open_from_properties(const struct lttng_directory_handle *dir_handle,
5c1f54d1 327 const char *path, struct open_properties *properties)
df038819
JG
328{
329 int ret;
330
331 /*
332 * open() ignores the 'flags' parameter unless the O_CREAT or O_TMPFILE
333 * flags are set. O_TMPFILE would not make sense in the context of a
334 * suspendable fs_handle as it would not be restorable (see OPEN(2)),
335 * thus it is ignored here.
336 */
337 if ((properties->flags & O_CREAT) && properties->mode.is_set) {
f7c3ffd7
JG
338 ret = lttng_directory_handle_open_file(dir_handle, path,
339 properties->flags, properties->mode.value);
df038819 340 } else {
f7c3ffd7
JG
341 ret = lttng_directory_handle_open_file(dir_handle, path,
342 properties->flags, 0);
df038819
JG
343 }
344 /*
345 * Some flags should not be used beyond the initial open() of a
346 * restorable file system handle. O_CREAT and O_TRUNC must
347 * be cleared since it would be unexpected to re-use them
348 * when the handle is retored:
349 * - O_CREAT should not be needed as the file has been created
350 * on the initial call to open(),
351 * - O_TRUNC would destroy the file's contents by truncating it
352 * to length 0.
353 */
354 properties->flags &= ~(O_CREAT | O_TRUNC);
355 if (ret < 0) {
356 ret = -errno;
357 goto end;
358 }
359end:
360 return ret;
361}
362
f7c3ffd7
JG
363struct fd_tracker *fd_tracker_create(const char *unlinked_file_path,
364 unsigned int capacity)
df038819
JG
365{
366 struct fd_tracker *tracker = zmalloc(sizeof(struct fd_tracker));
367
368 if (!tracker) {
369 goto end;
370 }
371
372 pthread_mutex_lock(&seed.lock);
373 if (!seed.initialized) {
374 seed.value = (unsigned long) time(NULL);
375 seed.initialized = true;
376 }
377 pthread_mutex_unlock(&seed.lock);
378
379 CDS_INIT_LIST_HEAD(&tracker->active_handles);
380 CDS_INIT_LIST_HEAD(&tracker->suspended_handles);
381 tracker->capacity = capacity;
382 tracker->unsuspendable_fds = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
383 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
874a3441
JG
384 if (!tracker->unsuspendable_fds) {
385 ERR("Failed to create fd-tracker's unsuspendable_fds hash table");
386 goto error;
387 }
388 tracker->inode_registry = lttng_inode_registry_create();
389 if (!tracker->inode_registry) {
390 ERR("Failed to create fd-tracker's inode registry");
391 goto error;
392 }
f7c3ffd7
JG
393
394 tracker->unlinked_file_pool =
395 lttng_unlinked_file_pool_create(unlinked_file_path);
396 if (!tracker->unlinked_file_pool) {
397 goto error;
398 }
df038819
JG
399 DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
400 capacity);
401end:
402 return tracker;
874a3441
JG
403error:
404 fd_tracker_destroy(tracker);
405 return NULL;
df038819
JG
406}
407
408void fd_tracker_log(struct fd_tracker *tracker)
409{
f5ea0241 410 struct fs_handle_tracked *handle;
df038819
JG
411 struct unsuspendable_fd *unsuspendable_fd;
412 struct cds_lfht_iter iter;
413
414 pthread_mutex_lock(&tracker->lock);
415 DBG_NO_LOC("File descriptor tracker");
416 DBG_NO_LOC(" Stats:");
417 DBG_NO_LOC(" uses: %" PRIu64, tracker->stats.uses);
418 DBG_NO_LOC(" misses: %" PRIu64, tracker->stats.misses);
419 DBG_NO_LOC(" errors: %" PRIu64, tracker->stats.errors);
420 DBG_NO_LOC(" Tracked: %u", TRACKED_COUNT(tracker));
421 DBG_NO_LOC(" active: %u", ACTIVE_COUNT(tracker));
422 DBG_NO_LOC(" suspendable: %u", SUSPENDABLE_COUNT(tracker));
423 DBG_NO_LOC(" unsuspendable: %u", UNSUSPENDABLE_COUNT(tracker));
424 DBG_NO_LOC(" suspended: %u", SUSPENDED_COUNT(tracker));
425 DBG_NO_LOC(" capacity: %u", tracker->capacity);
426
427 DBG_NO_LOC(" Tracked suspendable file descriptors");
f5ea0241 428 cds_list_for_each_entry(
5c1f54d1 429 handle, &tracker->active_handles, handles_list_node) {
f5ea0241 430 fs_handle_tracked_log(handle);
df038819 431 }
f5ea0241 432 cds_list_for_each_entry(handle, &tracker->suspended_handles,
df038819 433 handles_list_node) {
f5ea0241 434 fs_handle_tracked_log(handle);
df038819
JG
435 }
436 if (!SUSPENDABLE_COUNT(tracker)) {
437 DBG_NO_LOC(" None");
438 }
439
440 DBG_NO_LOC(" Tracked unsuspendable file descriptors");
441 rcu_read_lock();
f5ea0241 442 cds_lfht_for_each_entry(tracker->unsuspendable_fds, &iter,
df038819 443 unsuspendable_fd, tracker_node) {
5c1f54d1
JG
444 DBG_NO_LOC(" %s [active, fd %d]",
445 unsuspendable_fd->name ?: "Unnamed",
df038819
JG
446 unsuspendable_fd->fd);
447 }
448 rcu_read_unlock();
449 if (!UNSUSPENDABLE_COUNT(tracker)) {
450 DBG_NO_LOC(" None");
451 }
452
453 pthread_mutex_unlock(&tracker->lock);
454}
455
456int fd_tracker_destroy(struct fd_tracker *tracker)
457{
458 int ret = 0;
459
f6bef966
JG
460 if (!tracker) {
461 goto end;
462 }
df038819
JG
463 /*
464 * Refuse to destroy the tracker as fs_handles may still old
465 * weak references to the tracker.
466 */
467 pthread_mutex_lock(&tracker->lock);
468 if (TRACKED_COUNT(tracker)) {
469 ERR("A file descriptor leak has been detected: %u tracked file descriptors are still being tracked",
470 TRACKED_COUNT(tracker));
471 pthread_mutex_unlock(&tracker->lock);
472 fd_tracker_log(tracker);
473 ret = -1;
474 goto end;
475 }
476 pthread_mutex_unlock(&tracker->lock);
477
874a3441
JG
478 if (tracker->unsuspendable_fds) {
479 ret = cds_lfht_destroy(tracker->unsuspendable_fds, NULL);
480 assert(!ret);
481 }
660a216c
JG
482
483 lttng_inode_registry_destroy(tracker->inode_registry);
f7c3ffd7 484 lttng_unlinked_file_pool_destroy(tracker->unlinked_file_pool);
df038819
JG
485 pthread_mutex_destroy(&tracker->lock);
486 free(tracker);
487end:
488 return ret;
489}
490
491struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
f7c3ffd7 492 struct lttng_directory_handle *directory,
5c1f54d1
JG
493 const char *path,
494 int flags,
495 mode_t *mode)
df038819
JG
496{
497 int ret;
f5ea0241 498 struct fs_handle_tracked *handle = NULL;
df038819
JG
499 struct stat fd_stat;
500 struct open_properties properties = {
df038819
JG
501 .flags = flags,
502 .mode.is_set = !!mode,
503 .mode.value = mode ? *mode : 0,
504 };
505
df038819
JG
506 pthread_mutex_lock(&tracker->lock);
507 if (ACTIVE_COUNT(tracker) == tracker->capacity) {
508 if (tracker->count.suspendable.active > 0) {
509 ret = fd_tracker_suspend_handles(tracker, 1);
510 if (ret) {
19380ea8 511 goto end;
df038819
JG
512 }
513 } else {
514 /*
515 * There are not enough active suspendable file
19380ea8
JG
516 * descriptors to open a new fd and still accommodate
517 * the tracker's capacity.
df038819
JG
518 */
519 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
520 tracker->count.unsuspendable);
19380ea8 521 goto end;
df038819
JG
522 }
523 }
524
525 handle = zmalloc(sizeof(*handle));
526 if (!handle) {
527 goto end;
528 }
f7c3ffd7
JG
529 handle->parent = (typeof(handle->parent)) {
530 .get_fd = fs_handle_tracked_get_fd,
531 .put_fd = fs_handle_tracked_put_fd,
532 .unlink = fs_handle_tracked_unlink,
533 .close = fs_handle_tracked_close,
534 };
535
660a216c 536 handle->tracker = tracker;
df038819
JG
537
538 ret = pthread_mutex_init(&handle->lock, NULL);
539 if (ret) {
540 PERROR("Failed to initialize handle mutex while creating fs handle");
19380ea8 541 goto error_mutex_init;
df038819
JG
542 }
543
f7c3ffd7 544 handle->fd = open_from_properties(directory, path, &properties);
df038819
JG
545 if (handle->fd < 0) {
546 PERROR("Failed to open fs handle to %s, open() returned", path);
19380ea8 547 goto error;
df038819
JG
548 }
549
550 handle->properties = properties;
874a3441 551
f7c3ffd7
JG
552 handle->inode = lttng_inode_registry_get_inode(tracker->inode_registry,
553 directory, path, handle->fd,
554 tracker->unlinked_file_pool);
874a3441 555 if (!handle->inode) {
5c1f54d1 556 ERR("Failed to get lttng_inode corresponding to file %s", path);
19380ea8 557 goto error;
874a3441 558 }
df038819
JG
559
560 if (fstat(handle->fd, &fd_stat)) {
561 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
19380ea8 562 goto error;
df038819
JG
563 }
564 handle->ino = fd_stat.st_ino;
565
566 fd_tracker_track(tracker, handle);
df038819 567end:
19380ea8 568 pthread_mutex_unlock(&tracker->lock);
f5ea0241 569 return handle ? &handle->parent : NULL;
19380ea8 570error:
660a216c
JG
571 if (handle->inode) {
572 lttng_inode_put(handle->inode);
573 }
19380ea8
JG
574 pthread_mutex_destroy(&handle->lock);
575error_mutex_init:
660a216c 576 free(handle);
df038819
JG
577 handle = NULL;
578 goto end;
579}
580
581/* Caller must hold the tracker's lock. */
5c1f54d1
JG
582static int fd_tracker_suspend_handles(
583 struct fd_tracker *tracker, unsigned int count)
df038819
JG
584{
585 unsigned int left_to_close = count;
f7c3ffd7 586 unsigned int attempts_left = tracker->count.suspendable.active;
f5ea0241 587 struct fs_handle_tracked *handle, *tmp;
df038819 588
f5ea0241 589 cds_list_for_each_entry_safe(handle, tmp, &tracker->active_handles,
df038819
JG
590 handles_list_node) {
591 int ret;
592
593 fd_tracker_untrack(tracker, handle);
f5ea0241 594 ret = fs_handle_tracked_suspend(handle);
df038819
JG
595 fd_tracker_track(tracker, handle);
596 if (!ret) {
597 left_to_close--;
598 }
f7c3ffd7 599 attempts_left--;
df038819 600
f7c3ffd7 601 if (left_to_close == 0 || attempts_left == 0) {
df038819
JG
602 break;
603 }
604 }
605 return left_to_close ? -EMFILE : 0;
606}
607
608int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
5c1f54d1
JG
609 int *out_fds,
610 const char **names,
611 unsigned int fd_count,
612 fd_open_cb open,
613 void *user_data)
df038819
JG
614{
615 int ret, user_ret, i, fds_to_suspend;
616 unsigned int active_fds;
b13a2b17 617 struct unsuspendable_fd **entries;
df038819 618
b13a2b17
JG
619 entries = zmalloc(fd_count * sizeof(*entries));
620 if (!entries) {
621 ret = -1;
622 goto end;
623 }
df038819
JG
624
625 pthread_mutex_lock(&tracker->lock);
626
627 active_fds = ACTIVE_COUNT(tracker);
5c1f54d1
JG
628 fds_to_suspend = (int) active_fds + (int) fd_count -
629 (int) tracker->capacity;
df038819
JG
630 if (fds_to_suspend > 0) {
631 if (fds_to_suspend <= tracker->count.suspendable.active) {
5c1f54d1
JG
632 ret = fd_tracker_suspend_handles(
633 tracker, fds_to_suspend);
df038819 634 if (ret) {
b13a2b17 635 goto end_unlock;
df038819
JG
636 }
637 } else {
638 /*
639 * There are not enough active suspendable file
640 * descriptors to open a new fd and still accomodate the
641 * tracker's capacity.
642 */
643 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
644 tracker->count.unsuspendable);
645 ret = -EMFILE;
b13a2b17 646 goto end_unlock;
df038819
JG
647 }
648 }
649
650 user_ret = open(user_data, out_fds);
651 if (user_ret) {
652 ret = user_ret;
b13a2b17 653 goto end_unlock;
df038819
JG
654 }
655
656 /*
657 * Add the fds returned by the user's callback to the hashtable
658 * of unsuspendable fds.
659 */
660 for (i = 0; i < fd_count; i++) {
5c1f54d1
JG
661 struct unsuspendable_fd *entry = unsuspendable_fd_create(
662 names ? names[i] : NULL, out_fds[i]);
df038819
JG
663
664 if (!entry) {
665 ret = -1;
666 goto end_free_entries;
667 }
668 entries[i] = entry;
669 }
670
671 rcu_read_lock();
672 for (i = 0; i < fd_count; i++) {
673 struct cds_lfht_node *node;
674 struct unsuspendable_fd *entry = entries[i];
675
5c1f54d1
JG
676 node = cds_lfht_add_unique(tracker->unsuspendable_fds,
677 hash_key_ulong((void *) (unsigned long)
678 out_fds[i],
df038819 679 seed.value),
5c1f54d1 680 match_fd, (void *) (unsigned long) out_fds[i],
df038819
JG
681 &entry->tracker_node);
682
683 if (node != &entry->tracker_node) {
684 ret = -EEXIST;
685 rcu_read_unlock();
686 goto end_free_entries;
687 }
688 entries[i] = NULL;
689 }
690 tracker->count.unsuspendable += fd_count;
691 rcu_read_unlock();
692 ret = user_ret;
b13a2b17 693end_unlock:
df038819 694 pthread_mutex_unlock(&tracker->lock);
b13a2b17
JG
695end:
696 free(entries);
df038819
JG
697 return ret;
698end_free_entries:
699 for (i = 0; i < fd_count; i++) {
700 unsuspendable_fd_destroy(entries[i]);
701 }
b13a2b17 702 goto end_unlock;
df038819
JG
703}
704
705int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
5c1f54d1
JG
706 int *fds_in,
707 unsigned int fd_count,
708 fd_close_cb close,
df038819
JG
709 void *user_data)
710{
711 int i, ret, user_ret;
b13a2b17 712 int *fds = NULL;
df038819
JG
713
714 /*
715 * Maintain a local copy of fds_in as the user's callback may modify its
716 * contents (e.g. setting the fd(s) to -1 after close).
717 */
b13a2b17
JG
718 fds = malloc(sizeof(*fds) * fd_count);
719 if (!fds) {
720 ret = -1;
721 goto end;
722 }
df038819
JG
723 memcpy(fds, fds_in, sizeof(*fds) * fd_count);
724
725 pthread_mutex_lock(&tracker->lock);
726 rcu_read_lock();
727
728 /* Let the user close the file descriptors. */
729 user_ret = close(user_data, fds_in);
730 if (user_ret) {
731 ret = user_ret;
b13a2b17 732 goto end_unlock;
df038819
JG
733 }
734
735 /* Untrack the fds that were just closed by the user's callback. */
736 for (i = 0; i < fd_count; i++) {
737 struct cds_lfht_node *node;
738 struct cds_lfht_iter iter;
739 struct unsuspendable_fd *entry;
740
741 cds_lfht_lookup(tracker->unsuspendable_fds,
742 hash_key_ulong((void *) (unsigned long) fds[i],
743 seed.value),
5c1f54d1 744 match_fd, (void *) (unsigned long) fds[i],
df038819
JG
745 &iter);
746 node = cds_lfht_iter_get_node(&iter);
747 if (!node) {
748 /* Unknown file descriptor. */
749 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
750 fds[i]);
751 ret = -EINVAL;
b13a2b17 752 goto end_unlock;
df038819 753 }
5c1f54d1
JG
754 entry = caa_container_of(
755 node, struct unsuspendable_fd, tracker_node);
df038819
JG
756
757 cds_lfht_del(tracker->unsuspendable_fds, node);
758 unsuspendable_fd_destroy(entry);
759 fds[i] = -1;
760 }
761
762 tracker->count.unsuspendable -= fd_count;
763 ret = 0;
b13a2b17 764end_unlock:
df038819
JG
765 rcu_read_unlock();
766 pthread_mutex_unlock(&tracker->lock);
b13a2b17
JG
767 free(fds);
768end:
df038819
JG
769 return ret;
770}
771
772/* Caller must have taken the tracker's and handle's locks. */
5c1f54d1 773static void fd_tracker_track(
f5ea0241 774 struct fd_tracker *tracker, struct fs_handle_tracked *handle)
df038819
JG
775{
776 if (handle->fd >= 0) {
777 tracker->count.suspendable.active++;
778 cds_list_add_tail(&handle->handles_list_node,
779 &tracker->active_handles);
780 } else {
781 tracker->count.suspendable.suspended++;
782 cds_list_add_tail(&handle->handles_list_node,
783 &tracker->suspended_handles);
784 }
785}
786
787/* Caller must have taken the tracker's and handle's locks. */
5c1f54d1 788static void fd_tracker_untrack(
f5ea0241 789 struct fd_tracker *tracker, struct fs_handle_tracked *handle)
df038819
JG
790{
791 if (handle->fd >= 0) {
792 tracker->count.suspendable.active--;
793 } else {
794 tracker->count.suspendable.suspended--;
795 }
796 cds_list_del(&handle->handles_list_node);
797}
798
799/* Caller must have taken the tracker's and handle's locks. */
5c1f54d1 800static int fd_tracker_restore_handle(
f5ea0241 801 struct fd_tracker *tracker, struct fs_handle_tracked *handle)
df038819
JG
802{
803 int ret;
804
805 fd_tracker_untrack(tracker, handle);
806 if (ACTIVE_COUNT(tracker) >= tracker->capacity) {
807 ret = fd_tracker_suspend_handles(tracker, 1);
808 if (ret) {
809 goto end;
810 }
811 }
f5ea0241 812 ret = fs_handle_tracked_restore(handle);
df038819
JG
813end:
814 fd_tracker_track(tracker, handle);
815 return ret ? ret : handle->fd;
816}
817
f5ea0241 818static int fs_handle_tracked_get_fd(struct fs_handle *_handle)
df038819
JG
819{
820 int ret;
f5ea0241
JG
821 struct fs_handle_tracked *handle =
822 container_of(_handle, struct fs_handle_tracked, parent);
df038819
JG
823
824 /*
825 * TODO This should be optimized as it is a fairly hot path.
826 * The fd-tracker's lock should only be taken when a fs_handle is
827 * restored (slow path). On the fast path (fs_handle is active),
828 * the only effect on the fd_tracker is marking the handle as the
829 * most recently used. Currently, it is done by a call to the
830 * track/untrack helpers, but it should be done atomically.
831 *
832 * Note that the lock's nesting order must still be respected here.
833 * The handle's lock nests inside the tracker's lock.
834 */
835 pthread_mutex_lock(&handle->tracker->lock);
836 pthread_mutex_lock(&handle->lock);
837 assert(!handle->in_use);
838
839 handle->tracker->stats.uses++;
840 if (handle->fd >= 0) {
841 ret = handle->fd;
842 /* Mark as most recently used. */
843 fd_tracker_untrack(handle->tracker, handle);
844 fd_tracker_track(handle->tracker, handle);
845 } else {
846 handle->tracker->stats.misses++;
847 ret = fd_tracker_restore_handle(handle->tracker, handle);
848 if (ret < 0) {
849 handle->tracker->stats.errors++;
850 goto end;
851 }
852 }
853 handle->in_use = true;
854end:
855 pthread_mutex_unlock(&handle->lock);
856 pthread_mutex_unlock(&handle->tracker->lock);
857 return ret;
858}
859
f5ea0241 860static void fs_handle_tracked_put_fd(struct fs_handle *_handle)
df038819 861{
f5ea0241
JG
862 struct fs_handle_tracked *handle =
863 container_of(_handle, struct fs_handle_tracked, parent);
864
df038819
JG
865 pthread_mutex_lock(&handle->lock);
866 handle->in_use = false;
867 pthread_mutex_unlock(&handle->lock);
868}
869
f5ea0241 870static int fs_handle_tracked_unlink(struct fs_handle *_handle)
9d16fc7f
JG
871{
872 int ret;
f5ea0241
JG
873 struct fs_handle_tracked *handle =
874 container_of(_handle, struct fs_handle_tracked, parent);
9d16fc7f
JG
875
876 pthread_mutex_lock(&handle->tracker->lock);
877 pthread_mutex_lock(&handle->lock);
f7c3ffd7 878 ret = lttng_inode_unlink(handle->inode);
9d16fc7f
JG
879 pthread_mutex_unlock(&handle->lock);
880 pthread_mutex_unlock(&handle->tracker->lock);
881 return ret;
882}
883
f5ea0241 884static int fs_handle_tracked_close(struct fs_handle *_handle)
df038819
JG
885{
886 int ret = 0;
660a216c 887 const char *path = NULL;
f5ea0241
JG
888 struct fs_handle_tracked *handle =
889 container_of(_handle, struct fs_handle_tracked, parent);
dd95933f 890 struct lttng_directory_handle *inode_directory_handle = NULL;
df038819
JG
891
892 if (!handle) {
893 ret = -EINVAL;
894 goto end;
895 }
896
897 pthread_mutex_lock(&handle->tracker->lock);
898 pthread_mutex_lock(&handle->lock);
660a216c 899 if (handle->inode) {
dd95933f
JG
900 lttng_inode_borrow_location(handle->inode, NULL, &path);
901 /*
902 * Here a reference to the inode's directory handle is acquired
903 * to prevent the last reference to it from being released while
904 * the tracker's lock is taken.
905 *
906 * If this wasn't done, the directory handle could attempt to
907 * close its underlying directory file descriptor, which would
908 * attempt to lock the tracker's lock, resulting in a deadlock.
909 *
910 * Since a new reference to the directory handle is taken within
911 * the scope of this function, it is not possible for the last
912 * reference to the inode's location directory handle to be
913 * released during the call to lttng_inode_put().
914 *
915 * We wait until the tracker's lock is released to release the
916 * reference. Hence, the call to the tracker is delayed just
917 * enough to not attempt to recursively acquire the tracker's
918 * lock twice.
919 */
920 inode_directory_handle =
921 lttng_inode_get_location_directory_handle(
922 handle->inode);
660a216c 923 }
df038819
JG
924 fd_tracker_untrack(handle->tracker, handle);
925 if (handle->fd >= 0) {
926 /*
927 * The return value of close() is not propagated as there
928 * isn't much the user can do about it.
929 */
930 if (close(handle->fd)) {
931 PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
660a216c 932 handle->fd, path ? path : "Unknown");
df038819
JG
933 }
934 handle->fd = -1;
935 }
4c372c54
JG
936 if (handle->inode) {
937 lttng_inode_put(handle->inode);
938 }
df038819
JG
939 pthread_mutex_unlock(&handle->lock);
940 pthread_mutex_destroy(&handle->lock);
941 pthread_mutex_unlock(&handle->tracker->lock);
df038819 942 free(handle);
dd95933f 943 lttng_directory_handle_put(inode_directory_handle);
df038819
JG
944end:
945 return ret;
946}
This page took 0.107906 seconds and 4 git commands to generate.