Backport: fd-tracker: log tracker capacity on creation
[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/ref.h>
19 #include <urcu.h>
20 #include <urcu/list.h>
21 #include <urcu/rculfhash.h>
22
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #include <stdbool.h>
27 #include <pthread.h>
28 #include <inttypes.h>
29
30 #include "common/macros.h"
31 #include "common/error.h"
32 #include "common/defaults.h"
33 #include "common/hashtable/utils.h"
34 #include "common/hashtable/hashtable.h"
35
36 #include "fd-tracker.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 };
89
90 struct open_properties {
91 char *path;
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 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 };
134
135 static struct {
136 pthread_mutex_t lock;
137 bool initialized;
138 unsigned long value;
139 } seed = {
140 .lock = PTHREAD_MUTEX_INITIALIZER,
141 };
142
143 static int match_fd(struct cds_lfht_node *node, const void *key);
144 static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry);
145 static struct unsuspendable_fd *unsuspendable_fd_create(const char *name,
146 int fd);
147 static int open_from_properties(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(struct fd_tracker *tracker,
154 struct fs_handle *handle);
155 static void fd_tracker_untrack(struct fd_tracker *tracker,
156 struct fs_handle *handle);
157 static int fd_tracker_suspend_handles(struct fd_tracker *tracker,
158 unsigned int count);
159 static int fd_tracker_restore_handle(struct fd_tracker *tracker,
160 struct fs_handle *handle);
161
162 /* Match function of the tracker's unsuspendable_fds hash table. */
163 static
164 int match_fd(struct cds_lfht_node *node, const void *key)
165 {
166 struct unsuspendable_fd *entry =
167 caa_container_of(node, struct unsuspendable_fd, tracker_node);
168
169 return hash_match_key_ulong((void *) (unsigned long) entry->fd,
170 (void *) key);
171 }
172
173 static
174 void unsuspendable_fd_destroy(struct unsuspendable_fd *entry)
175 {
176 if (!entry) {
177 return;
178 }
179 free(entry->name);
180 free(entry);
181 }
182
183 static
184 struct unsuspendable_fd *unsuspendable_fd_create(const char *name, int fd)
185 {
186 struct unsuspendable_fd *entry =
187 zmalloc(sizeof(*entry));
188
189 if (!entry) {
190 goto error;
191 }
192 if (name) {
193 entry->name = strdup(name);
194 if (!entry->name) {
195 goto error;
196 }
197 }
198 cds_lfht_node_init(&entry->tracker_node);
199 entry->fd = fd;
200 return entry;
201 error:
202 unsuspendable_fd_destroy(entry);
203 return NULL;
204 }
205
206 static
207 void fs_handle_log(struct fs_handle *handle)
208 {
209 pthread_mutex_lock(&handle->lock);
210 if (handle->fd >= 0) {
211 DBG_NO_LOC(" %s [active, fd %d%s]",
212 handle->properties.path,
213 handle->fd,
214 handle->in_use ? ", in use" : "");
215 } else {
216 DBG_NO_LOC(" %s [suspended]", handle->properties.path);
217 }
218 pthread_mutex_unlock(&handle->lock);
219 }
220
221 static
222 int fs_handle_suspend(struct fs_handle *handle)
223 {
224 int ret = 0;
225 struct stat fs_stat;
226
227 pthread_mutex_lock(&handle->lock);
228 assert(handle->fd >= 0);
229 if (handle->in_use) {
230 /* This handle can't be suspended as it is currently in use. */
231 ret = -EAGAIN;
232 goto end;
233 }
234
235 ret = stat(handle->properties.path, &fs_stat);
236 if (ret) {
237 PERROR("Filesystem handle to %s cannot be suspended as stat() failed",
238 handle->properties.path);
239 ret = -errno;
240 goto end;
241 }
242
243 if (fs_stat.st_ino != handle->ino) {
244 /* Don't suspend as the handle would not be restorable. */
245 WARN("Filesystem handle to %s cannot be suspended as its inode changed",
246 handle->properties.path);
247 ret = -ENOENT;
248 goto end;
249 }
250
251 handle->offset = lseek(handle->fd, 0, SEEK_CUR);
252 if (handle->offset == -1) {
253 WARN("Filesystem handle to %s cannot be suspended as lseek() failed to sample its current position",
254 handle->properties.path);
255 ret = -errno;
256 goto end;
257 }
258
259 ret = close(handle->fd);
260 if (ret) {
261 PERROR("Filesystem handle to %s cannot be suspended as close() failed",
262 handle->properties.path);
263 ret = -errno;
264 goto end;
265 }
266 DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64,
267 handle->properties.path, handle->fd, handle->offset);
268 handle->fd = -1;
269 end:
270 if (ret) {
271 handle->tracker->stats.errors++;
272 }
273 pthread_mutex_unlock(&handle->lock);
274 return ret;
275 }
276
277 /* Caller must hold the tracker and handle's locks. */
278 static
279 int fs_handle_restore(struct fs_handle *handle)
280 {
281 int ret, fd = -1;
282
283 assert(handle->fd == -1);
284 ret = open_from_properties(&handle->properties);
285 if (ret < 0) {
286 PERROR("Failed to restore filesystem handle to %s, open() failed",
287 handle->properties.path);
288 ret = -errno;
289 goto end;
290 }
291 fd = ret;
292
293 ret = lseek(fd, handle->offset, SEEK_SET);
294 if (ret < 0) {
295 PERROR("Failed to restore filesystem handle to %s, lseek() failed",
296 handle->properties.path);
297 ret = -errno;
298 goto end;
299 }
300 DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64,
301 handle->properties.path, fd, handle->offset);
302 ret = 0;
303 handle->fd = fd;
304 fd = -1;
305 end:
306 if (fd >= 0) {
307 (void) close(fd);
308 }
309 return ret;
310 }
311
312 static
313 int open_from_properties(struct open_properties *properties)
314 {
315 int ret;
316
317 /*
318 * open() ignores the 'flags' parameter unless the O_CREAT or O_TMPFILE
319 * flags are set. O_TMPFILE would not make sense in the context of a
320 * suspendable fs_handle as it would not be restorable (see OPEN(2)),
321 * thus it is ignored here.
322 */
323 if ((properties->flags & O_CREAT) && properties->mode.is_set) {
324 ret = open(properties->path, properties->flags,
325 properties->mode.value);
326 } else {
327 ret = open(properties->path, properties->flags);
328 }
329 /*
330 * Some flags should not be used beyond the initial open() of a
331 * restorable file system handle. O_CREAT and O_TRUNC must
332 * be cleared since it would be unexpected to re-use them
333 * when the handle is retored:
334 * - O_CREAT should not be needed as the file has been created
335 * on the initial call to open(),
336 * - O_TRUNC would destroy the file's contents by truncating it
337 * to length 0.
338 */
339 properties->flags &= ~(O_CREAT | O_TRUNC);
340 if (ret < 0) {
341 ret = -errno;
342 goto end;
343 }
344 end:
345 return ret;
346 }
347
348 struct fd_tracker *fd_tracker_create(unsigned int capacity)
349 {
350 struct fd_tracker *tracker = zmalloc(sizeof(struct fd_tracker));
351
352 if (!tracker) {
353 goto end;
354 }
355
356 pthread_mutex_lock(&seed.lock);
357 if (!seed.initialized) {
358 seed.value = (unsigned long) time(NULL);
359 seed.initialized = true;
360 }
361 pthread_mutex_unlock(&seed.lock);
362
363 CDS_INIT_LIST_HEAD(&tracker->active_handles);
364 CDS_INIT_LIST_HEAD(&tracker->suspended_handles);
365 tracker->capacity = capacity;
366 tracker->unsuspendable_fds = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
367 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
368 DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
369 capacity);
370 end:
371 return tracker;
372 }
373
374 void fd_tracker_log(struct fd_tracker *tracker)
375 {
376 struct fs_handle *handle;
377 struct unsuspendable_fd *unsuspendable_fd;
378 struct cds_lfht_iter iter;
379
380 pthread_mutex_lock(&tracker->lock);
381 DBG_NO_LOC("File descriptor tracker");
382 DBG_NO_LOC(" Stats:");
383 DBG_NO_LOC(" uses: %" PRIu64, tracker->stats.uses);
384 DBG_NO_LOC(" misses: %" PRIu64, tracker->stats.misses);
385 DBG_NO_LOC(" errors: %" PRIu64, tracker->stats.errors);
386 DBG_NO_LOC(" Tracked: %u", TRACKED_COUNT(tracker));
387 DBG_NO_LOC(" active: %u", ACTIVE_COUNT(tracker));
388 DBG_NO_LOC(" suspendable: %u", SUSPENDABLE_COUNT(tracker));
389 DBG_NO_LOC(" unsuspendable: %u", UNSUSPENDABLE_COUNT(tracker));
390 DBG_NO_LOC(" suspended: %u", SUSPENDED_COUNT(tracker));
391 DBG_NO_LOC(" capacity: %u", tracker->capacity);
392
393 DBG_NO_LOC(" Tracked suspendable file descriptors");
394 cds_list_for_each_entry(handle, &tracker->active_handles,
395 handles_list_node) {
396 fs_handle_log(handle);
397 }
398 cds_list_for_each_entry(handle, &tracker->suspended_handles,
399 handles_list_node) {
400 fs_handle_log(handle);
401 }
402 if (!SUSPENDABLE_COUNT(tracker)) {
403 DBG_NO_LOC(" None");
404 }
405
406 DBG_NO_LOC(" Tracked unsuspendable file descriptors");
407 rcu_read_lock();
408 cds_lfht_for_each_entry(tracker->unsuspendable_fds, &iter,
409 unsuspendable_fd, tracker_node) {
410 DBG_NO_LOC(" %s [active, fd %d]", unsuspendable_fd->name ? : "Unnamed",
411 unsuspendable_fd->fd);
412 }
413 rcu_read_unlock();
414 if (!UNSUSPENDABLE_COUNT(tracker)) {
415 DBG_NO_LOC(" None");
416 }
417
418 pthread_mutex_unlock(&tracker->lock);
419 }
420
421 int fd_tracker_destroy(struct fd_tracker *tracker)
422 {
423 int ret = 0;
424
425 /*
426 * Refuse to destroy the tracker as fs_handles may still old
427 * weak references to the tracker.
428 */
429 pthread_mutex_lock(&tracker->lock);
430 if (TRACKED_COUNT(tracker)) {
431 ERR("A file descriptor leak has been detected: %u tracked file descriptors are still being tracked",
432 TRACKED_COUNT(tracker));
433 pthread_mutex_unlock(&tracker->lock);
434 fd_tracker_log(tracker);
435 ret = -1;
436 goto end;
437 }
438 pthread_mutex_unlock(&tracker->lock);
439
440 ret = cds_lfht_destroy(tracker->unsuspendable_fds, NULL);
441 assert(!ret);
442 pthread_mutex_destroy(&tracker->lock);
443 free(tracker);
444 end:
445 return ret;
446 }
447
448 struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
449 const char *path, int flags, mode_t *mode)
450 {
451 int ret;
452 struct fs_handle *handle = NULL;
453 struct stat fd_stat;
454 struct open_properties properties = {
455 .path = strdup(path),
456 .flags = flags,
457 .mode.is_set = !!mode,
458 .mode.value = mode ? *mode : 0,
459 };
460
461 if (!properties.path) {
462 goto end;
463 }
464
465 pthread_mutex_lock(&tracker->lock);
466 if (ACTIVE_COUNT(tracker) == tracker->capacity) {
467 if (tracker->count.suspendable.active > 0) {
468 ret = fd_tracker_suspend_handles(tracker, 1);
469 if (ret) {
470 goto error_destroy;
471 }
472 } else {
473 /*
474 * There are not enough active suspendable file
475 * descriptors to open a new fd and still accomodate the
476 * tracker's capacity.
477 */
478 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
479 tracker->count.unsuspendable);
480 ret = -EMFILE;
481 goto error_destroy;
482 }
483 }
484
485 handle = zmalloc(sizeof(*handle));
486 if (!handle) {
487 goto end;
488 }
489
490 ret = pthread_mutex_init(&handle->lock, NULL);
491 if (ret) {
492 PERROR("Failed to initialize handle mutex while creating fs handle");
493 free(handle);
494 goto end;
495 }
496
497 handle->fd = open_from_properties(&properties);
498 if (handle->fd < 0) {
499 PERROR("Failed to open fs handle to %s, open() returned", path);
500 ret = -errno;
501 goto error_destroy;
502 }
503
504 /*
505 * Clear the create flag from the open flags as it would make no sense
506 * to use it when restoring a fs handle.
507 */
508 properties.flags &= ~O_CREAT;
509 handle->properties = properties;
510 properties.path = NULL;
511
512 if (fstat(handle->fd, &fd_stat)) {
513 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
514 ret = -errno;
515 goto error_destroy;
516 }
517 handle->ino = fd_stat.st_ino;
518
519 fd_tracker_track(tracker, handle);
520 handle->tracker = tracker;
521 pthread_mutex_unlock(&tracker->lock);
522 end:
523 free(properties.path);
524 return handle;
525 error_destroy:
526 pthread_mutex_unlock(&tracker->lock);
527 (void) fs_handle_close(handle);
528 handle = NULL;
529 goto end;
530 }
531
532 /* Caller must hold the tracker's lock. */
533 static
534 int fd_tracker_suspend_handles(struct fd_tracker *tracker,
535 unsigned int count)
536 {
537 unsigned int left_to_close = count;
538 struct fs_handle *handle, *tmp;
539
540 cds_list_for_each_entry_safe(handle, tmp, &tracker->active_handles,
541 handles_list_node) {
542 int ret;
543
544 fd_tracker_untrack(tracker, handle);
545 ret = fs_handle_suspend(handle);
546 fd_tracker_track(tracker, handle);
547 if (!ret) {
548 left_to_close--;
549 }
550
551 if (!left_to_close) {
552 break;
553 }
554 }
555 return left_to_close ? -EMFILE : 0;
556 }
557
558 int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
559 int *out_fds, const char **names, unsigned int fd_count,
560 fd_open_cb open, void *user_data)
561 {
562 int ret, user_ret, i, fds_to_suspend;
563 unsigned int active_fds;
564 struct unsuspendable_fd *entries[fd_count];
565
566 memset(entries, 0, sizeof(entries));
567
568 pthread_mutex_lock(&tracker->lock);
569
570 active_fds = ACTIVE_COUNT(tracker);
571 fds_to_suspend = (int) active_fds + (int) fd_count - (int) tracker->capacity;
572 if (fds_to_suspend > 0) {
573 if (fds_to_suspend <= tracker->count.suspendable.active) {
574 ret = fd_tracker_suspend_handles(tracker, fds_to_suspend);
575 if (ret) {
576 goto end;
577 }
578 } else {
579 /*
580 * There are not enough active suspendable file
581 * descriptors to open a new fd and still accomodate the
582 * tracker's capacity.
583 */
584 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
585 tracker->count.unsuspendable);
586 ret = -EMFILE;
587 goto end;
588 }
589 }
590
591 user_ret = open(user_data, out_fds);
592 if (user_ret) {
593 ret = user_ret;
594 goto end;
595 }
596
597 /*
598 * Add the fds returned by the user's callback to the hashtable
599 * of unsuspendable fds.
600 */
601 for (i = 0; i < fd_count; i++) {
602 struct unsuspendable_fd *entry =
603 unsuspendable_fd_create(names ? names[i] : NULL,
604 out_fds[i]);
605
606 if (!entry) {
607 ret = -1;
608 goto end_free_entries;
609 }
610 entries[i] = entry;
611 }
612
613 rcu_read_lock();
614 for (i = 0; i < fd_count; i++) {
615 struct cds_lfht_node *node;
616 struct unsuspendable_fd *entry = entries[i];
617
618 node = cds_lfht_add_unique(
619 tracker->unsuspendable_fds,
620 hash_key_ulong((void *) (unsigned long) out_fds[i],
621 seed.value),
622 match_fd,
623 (void *) (unsigned long) out_fds[i],
624 &entry->tracker_node);
625
626 if (node != &entry->tracker_node) {
627 ret = -EEXIST;
628 rcu_read_unlock();
629 goto end_free_entries;
630 }
631 entries[i] = NULL;
632 }
633 tracker->count.unsuspendable += fd_count;
634 rcu_read_unlock();
635 ret = user_ret;
636 end:
637 pthread_mutex_unlock(&tracker->lock);
638 return ret;
639 end_free_entries:
640 for (i = 0; i < fd_count; i++) {
641 unsuspendable_fd_destroy(entries[i]);
642 }
643 goto end;
644 }
645
646 int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
647 int *fds_in, unsigned int fd_count, fd_close_cb close,
648 void *user_data)
649 {
650 int i, ret, user_ret;
651 int fds[fd_count];
652
653 /*
654 * Maintain a local copy of fds_in as the user's callback may modify its
655 * contents (e.g. setting the fd(s) to -1 after close).
656 */
657 memcpy(fds, fds_in, sizeof(*fds) * fd_count);
658
659 pthread_mutex_lock(&tracker->lock);
660 rcu_read_lock();
661
662 /* Let the user close the file descriptors. */
663 user_ret = close(user_data, fds_in);
664 if (user_ret) {
665 ret = user_ret;
666 goto end;
667 }
668
669 /* Untrack the fds that were just closed by the user's callback. */
670 for (i = 0; i < fd_count; i++) {
671 struct cds_lfht_node *node;
672 struct cds_lfht_iter iter;
673 struct unsuspendable_fd *entry;
674
675 cds_lfht_lookup(tracker->unsuspendable_fds,
676 hash_key_ulong((void *) (unsigned long) fds[i],
677 seed.value),
678 match_fd,
679 (void *) (unsigned long) fds[i],
680 &iter);
681 node = cds_lfht_iter_get_node(&iter);
682 if (!node) {
683 /* Unknown file descriptor. */
684 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
685 fds[i]);
686 ret = -EINVAL;
687 goto end;
688 }
689 entry = caa_container_of(node,
690 struct unsuspendable_fd,
691 tracker_node);
692
693 cds_lfht_del(tracker->unsuspendable_fds, node);
694 unsuspendable_fd_destroy(entry);
695 fds[i] = -1;
696 }
697
698 tracker->count.unsuspendable -= fd_count;
699 ret = 0;
700 end:
701 rcu_read_unlock();
702 pthread_mutex_unlock(&tracker->lock);
703 return ret;
704 }
705
706 /* Caller must have taken the tracker's and handle's locks. */
707 static
708 void fd_tracker_track(struct fd_tracker *tracker, struct fs_handle *handle)
709 {
710 if (handle->fd >= 0) {
711 tracker->count.suspendable.active++;
712 cds_list_add_tail(&handle->handles_list_node,
713 &tracker->active_handles);
714 } else {
715 tracker->count.suspendable.suspended++;
716 cds_list_add_tail(&handle->handles_list_node,
717 &tracker->suspended_handles);
718 }
719 }
720
721 /* Caller must have taken the tracker's and handle's locks. */
722 static
723 void fd_tracker_untrack(struct fd_tracker *tracker, struct fs_handle *handle)
724 {
725 if (handle->fd >= 0) {
726 tracker->count.suspendable.active--;
727 } else {
728 tracker->count.suspendable.suspended--;
729 }
730 cds_list_del(&handle->handles_list_node);
731 }
732
733 /* Caller must have taken the tracker's and handle's locks. */
734 static
735 int fd_tracker_restore_handle(struct fd_tracker *tracker,
736 struct fs_handle *handle)
737 {
738 int ret;
739
740 fd_tracker_untrack(tracker, handle);
741 if (ACTIVE_COUNT(tracker) >= tracker->capacity) {
742 ret = fd_tracker_suspend_handles(tracker, 1);
743 if (ret) {
744 goto end;
745 }
746 }
747 ret = fs_handle_restore(handle);
748 end:
749 fd_tracker_track(tracker, handle);
750 return ret ? ret : handle->fd;
751 }
752
753 int fs_handle_get_fd(struct fs_handle *handle)
754 {
755 int ret;
756
757 pthread_mutex_lock(&handle->tracker->lock);
758 pthread_mutex_lock(&handle->lock);
759 assert(!handle->in_use);
760
761 handle->tracker->stats.uses++;
762 if (handle->fd >= 0) {
763 ret = handle->fd;
764 /* Mark as most recently used. */
765 fd_tracker_untrack(handle->tracker, handle);
766 fd_tracker_track(handle->tracker, handle);
767 } else {
768 handle->tracker->stats.misses++;
769 ret = fd_tracker_restore_handle(handle->tracker, handle);
770 if (ret < 0) {
771 handle->tracker->stats.errors++;
772 goto end;
773 }
774 }
775 handle->in_use = true;
776 end:
777 pthread_mutex_unlock(&handle->lock);
778 pthread_mutex_unlock(&handle->tracker->lock);
779 return ret;
780 }
781
782 void fs_handle_put_fd(struct fs_handle *handle)
783 {
784 pthread_mutex_lock(&handle->lock);
785 handle->in_use = false;
786 pthread_mutex_unlock(&handle->lock);
787 }
788
789 int fs_handle_close(struct fs_handle *handle)
790 {
791 int ret = 0;
792
793 if (!handle) {
794 ret = -EINVAL;
795 goto end;
796 }
797
798 pthread_mutex_lock(&handle->tracker->lock);
799 pthread_mutex_lock(&handle->lock);
800 fd_tracker_untrack(handle->tracker, handle);
801 if (handle->fd >= 0) {
802 assert(!handle->in_use);
803 /*
804 * The return value of close() is not propagated as there
805 * isn't much the user can do about it.
806 */
807 if (close(handle->fd)) {
808 PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
809 handle->fd, handle->properties.path);
810 }
811 handle->fd = -1;
812 }
813 pthread_mutex_unlock(&handle->lock);
814 pthread_mutex_destroy(&handle->lock);
815 pthread_mutex_unlock(&handle->tracker->lock);
816 free(handle->properties.path);
817 free(handle);
818 end:
819 return ret;
820 }
This page took 0.047714 seconds and 5 git commands to generate.