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