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