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