fd-tracker: restore suspended handles from their inode's path
[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 /* Unlinked files are moved in this directory under a unique name. */
89 struct lttng_directory_handle *unlink_directory_handle;
90 struct lttng_unlinked_file_pool *unlinked_file_pool;
91 };
92
93 struct open_properties {
94 int flags;
95 LTTNG_OPTIONAL(mode_t) mode;
96 };
97
98 /*
99 * A fs_handle_tracked is not ref-counted. Therefore, it is assumed that a
100 * handle is never in-use while it is being reclaimed. It can be
101 * shared by multiple threads, but external synchronization is required
102 * to ensure it is not still being used when it is reclaimed (close method).
103 * In this respect, it is not different from a regular file descriptor.
104 *
105 * The fs_handle lock always nests _within_ the tracker's lock.
106 */
107 struct fs_handle_tracked {
108 struct fs_handle parent;
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(
148 const char *name, int fd);
149 static int open_from_properties(const struct lttng_directory_handle *dir_handle,
150 const char *path, struct open_properties *properties);
151
152 static void fs_handle_tracked_log(struct fs_handle_tracked *handle);
153 static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle);
154 static int fs_handle_tracked_restore(struct fs_handle_tracked *handle);
155 static int fs_handle_tracked_get_fd(struct fs_handle *_handle);
156 static void fs_handle_tracked_put_fd(struct fs_handle *_handle);
157 static int fs_handle_tracked_unlink(struct fs_handle *_handle);
158 static int fs_handle_tracked_close(struct fs_handle *_handle);
159
160 static void fd_tracker_track(
161 struct fd_tracker *tracker, struct fs_handle_tracked *handle);
162 static void fd_tracker_untrack(
163 struct fd_tracker *tracker, struct fs_handle_tracked *handle);
164 static int fd_tracker_suspend_handles(
165 struct fd_tracker *tracker, unsigned int count);
166 static int fd_tracker_restore_handle(
167 struct fd_tracker *tracker, struct fs_handle_tracked *handle);
168
169 /* Match function of the tracker's unsuspendable_fds hash table. */
170 static int match_fd(struct cds_lfht_node *node, const void *key)
171 {
172 struct unsuspendable_fd *entry = caa_container_of(
173 node, struct unsuspendable_fd, tracker_node);
174
175 return hash_match_key_ulong(
176 (void *) (unsigned long) entry->fd, (void *) key);
177 }
178
179 static void delete_unsuspendable_fd(struct rcu_head *head)
180 {
181 struct unsuspendable_fd *fd = caa_container_of(
182 head, struct unsuspendable_fd, rcu_head);
183
184 free(fd->name);
185 free(fd);
186 }
187
188 static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry)
189 {
190 if (!entry) {
191 return;
192 }
193 call_rcu(&entry->rcu_head, delete_unsuspendable_fd);
194 }
195
196 static struct unsuspendable_fd *unsuspendable_fd_create(
197 const char *name, int fd)
198 {
199 struct unsuspendable_fd *entry = 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 void fs_handle_tracked_log(struct fs_handle_tracked *handle)
219 {
220 const char *path;
221
222 pthread_mutex_lock(&handle->lock);
223 lttng_inode_get_location(handle->inode, NULL, &path);
224
225 if (handle->fd >= 0) {
226 DBG_NO_LOC(" %s [active, fd %d%s]", path, handle->fd,
227 handle->in_use ? ", in use" : "");
228 } else {
229 DBG_NO_LOC(" %s [suspended]", path);
230 }
231 pthread_mutex_unlock(&handle->lock);
232 }
233
234 /* Tracker lock must be held by the caller. */
235 static int fs_handle_tracked_suspend(struct fs_handle_tracked *handle)
236 {
237 int ret = 0;
238 struct stat fs_stat;
239 const char *path;
240 const struct lttng_directory_handle *node_directory_handle;
241
242 pthread_mutex_lock(&handle->lock);
243 lttng_inode_get_location(handle->inode, &node_directory_handle, &path);
244 assert(handle->fd >= 0);
245 if (handle->in_use) {
246 /* This handle can't be suspended as it is currently in use. */
247 ret = -EAGAIN;
248 goto end;
249 }
250
251 ret = lttng_directory_handle_stat(
252 node_directory_handle, path, &fs_stat);
253 if (ret) {
254 PERROR("Filesystem handle to %s cannot be suspended as stat() failed",
255 path);
256 ret = -errno;
257 goto end;
258 }
259
260 if (fs_stat.st_ino != handle->ino) {
261 /* Don't suspend as the handle would not be restorable. */
262 WARN("Filesystem handle to %s cannot be suspended as its inode changed",
263 path);
264 ret = -ENOENT;
265 goto end;
266 }
267
268 handle->offset = lseek(handle->fd, 0, SEEK_CUR);
269 if (handle->offset == -1) {
270 WARN("Filesystem handle to %s cannot be suspended as lseek() failed to sample its current position",
271 path);
272 ret = -errno;
273 goto end;
274 }
275
276 ret = close(handle->fd);
277 if (ret) {
278 PERROR("Filesystem handle to %s cannot be suspended as close() failed",
279 path);
280 ret = -errno;
281 goto end;
282 }
283 DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64,
284 path, handle->fd, handle->offset);
285 handle->fd = -1;
286 end:
287 if (ret) {
288 handle->tracker->stats.errors++;
289 }
290 pthread_mutex_unlock(&handle->lock);
291 return ret;
292 }
293
294 /* Caller must hold the tracker and handle's locks. */
295 static int fs_handle_tracked_restore(struct fs_handle_tracked *handle)
296 {
297 int ret, fd = -1;
298 const char *path;
299 const struct lttng_directory_handle *node_directory_handle;
300
301 lttng_inode_get_location(handle->inode, &node_directory_handle, &path);
302
303 assert(handle->fd == -1);
304 assert(path);
305 ret = open_from_properties(
306 node_directory_handle, 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(const struct lttng_directory_handle *dir_handle,
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 = lttng_directory_handle_open_file(dir_handle, path,
347 properties->flags, properties->mode.value);
348 } else {
349 ret = lttng_directory_handle_open_file(dir_handle, path,
350 properties->flags, 0);
351 }
352 /*
353 * Some flags should not be used beyond the initial open() of a
354 * restorable file system handle. O_CREAT and O_TRUNC must
355 * be cleared since it would be unexpected to re-use them
356 * when the handle is retored:
357 * - O_CREAT should not be needed as the file has been created
358 * on the initial call to open(),
359 * - O_TRUNC would destroy the file's contents by truncating it
360 * to length 0.
361 */
362 properties->flags &= ~(O_CREAT | O_TRUNC);
363 if (ret < 0) {
364 ret = -errno;
365 goto end;
366 }
367 end:
368 return ret;
369 }
370
371 struct fd_tracker *fd_tracker_create(const char *unlinked_file_path,
372 unsigned int capacity)
373 {
374 struct fd_tracker *tracker = zmalloc(sizeof(struct fd_tracker));
375
376 if (!tracker) {
377 goto end;
378 }
379
380 pthread_mutex_lock(&seed.lock);
381 if (!seed.initialized) {
382 seed.value = (unsigned long) time(NULL);
383 seed.initialized = true;
384 }
385 pthread_mutex_unlock(&seed.lock);
386
387 CDS_INIT_LIST_HEAD(&tracker->active_handles);
388 CDS_INIT_LIST_HEAD(&tracker->suspended_handles);
389 tracker->capacity = capacity;
390 tracker->unsuspendable_fds = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
391 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
392 if (!tracker->unsuspendable_fds) {
393 ERR("Failed to create fd-tracker's unsuspendable_fds hash table");
394 goto error;
395 }
396 tracker->inode_registry = lttng_inode_registry_create();
397 if (!tracker->inode_registry) {
398 ERR("Failed to create fd-tracker's inode registry");
399 goto error;
400 }
401
402 tracker->unlinked_file_pool =
403 lttng_unlinked_file_pool_create(unlinked_file_path);
404 if (!tracker->unlinked_file_pool) {
405 goto error;
406 }
407 DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
408 capacity);
409 end:
410 return tracker;
411 error:
412 fd_tracker_destroy(tracker);
413 return NULL;
414 }
415
416 void fd_tracker_log(struct fd_tracker *tracker)
417 {
418 struct fs_handle_tracked *handle;
419 struct unsuspendable_fd *unsuspendable_fd;
420 struct cds_lfht_iter iter;
421
422 pthread_mutex_lock(&tracker->lock);
423 DBG_NO_LOC("File descriptor tracker");
424 DBG_NO_LOC(" Stats:");
425 DBG_NO_LOC(" uses: %" PRIu64, tracker->stats.uses);
426 DBG_NO_LOC(" misses: %" PRIu64, tracker->stats.misses);
427 DBG_NO_LOC(" errors: %" PRIu64, tracker->stats.errors);
428 DBG_NO_LOC(" Tracked: %u", TRACKED_COUNT(tracker));
429 DBG_NO_LOC(" active: %u", ACTIVE_COUNT(tracker));
430 DBG_NO_LOC(" suspendable: %u", SUSPENDABLE_COUNT(tracker));
431 DBG_NO_LOC(" unsuspendable: %u", UNSUSPENDABLE_COUNT(tracker));
432 DBG_NO_LOC(" suspended: %u", SUSPENDED_COUNT(tracker));
433 DBG_NO_LOC(" capacity: %u", tracker->capacity);
434
435 DBG_NO_LOC(" Tracked suspendable file descriptors");
436 cds_list_for_each_entry(
437 handle, &tracker->active_handles, handles_list_node) {
438 fs_handle_tracked_log(handle);
439 }
440 cds_list_for_each_entry(handle, &tracker->suspended_handles,
441 handles_list_node) {
442 fs_handle_tracked_log(handle);
443 }
444 if (!SUSPENDABLE_COUNT(tracker)) {
445 DBG_NO_LOC(" None");
446 }
447
448 DBG_NO_LOC(" Tracked unsuspendable file descriptors");
449 rcu_read_lock();
450 cds_lfht_for_each_entry(tracker->unsuspendable_fds, &iter,
451 unsuspendable_fd, tracker_node) {
452 DBG_NO_LOC(" %s [active, fd %d]",
453 unsuspendable_fd->name ?: "Unnamed",
454 unsuspendable_fd->fd);
455 }
456 rcu_read_unlock();
457 if (!UNSUSPENDABLE_COUNT(tracker)) {
458 DBG_NO_LOC(" None");
459 }
460
461 pthread_mutex_unlock(&tracker->lock);
462 }
463
464 int fd_tracker_destroy(struct fd_tracker *tracker)
465 {
466 int ret = 0;
467
468 /*
469 * Refuse to destroy the tracker as fs_handles may still old
470 * weak references to the tracker.
471 */
472 pthread_mutex_lock(&tracker->lock);
473 if (TRACKED_COUNT(tracker)) {
474 ERR("A file descriptor leak has been detected: %u tracked file descriptors are still being tracked",
475 TRACKED_COUNT(tracker));
476 pthread_mutex_unlock(&tracker->lock);
477 fd_tracker_log(tracker);
478 ret = -1;
479 goto end;
480 }
481 pthread_mutex_unlock(&tracker->lock);
482
483 if (tracker->unsuspendable_fds) {
484 ret = cds_lfht_destroy(tracker->unsuspendable_fds, NULL);
485 assert(!ret);
486 }
487
488 lttng_inode_registry_destroy(tracker->inode_registry);
489 lttng_unlinked_file_pool_destroy(tracker->unlinked_file_pool);
490 pthread_mutex_destroy(&tracker->lock);
491 free(tracker);
492 end:
493 return ret;
494 }
495
496 struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
497 struct lttng_directory_handle *directory,
498 const char *path,
499 int flags,
500 mode_t *mode)
501 {
502 int ret;
503 struct fs_handle_tracked *handle = NULL;
504 struct stat fd_stat;
505 struct open_properties properties = {
506 .flags = flags,
507 .mode.is_set = !!mode,
508 .mode.value = mode ? *mode : 0,
509 };
510
511 pthread_mutex_lock(&tracker->lock);
512 if (ACTIVE_COUNT(tracker) == tracker->capacity) {
513 if (tracker->count.suspendable.active > 0) {
514 ret = fd_tracker_suspend_handles(tracker, 1);
515 if (ret) {
516 goto end;
517 }
518 } else {
519 /*
520 * There are not enough active suspendable file
521 * descriptors to open a new fd and still accommodate
522 * the tracker's capacity.
523 */
524 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
525 tracker->count.unsuspendable);
526 goto end;
527 }
528 }
529
530 handle = zmalloc(sizeof(*handle));
531 if (!handle) {
532 goto end;
533 }
534 handle->parent = (typeof(handle->parent)) {
535 .get_fd = fs_handle_tracked_get_fd,
536 .put_fd = fs_handle_tracked_put_fd,
537 .unlink = fs_handle_tracked_unlink,
538 .close = fs_handle_tracked_close,
539 };
540
541 handle->tracker = tracker;
542
543 ret = pthread_mutex_init(&handle->lock, NULL);
544 if (ret) {
545 PERROR("Failed to initialize handle mutex while creating fs handle");
546 goto error_mutex_init;
547 }
548
549 handle->fd = open_from_properties(directory, path, &properties);
550 if (handle->fd < 0) {
551 PERROR("Failed to open fs handle to %s, open() returned", path);
552 goto error;
553 }
554
555 handle->properties = properties;
556
557 handle->inode = lttng_inode_registry_get_inode(tracker->inode_registry,
558 directory, path, handle->fd,
559 tracker->unlinked_file_pool);
560 if (!handle->inode) {
561 ERR("Failed to get lttng_inode corresponding to file %s", path);
562 goto error;
563 }
564
565 if (fstat(handle->fd, &fd_stat)) {
566 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
567 goto error;
568 }
569 handle->ino = fd_stat.st_ino;
570
571 fd_tracker_track(tracker, handle);
572 end:
573 pthread_mutex_unlock(&tracker->lock);
574 return handle ? &handle->parent : NULL;
575 error:
576 if (handle->inode) {
577 lttng_inode_put(handle->inode);
578 }
579 pthread_mutex_destroy(&handle->lock);
580 error_mutex_init:
581 free(handle);
582 handle = NULL;
583 goto end;
584 }
585
586 /* Caller must hold the tracker's lock. */
587 static int fd_tracker_suspend_handles(
588 struct fd_tracker *tracker, unsigned int count)
589 {
590 unsigned int left_to_close = count;
591 unsigned int attempts_left = tracker->count.suspendable.active;
592 struct fs_handle_tracked *handle, *tmp;
593
594 cds_list_for_each_entry_safe(handle, tmp, &tracker->active_handles,
595 handles_list_node) {
596 int ret;
597
598 fd_tracker_untrack(tracker, handle);
599 ret = fs_handle_tracked_suspend(handle);
600 fd_tracker_track(tracker, handle);
601 if (!ret) {
602 left_to_close--;
603 }
604 attempts_left--;
605
606 if (left_to_close == 0 || attempts_left == 0) {
607 break;
608 }
609 }
610 return left_to_close ? -EMFILE : 0;
611 }
612
613 int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
614 int *out_fds,
615 const char **names,
616 unsigned int fd_count,
617 fd_open_cb open,
618 void *user_data)
619 {
620 int ret, user_ret, i, fds_to_suspend;
621 unsigned int active_fds;
622 struct unsuspendable_fd **entries;
623
624 entries = zmalloc(fd_count * sizeof(*entries));
625 if (!entries) {
626 ret = -1;
627 goto end;
628 }
629
630 pthread_mutex_lock(&tracker->lock);
631
632 active_fds = ACTIVE_COUNT(tracker);
633 fds_to_suspend = (int) active_fds + (int) fd_count -
634 (int) tracker->capacity;
635 if (fds_to_suspend > 0) {
636 if (fds_to_suspend <= tracker->count.suspendable.active) {
637 ret = fd_tracker_suspend_handles(
638 tracker, fds_to_suspend);
639 if (ret) {
640 goto end_unlock;
641 }
642 } else {
643 /*
644 * There are not enough active suspendable file
645 * descriptors to open a new fd and still accomodate the
646 * tracker's capacity.
647 */
648 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
649 tracker->count.unsuspendable);
650 ret = -EMFILE;
651 goto end_unlock;
652 }
653 }
654
655 user_ret = open(user_data, out_fds);
656 if (user_ret) {
657 ret = user_ret;
658 goto end_unlock;
659 }
660
661 /*
662 * Add the fds returned by the user's callback to the hashtable
663 * of unsuspendable fds.
664 */
665 for (i = 0; i < fd_count; i++) {
666 struct unsuspendable_fd *entry = unsuspendable_fd_create(
667 names ? names[i] : NULL, out_fds[i]);
668
669 if (!entry) {
670 ret = -1;
671 goto end_free_entries;
672 }
673 entries[i] = entry;
674 }
675
676 rcu_read_lock();
677 for (i = 0; i < fd_count; i++) {
678 struct cds_lfht_node *node;
679 struct unsuspendable_fd *entry = entries[i];
680
681 node = cds_lfht_add_unique(tracker->unsuspendable_fds,
682 hash_key_ulong((void *) (unsigned long)
683 out_fds[i],
684 seed.value),
685 match_fd, (void *) (unsigned long) out_fds[i],
686 &entry->tracker_node);
687
688 if (node != &entry->tracker_node) {
689 ret = -EEXIST;
690 rcu_read_unlock();
691 goto end_free_entries;
692 }
693 entries[i] = NULL;
694 }
695 tracker->count.unsuspendable += fd_count;
696 rcu_read_unlock();
697 ret = user_ret;
698 end_unlock:
699 pthread_mutex_unlock(&tracker->lock);
700 end:
701 free(entries);
702 return ret;
703 end_free_entries:
704 for (i = 0; i < fd_count; i++) {
705 unsuspendable_fd_destroy(entries[i]);
706 }
707 goto end_unlock;
708 }
709
710 int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
711 int *fds_in,
712 unsigned int fd_count,
713 fd_close_cb close,
714 void *user_data)
715 {
716 int i, ret, user_ret;
717 int *fds = NULL;
718
719 /*
720 * Maintain a local copy of fds_in as the user's callback may modify its
721 * contents (e.g. setting the fd(s) to -1 after close).
722 */
723 fds = malloc(sizeof(*fds) * fd_count);
724 if (!fds) {
725 ret = -1;
726 goto end;
727 }
728 memcpy(fds, fds_in, sizeof(*fds) * fd_count);
729
730 pthread_mutex_lock(&tracker->lock);
731 rcu_read_lock();
732
733 /* Let the user close the file descriptors. */
734 user_ret = close(user_data, fds_in);
735 if (user_ret) {
736 ret = user_ret;
737 goto end_unlock;
738 }
739
740 /* Untrack the fds that were just closed by the user's callback. */
741 for (i = 0; i < fd_count; i++) {
742 struct cds_lfht_node *node;
743 struct cds_lfht_iter iter;
744 struct unsuspendable_fd *entry;
745
746 cds_lfht_lookup(tracker->unsuspendable_fds,
747 hash_key_ulong((void *) (unsigned long) fds[i],
748 seed.value),
749 match_fd, (void *) (unsigned long) fds[i],
750 &iter);
751 node = cds_lfht_iter_get_node(&iter);
752 if (!node) {
753 /* Unknown file descriptor. */
754 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
755 fds[i]);
756 ret = -EINVAL;
757 goto end_unlock;
758 }
759 entry = caa_container_of(
760 node, struct unsuspendable_fd, tracker_node);
761
762 cds_lfht_del(tracker->unsuspendable_fds, node);
763 unsuspendable_fd_destroy(entry);
764 fds[i] = -1;
765 }
766
767 tracker->count.unsuspendable -= fd_count;
768 ret = 0;
769 end_unlock:
770 rcu_read_unlock();
771 pthread_mutex_unlock(&tracker->lock);
772 free(fds);
773 end:
774 return ret;
775 }
776
777 /* Caller must have taken the tracker's and handle's locks. */
778 static void fd_tracker_track(
779 struct fd_tracker *tracker, struct fs_handle_tracked *handle)
780 {
781 if (handle->fd >= 0) {
782 tracker->count.suspendable.active++;
783 cds_list_add_tail(&handle->handles_list_node,
784 &tracker->active_handles);
785 } else {
786 tracker->count.suspendable.suspended++;
787 cds_list_add_tail(&handle->handles_list_node,
788 &tracker->suspended_handles);
789 }
790 }
791
792 /* Caller must have taken the tracker's and handle's locks. */
793 static void fd_tracker_untrack(
794 struct fd_tracker *tracker, struct fs_handle_tracked *handle)
795 {
796 if (handle->fd >= 0) {
797 tracker->count.suspendable.active--;
798 } else {
799 tracker->count.suspendable.suspended--;
800 }
801 cds_list_del(&handle->handles_list_node);
802 }
803
804 /* Caller must have taken the tracker's and handle's locks. */
805 static int fd_tracker_restore_handle(
806 struct fd_tracker *tracker, struct fs_handle_tracked *handle)
807 {
808 int ret;
809
810 fd_tracker_untrack(tracker, handle);
811 if (ACTIVE_COUNT(tracker) >= tracker->capacity) {
812 ret = fd_tracker_suspend_handles(tracker, 1);
813 if (ret) {
814 goto end;
815 }
816 }
817 ret = fs_handle_tracked_restore(handle);
818 end:
819 fd_tracker_track(tracker, handle);
820 return ret ? ret : handle->fd;
821 }
822
823 static int fs_handle_tracked_get_fd(struct fs_handle *_handle)
824 {
825 int ret;
826 struct fs_handle_tracked *handle =
827 container_of(_handle, struct fs_handle_tracked, parent);
828
829 /*
830 * TODO This should be optimized as it is a fairly hot path.
831 * The fd-tracker's lock should only be taken when a fs_handle is
832 * restored (slow path). On the fast path (fs_handle is active),
833 * the only effect on the fd_tracker is marking the handle as the
834 * most recently used. Currently, it is done by a call to the
835 * track/untrack helpers, but it should be done atomically.
836 *
837 * Note that the lock's nesting order must still be respected here.
838 * The handle's lock nests inside the tracker's lock.
839 */
840 pthread_mutex_lock(&handle->tracker->lock);
841 pthread_mutex_lock(&handle->lock);
842 assert(!handle->in_use);
843
844 handle->tracker->stats.uses++;
845 if (handle->fd >= 0) {
846 ret = handle->fd;
847 /* Mark as most recently used. */
848 fd_tracker_untrack(handle->tracker, handle);
849 fd_tracker_track(handle->tracker, handle);
850 } else {
851 handle->tracker->stats.misses++;
852 ret = fd_tracker_restore_handle(handle->tracker, handle);
853 if (ret < 0) {
854 handle->tracker->stats.errors++;
855 goto end;
856 }
857 }
858 handle->in_use = true;
859 end:
860 pthread_mutex_unlock(&handle->lock);
861 pthread_mutex_unlock(&handle->tracker->lock);
862 return ret;
863 }
864
865 static void fs_handle_tracked_put_fd(struct fs_handle *_handle)
866 {
867 struct fs_handle_tracked *handle =
868 container_of(_handle, struct fs_handle_tracked, parent);
869
870 pthread_mutex_lock(&handle->lock);
871 handle->in_use = false;
872 pthread_mutex_unlock(&handle->lock);
873 }
874
875 static int fs_handle_tracked_unlink(struct fs_handle *_handle)
876 {
877 int ret;
878 struct fs_handle_tracked *handle =
879 container_of(_handle, struct fs_handle_tracked, parent);
880
881 pthread_mutex_lock(&handle->tracker->lock);
882 pthread_mutex_lock(&handle->lock);
883 ret = lttng_inode_unlink(handle->inode);
884 pthread_mutex_unlock(&handle->lock);
885 pthread_mutex_unlock(&handle->tracker->lock);
886 return ret;
887 }
888
889 static int fs_handle_tracked_close(struct fs_handle *_handle)
890 {
891 int ret = 0;
892 const char *path = NULL;
893 struct fs_handle_tracked *handle =
894 container_of(_handle, struct fs_handle_tracked, parent);
895
896 if (!handle) {
897 ret = -EINVAL;
898 goto end;
899 }
900
901 pthread_mutex_lock(&handle->tracker->lock);
902 pthread_mutex_lock(&handle->lock);
903 if (handle->inode) {
904 lttng_inode_get_location(handle->inode, NULL, &path);
905 }
906 fd_tracker_untrack(handle->tracker, handle);
907 if (handle->fd >= 0) {
908 /*
909 * The return value of close() is not propagated as there
910 * isn't much the user can do about it.
911 */
912 if (close(handle->fd)) {
913 PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
914 handle->fd, path ? path : "Unknown");
915 }
916 handle->fd = -1;
917 }
918 if (handle->inode) {
919 lttng_inode_put(handle->inode);
920 }
921 pthread_mutex_unlock(&handle->lock);
922 pthread_mutex_destroy(&handle->lock);
923 pthread_mutex_unlock(&handle->tracker->lock);
924 free(handle);
925 end:
926 return ret;
927 }
This page took 0.047199 seconds and 4 git commands to generate.