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