Fix: C++ syntax of macOS compat code
[lttng-tools.git] / src / common / fd-tracker / fd-tracker.cpp
1 /*
2 * Copyright (C) 2018-2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <urcu.h>
9 #include <urcu/list.h>
10 #include <urcu/rculfhash.h>
11
12 #include <fcntl.h>
13 #include <inttypes.h>
14 #include <pthread.h>
15 #include <stdbool.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18
19 #include <common/defaults.h>
20 #include <common/error.h>
21 #include <common/fs-handle-internal.h>
22 #include <common/hashtable/hashtable.h>
23 #include <common/hashtable/utils.h>
24 #include <common/macros.h>
25 #include <common/optional.h>
26
27 #include "fd-tracker.h"
28 #include "inode.h"
29
30 /* Tracker lock must be taken by the user. */
31 #define TRACKED_COUNT(tracker) \
32 (tracker->count.suspendable.active + \
33 tracker->count.suspendable.suspended + \
34 tracker->count.unsuspendable)
35
36 /* Tracker lock must be taken by the user. */
37 #define ACTIVE_COUNT(tracker) \
38 (tracker->count.suspendable.active + tracker->count.unsuspendable)
39
40 /* Tracker lock must be taken by the user. */
41 #define SUSPENDED_COUNT(tracker) (tracker->count.suspendable.suspended)
42
43 /* Tracker lock must be taken by the user. */
44 #define SUSPENDABLE_COUNT(tracker) \
45 (tracker->count.suspendable.active + \
46 tracker->count.suspendable.suspended)
47
48 /* Tracker lock must be taken by the user. */
49 #define UNSUSPENDABLE_COUNT(tracker) (tracker->count.unsuspendable)
50
51 struct fd_tracker {
52 pthread_mutex_t lock;
53 struct {
54 struct {
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;
77 struct lttng_inode_registry *inode_registry;
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;
81 };
82
83 struct open_properties {
84 int flags;
85 LTTNG_OPTIONAL(mode_t) mode;
86 };
87
88 /*
89 * A fs_handle_tracked is not ref-counted. Therefore, it is assumed that a
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 */
97 struct fs_handle_tracked {
98 struct fs_handle parent;
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;
106 struct lttng_inode *inode;
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
116 struct 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
127 static struct {
128 pthread_mutex_t lock;
129 bool initialized;
130 unsigned long value;
131 } seed = {
132 .lock = PTHREAD_MUTEX_INITIALIZER,
133 };
134
135 static int match_fd(struct cds_lfht_node *node, const void *key);
136 static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry);
137 static struct unsuspendable_fd *unsuspendable_fd_create(
138 const char *name, int fd);
139 static int open_from_properties(const struct lttng_directory_handle *dir_handle,
140 const char *path, struct open_properties *properties);
141
142 static void fs_handle_tracked_log(struct fs_handle_tracked *handle);
143 static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle);
144 static int fs_handle_tracked_restore(struct fs_handle_tracked *handle);
145 static int fs_handle_tracked_get_fd(struct fs_handle *_handle);
146 static void fs_handle_tracked_put_fd(struct fs_handle *_handle);
147 static int fs_handle_tracked_unlink(struct fs_handle *_handle);
148 static int fs_handle_tracked_close(struct fs_handle *_handle);
149
150 static void fd_tracker_track(
151 struct fd_tracker *tracker, struct fs_handle_tracked *handle);
152 static void fd_tracker_untrack(
153 struct fd_tracker *tracker, struct fs_handle_tracked *handle);
154 static int fd_tracker_suspend_handles(
155 struct fd_tracker *tracker, unsigned int count);
156 static int fd_tracker_restore_handle(
157 struct fd_tracker *tracker, struct fs_handle_tracked *handle);
158
159 /* Match function of the tracker's unsuspendable_fds hash table. */
160 static int match_fd(struct cds_lfht_node *node, const void *key)
161 {
162 struct unsuspendable_fd *entry = caa_container_of(
163 node, struct unsuspendable_fd, tracker_node);
164
165 return hash_match_key_ulong(
166 (void *) (unsigned long) entry->fd, (void *) key);
167 }
168
169 static void delete_unsuspendable_fd(struct rcu_head *head)
170 {
171 struct unsuspendable_fd *fd = caa_container_of(
172 head, struct unsuspendable_fd, rcu_head);
173
174 free(fd->name);
175 free(fd);
176 }
177
178 static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry)
179 {
180 if (!entry) {
181 return;
182 }
183 call_rcu(&entry->rcu_head, delete_unsuspendable_fd);
184 }
185
186 static struct unsuspendable_fd *unsuspendable_fd_create(
187 const char *name, int fd)
188 {
189 struct unsuspendable_fd *entry = (unsuspendable_fd *) zmalloc(sizeof(*entry));
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;
203 error:
204 unsuspendable_fd_destroy(entry);
205 return NULL;
206 }
207
208 static void fs_handle_tracked_log(struct fs_handle_tracked *handle)
209 {
210 const char *path;
211
212 pthread_mutex_lock(&handle->lock);
213 lttng_inode_borrow_location(handle->inode, NULL, &path);
214
215 if (handle->fd >= 0) {
216 DBG_NO_LOC(" %s [active, fd %d%s]", path, handle->fd,
217 handle->in_use ? ", in use" : "");
218 } else {
219 DBG_NO_LOC(" %s [suspended]", path);
220 }
221 pthread_mutex_unlock(&handle->lock);
222 }
223
224 /* Tracker lock must be held by the caller. */
225 static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle)
226 {
227 int ret = 0;
228 struct stat fs_stat;
229 const char *path;
230 const struct lttng_directory_handle *node_directory_handle;
231
232 pthread_mutex_lock(&handle->lock);
233 lttng_inode_borrow_location(
234 handle->inode, &node_directory_handle, &path);
235 LTTNG_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
242 ret = lttng_directory_handle_stat(
243 node_directory_handle, path, &fs_stat);
244 if (ret) {
245 PERROR("Filesystem handle to %s cannot be suspended as stat() failed",
246 path);
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",
254 path);
255 ret = -ENOENT;
256 goto end;
257 }
258
259 handle->offset = lseek(handle->fd, 0, SEEK_CUR);
260 if (handle->offset == -1) {
261 WARN("Filesystem handle to %s cannot be suspended as lseek() failed to sample its current position",
262 path);
263 ret = -errno;
264 goto end;
265 }
266
267 ret = close(handle->fd);
268 if (ret) {
269 PERROR("Filesystem handle to %s cannot be suspended as close() failed",
270 path);
271 ret = -errno;
272 goto end;
273 }
274 DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64,
275 path, handle->fd, handle->offset);
276 handle->fd = -1;
277 end:
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. */
286 static int fs_handle_tracked_restore(struct fs_handle_tracked *handle)
287 {
288 int ret, fd = -1;
289 const char *path;
290 const struct lttng_directory_handle *node_directory_handle;
291
292 lttng_inode_borrow_location(
293 handle->inode, &node_directory_handle, &path);
294
295 LTTNG_ASSERT(handle->fd == -1);
296 LTTNG_ASSERT(path);
297 ret = open_from_properties(
298 node_directory_handle, path, &handle->properties);
299 if (ret < 0) {
300 PERROR("Failed to restore filesystem handle to %s, open() failed",
301 path);
302 ret = -errno;
303 goto end;
304 }
305 fd = ret;
306
307 ret = lseek(fd, handle->offset, SEEK_SET);
308 if (ret < 0) {
309 PERROR("Failed to restore filesystem handle to %s, lseek() failed",
310 path);
311 ret = -errno;
312 goto end;
313 }
314 DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64,
315 path, fd, handle->offset);
316 ret = 0;
317 handle->fd = fd;
318 fd = -1;
319 end:
320 if (fd >= 0) {
321 (void) close(fd);
322 }
323 return ret;
324 }
325
326 static int open_from_properties(const struct lttng_directory_handle *dir_handle,
327 const char *path, struct open_properties *properties)
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) {
338 ret = lttng_directory_handle_open_file(dir_handle, path,
339 properties->flags, properties->mode.value);
340 } else {
341 ret = lttng_directory_handle_open_file(dir_handle, path,
342 properties->flags, 0);
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 }
359 end:
360 return ret;
361 }
362
363 struct fd_tracker *fd_tracker_create(const char *unlinked_file_path,
364 unsigned int capacity)
365 {
366 struct fd_tracker *tracker = (fd_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);
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 }
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 }
399 DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
400 capacity);
401 end:
402 return tracker;
403 error:
404 fd_tracker_destroy(tracker);
405 return NULL;
406 }
407
408 void fd_tracker_log(struct fd_tracker *tracker)
409 {
410 struct fs_handle_tracked *handle;
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");
428 cds_list_for_each_entry(
429 handle, &tracker->active_handles, handles_list_node) {
430 fs_handle_tracked_log(handle);
431 }
432 cds_list_for_each_entry(handle, &tracker->suspended_handles,
433 handles_list_node) {
434 fs_handle_tracked_log(handle);
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();
442 cds_lfht_for_each_entry(tracker->unsuspendable_fds, &iter,
443 unsuspendable_fd, tracker_node) {
444 DBG_NO_LOC(" %s [active, fd %d]",
445 unsuspendable_fd->name ?: "Unnamed",
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
456 int fd_tracker_destroy(struct fd_tracker *tracker)
457 {
458 int ret = 0;
459
460 if (!tracker) {
461 goto end;
462 }
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
478 if (tracker->unsuspendable_fds) {
479 ret = cds_lfht_destroy(tracker->unsuspendable_fds, NULL);
480 LTTNG_ASSERT(!ret);
481 }
482
483 lttng_inode_registry_destroy(tracker->inode_registry);
484 lttng_unlinked_file_pool_destroy(tracker->unlinked_file_pool);
485 pthread_mutex_destroy(&tracker->lock);
486 free(tracker);
487 end:
488 return ret;
489 }
490
491 struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
492 struct lttng_directory_handle *directory,
493 const char *path,
494 int flags,
495 mode_t *mode)
496 {
497 int ret;
498 struct fs_handle_tracked *handle = NULL;
499 struct stat fd_stat;
500 struct open_properties properties = {
501 .flags = flags,
502 .mode = {
503 .is_set = !!mode,
504 .value = static_cast<mode_t>(mode ? *mode : 0),
505 }
506 };
507
508 pthread_mutex_lock(&tracker->lock);
509 if (ACTIVE_COUNT(tracker) == tracker->capacity) {
510 if (tracker->count.suspendable.active > 0) {
511 ret = fd_tracker_suspend_handles(tracker, 1);
512 if (ret) {
513 goto end;
514 }
515 } else {
516 /*
517 * There are not enough active suspendable file
518 * descriptors to open a new fd and still accommodate
519 * the tracker's capacity.
520 */
521 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
522 tracker->count.unsuspendable);
523 goto end;
524 }
525 }
526
527 handle = (fs_handle_tracked *) zmalloc(sizeof(*handle));
528 if (!handle) {
529 goto end;
530 }
531 handle->parent = (typeof(handle->parent)) {
532 .get_fd = fs_handle_tracked_get_fd,
533 .put_fd = fs_handle_tracked_put_fd,
534 .unlink = fs_handle_tracked_unlink,
535 .close = fs_handle_tracked_close,
536 };
537
538 handle->tracker = tracker;
539
540 ret = pthread_mutex_init(&handle->lock, NULL);
541 if (ret) {
542 PERROR("Failed to initialize handle mutex while creating fs handle");
543 goto error_mutex_init;
544 }
545
546 handle->fd = open_from_properties(directory, path, &properties);
547 if (handle->fd < 0) {
548 PERROR("Failed to open fs handle to %s, open() returned", path);
549 goto error;
550 }
551
552 handle->properties = properties;
553
554 handle->inode = lttng_inode_registry_get_inode(tracker->inode_registry,
555 directory, path, handle->fd,
556 tracker->unlinked_file_pool);
557 if (!handle->inode) {
558 ERR("Failed to get lttng_inode corresponding to file %s", path);
559 goto error;
560 }
561
562 if (fstat(handle->fd, &fd_stat)) {
563 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
564 goto error;
565 }
566 handle->ino = fd_stat.st_ino;
567
568 fd_tracker_track(tracker, handle);
569 end:
570 pthread_mutex_unlock(&tracker->lock);
571 return handle ? &handle->parent : NULL;
572 error:
573 if (handle->inode) {
574 lttng_inode_put(handle->inode);
575 }
576 pthread_mutex_destroy(&handle->lock);
577 error_mutex_init:
578 free(handle);
579 handle = NULL;
580 goto end;
581 }
582
583 /* Caller must hold the tracker's lock. */
584 static int fd_tracker_suspend_handles(
585 struct fd_tracker *tracker, unsigned int count)
586 {
587 unsigned int left_to_close = count;
588 unsigned int attempts_left = tracker->count.suspendable.active;
589 struct fs_handle_tracked *handle, *tmp;
590
591 cds_list_for_each_entry_safe(handle, tmp, &tracker->active_handles,
592 handles_list_node) {
593 int ret;
594
595 fd_tracker_untrack(tracker, handle);
596 ret = fs_handle_tracked_suspend(handle);
597 fd_tracker_track(tracker, handle);
598 if (!ret) {
599 left_to_close--;
600 }
601 attempts_left--;
602
603 if (left_to_close == 0 || attempts_left == 0) {
604 break;
605 }
606 }
607 return left_to_close ? -EMFILE : 0;
608 }
609
610 int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
611 int *out_fds,
612 const char **names,
613 unsigned int fd_count,
614 fd_open_cb open,
615 void *user_data)
616 {
617 int ret, user_ret, i, fds_to_suspend;
618 unsigned int active_fds;
619 struct unsuspendable_fd **entries;
620
621 entries = (unsuspendable_fd **) zmalloc(fd_count * sizeof(*entries));
622 if (!entries) {
623 ret = -1;
624 goto end;
625 }
626
627 pthread_mutex_lock(&tracker->lock);
628
629 active_fds = ACTIVE_COUNT(tracker);
630 fds_to_suspend = (int) active_fds + (int) fd_count -
631 (int) tracker->capacity;
632 if (fds_to_suspend > 0) {
633 if (fds_to_suspend <= tracker->count.suspendable.active) {
634 ret = fd_tracker_suspend_handles(
635 tracker, fds_to_suspend);
636 if (ret) {
637 goto end_unlock;
638 }
639 } else {
640 /*
641 * There are not enough active suspendable file
642 * descriptors to open a new fd and still accommodates the
643 * tracker's capacity.
644 */
645 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
646 tracker->count.unsuspendable);
647 ret = -EMFILE;
648 goto end_unlock;
649 }
650 }
651
652 user_ret = open(user_data, out_fds);
653 if (user_ret) {
654 ret = user_ret;
655 goto end_unlock;
656 }
657
658 /*
659 * Add the fds returned by the user's callback to the hashtable
660 * of unsuspendable fds.
661 */
662 for (i = 0; i < fd_count; i++) {
663 struct unsuspendable_fd *entry = unsuspendable_fd_create(
664 names ? names[i] : NULL, out_fds[i]);
665
666 if (!entry) {
667 ret = -1;
668 goto end_free_entries;
669 }
670 entries[i] = entry;
671 }
672
673 rcu_read_lock();
674 for (i = 0; i < fd_count; i++) {
675 struct cds_lfht_node *node;
676 struct unsuspendable_fd *entry = entries[i];
677
678 node = cds_lfht_add_unique(tracker->unsuspendable_fds,
679 hash_key_ulong((void *) (unsigned long)
680 out_fds[i],
681 seed.value),
682 match_fd, (void *) (unsigned long) out_fds[i],
683 &entry->tracker_node);
684
685 if (node != &entry->tracker_node) {
686 ret = -EEXIST;
687 rcu_read_unlock();
688 goto end_free_entries;
689 }
690 entries[i] = NULL;
691 }
692 tracker->count.unsuspendable += fd_count;
693 rcu_read_unlock();
694 ret = user_ret;
695 end_unlock:
696 pthread_mutex_unlock(&tracker->lock);
697 end:
698 free(entries);
699 return ret;
700 end_free_entries:
701 for (i = 0; i < fd_count; i++) {
702 unsuspendable_fd_destroy(entries[i]);
703 }
704 goto end_unlock;
705 }
706
707 int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
708 int *fds_in,
709 unsigned int fd_count,
710 fd_close_cb close,
711 void *user_data)
712 {
713 int i, ret, user_ret;
714 int *fds = NULL;
715
716 /*
717 * Maintain a local copy of fds_in as the user's callback may modify its
718 * contents (e.g. setting the fd(s) to -1 after close).
719 */
720 fds = (int *) malloc(sizeof(*fds) * fd_count);
721 if (!fds) {
722 ret = -1;
723 goto end;
724 }
725 memcpy(fds, fds_in, sizeof(*fds) * fd_count);
726
727 pthread_mutex_lock(&tracker->lock);
728 rcu_read_lock();
729
730 /* Let the user close the file descriptors. */
731 user_ret = close(user_data, fds_in);
732 if (user_ret) {
733 ret = user_ret;
734 goto end_unlock;
735 }
736
737 /* Untrack the fds that were just closed by the user's callback. */
738 for (i = 0; i < fd_count; i++) {
739 struct cds_lfht_node *node;
740 struct cds_lfht_iter iter;
741 struct unsuspendable_fd *entry;
742
743 cds_lfht_lookup(tracker->unsuspendable_fds,
744 hash_key_ulong((void *) (unsigned long) fds[i],
745 seed.value),
746 match_fd, (void *) (unsigned long) fds[i],
747 &iter);
748 node = cds_lfht_iter_get_node(&iter);
749 if (!node) {
750 /* Unknown file descriptor. */
751 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
752 fds[i]);
753 ret = -EINVAL;
754 goto end_unlock;
755 }
756 entry = caa_container_of(
757 node, struct unsuspendable_fd, tracker_node);
758
759 cds_lfht_del(tracker->unsuspendable_fds, node);
760 unsuspendable_fd_destroy(entry);
761 fds[i] = -1;
762 }
763
764 tracker->count.unsuspendable -= fd_count;
765 ret = 0;
766 end_unlock:
767 rcu_read_unlock();
768 pthread_mutex_unlock(&tracker->lock);
769 free(fds);
770 end:
771 return ret;
772 }
773
774 /* Caller must have taken the tracker's and handle's locks. */
775 static void fd_tracker_track(
776 struct fd_tracker *tracker, struct fs_handle_tracked *handle)
777 {
778 if (handle->fd >= 0) {
779 tracker->count.suspendable.active++;
780 cds_list_add_tail(&handle->handles_list_node,
781 &tracker->active_handles);
782 } else {
783 tracker->count.suspendable.suspended++;
784 cds_list_add_tail(&handle->handles_list_node,
785 &tracker->suspended_handles);
786 }
787 }
788
789 /* Caller must have taken the tracker's and handle's locks. */
790 static void fd_tracker_untrack(
791 struct fd_tracker *tracker, struct fs_handle_tracked *handle)
792 {
793 if (handle->fd >= 0) {
794 tracker->count.suspendable.active--;
795 } else {
796 tracker->count.suspendable.suspended--;
797 }
798 cds_list_del(&handle->handles_list_node);
799 }
800
801 /* Caller must have taken the tracker's and handle's locks. */
802 static int fd_tracker_restore_handle(
803 struct fd_tracker *tracker, struct fs_handle_tracked *handle)
804 {
805 int ret;
806
807 fd_tracker_untrack(tracker, handle);
808 if (ACTIVE_COUNT(tracker) >= tracker->capacity) {
809 ret = fd_tracker_suspend_handles(tracker, 1);
810 if (ret) {
811 goto end;
812 }
813 }
814 ret = fs_handle_tracked_restore(handle);
815 end:
816 fd_tracker_track(tracker, handle);
817 return ret ? ret : handle->fd;
818 }
819
820 static int fs_handle_tracked_get_fd(struct fs_handle *_handle)
821 {
822 int ret;
823 struct fs_handle_tracked *handle =
824 container_of(_handle, struct fs_handle_tracked, parent);
825
826 /*
827 * TODO This should be optimized as it is a fairly hot path.
828 * The fd-tracker's lock should only be taken when a fs_handle is
829 * restored (slow path). On the fast path (fs_handle is active),
830 * the only effect on the fd_tracker is marking the handle as the
831 * most recently used. Currently, it is done by a call to the
832 * track/untrack helpers, but it should be done atomically.
833 *
834 * Note that the lock's nesting order must still be respected here.
835 * The handle's lock nests inside the tracker's lock.
836 */
837 pthread_mutex_lock(&handle->tracker->lock);
838 pthread_mutex_lock(&handle->lock);
839 LTTNG_ASSERT(!handle->in_use);
840
841 handle->tracker->stats.uses++;
842 if (handle->fd >= 0) {
843 ret = handle->fd;
844 /* Mark as most recently used. */
845 fd_tracker_untrack(handle->tracker, handle);
846 fd_tracker_track(handle->tracker, handle);
847 } else {
848 handle->tracker->stats.misses++;
849 ret = fd_tracker_restore_handle(handle->tracker, handle);
850 if (ret < 0) {
851 handle->tracker->stats.errors++;
852 goto end;
853 }
854 }
855 handle->in_use = true;
856 end:
857 pthread_mutex_unlock(&handle->lock);
858 pthread_mutex_unlock(&handle->tracker->lock);
859 return ret;
860 }
861
862 static void fs_handle_tracked_put_fd(struct fs_handle *_handle)
863 {
864 struct fs_handle_tracked *handle =
865 container_of(_handle, struct fs_handle_tracked, parent);
866
867 pthread_mutex_lock(&handle->lock);
868 handle->in_use = false;
869 pthread_mutex_unlock(&handle->lock);
870 }
871
872 static int fs_handle_tracked_unlink(struct fs_handle *_handle)
873 {
874 int ret;
875 struct fs_handle_tracked *handle =
876 container_of(_handle, struct fs_handle_tracked, parent);
877
878 pthread_mutex_lock(&handle->tracker->lock);
879 pthread_mutex_lock(&handle->lock);
880 ret = lttng_inode_unlink(handle->inode);
881 pthread_mutex_unlock(&handle->lock);
882 pthread_mutex_unlock(&handle->tracker->lock);
883 return ret;
884 }
885
886 static int fs_handle_tracked_close(struct fs_handle *_handle)
887 {
888 int ret = 0;
889 const char *path = NULL;
890 struct fs_handle_tracked *handle =
891 container_of(_handle, struct fs_handle_tracked, parent);
892 struct lttng_directory_handle *inode_directory_handle = NULL;
893
894 if (!handle) {
895 ret = -EINVAL;
896 goto end;
897 }
898
899 pthread_mutex_lock(&handle->tracker->lock);
900 pthread_mutex_lock(&handle->lock);
901 if (handle->inode) {
902 lttng_inode_borrow_location(handle->inode, NULL, &path);
903 /*
904 * Here a reference to the inode's directory handle is acquired
905 * to prevent the last reference to it from being released while
906 * the tracker's lock is taken.
907 *
908 * If this wasn't done, the directory handle could attempt to
909 * close its underlying directory file descriptor, which would
910 * attempt to lock the tracker's lock, resulting in a deadlock.
911 *
912 * Since a new reference to the directory handle is taken within
913 * the scope of this function, it is not possible for the last
914 * reference to the inode's location directory handle to be
915 * released during the call to lttng_inode_put().
916 *
917 * We wait until the tracker's lock is released to release the
918 * reference. Hence, the call to the tracker is delayed just
919 * enough to not attempt to recursively acquire the tracker's
920 * lock twice.
921 */
922 inode_directory_handle =
923 lttng_inode_get_location_directory_handle(
924 handle->inode);
925 }
926 fd_tracker_untrack(handle->tracker, handle);
927 if (handle->fd >= 0) {
928 /*
929 * The return value of close() is not propagated as there
930 * isn't much the user can do about it.
931 */
932 if (close(handle->fd)) {
933 PERROR("Failed to close the file descriptor (%d) of fs handle to %s, close() returned",
934 handle->fd, path ? path : "Unknown");
935 }
936 handle->fd = -1;
937 }
938 if (handle->inode) {
939 lttng_inode_put(handle->inode);
940 }
941 pthread_mutex_unlock(&handle->lock);
942 pthread_mutex_destroy(&handle->lock);
943 pthread_mutex_unlock(&handle->tracker->lock);
944 free(handle);
945 lttng_directory_handle_put(inode_directory_handle);
946 end:
947 return ret;
948 }
This page took 0.049797 seconds and 4 git commands to generate.