fbf46ec18193ecbbc1a39af6ae400e9a21c538cd
[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 end:
369 return tracker;
370 }
371
372 void fd_tracker_log(struct fd_tracker *tracker)
373 {
374 struct fs_handle *handle;
375 struct unsuspendable_fd *unsuspendable_fd;
376 struct cds_lfht_iter iter;
377
378 pthread_mutex_lock(&tracker->lock);
379 DBG_NO_LOC("File descriptor tracker");
380 DBG_NO_LOC(" Stats:");
381 DBG_NO_LOC(" uses: %" PRIu64, tracker->stats.uses);
382 DBG_NO_LOC(" misses: %" PRIu64, tracker->stats.misses);
383 DBG_NO_LOC(" errors: %" PRIu64, tracker->stats.errors);
384 DBG_NO_LOC(" Tracked: %u", TRACKED_COUNT(tracker));
385 DBG_NO_LOC(" active: %u", ACTIVE_COUNT(tracker));
386 DBG_NO_LOC(" suspendable: %u", SUSPENDABLE_COUNT(tracker));
387 DBG_NO_LOC(" unsuspendable: %u", UNSUSPENDABLE_COUNT(tracker));
388 DBG_NO_LOC(" suspended: %u", SUSPENDED_COUNT(tracker));
389 DBG_NO_LOC(" capacity: %u", tracker->capacity);
390
391 DBG_NO_LOC(" Tracked suspendable file descriptors");
392 cds_list_for_each_entry(handle, &tracker->active_handles,
393 handles_list_node) {
394 fs_handle_log(handle);
395 }
396 cds_list_for_each_entry(handle, &tracker->suspended_handles,
397 handles_list_node) {
398 fs_handle_log(handle);
399 }
400 if (!SUSPENDABLE_COUNT(tracker)) {
401 DBG_NO_LOC(" None");
402 }
403
404 DBG_NO_LOC(" Tracked unsuspendable file descriptors");
405 rcu_read_lock();
406 cds_lfht_for_each_entry(tracker->unsuspendable_fds, &iter,
407 unsuspendable_fd, tracker_node) {
408 DBG_NO_LOC(" %s [active, fd %d]", unsuspendable_fd->name ? : "Unnamed",
409 unsuspendable_fd->fd);
410 }
411 rcu_read_unlock();
412 if (!UNSUSPENDABLE_COUNT(tracker)) {
413 DBG_NO_LOC(" None");
414 }
415
416 pthread_mutex_unlock(&tracker->lock);
417 }
418
419 int fd_tracker_destroy(struct fd_tracker *tracker)
420 {
421 int ret = 0;
422
423 /*
424 * Refuse to destroy the tracker as fs_handles may still old
425 * weak references to the tracker.
426 */
427 pthread_mutex_lock(&tracker->lock);
428 if (TRACKED_COUNT(tracker)) {
429 ERR("A file descriptor leak has been detected: %u tracked file descriptors are still being tracked",
430 TRACKED_COUNT(tracker));
431 pthread_mutex_unlock(&tracker->lock);
432 fd_tracker_log(tracker);
433 ret = -1;
434 goto end;
435 }
436 pthread_mutex_unlock(&tracker->lock);
437
438 ret = cds_lfht_destroy(tracker->unsuspendable_fds, NULL);
439 assert(!ret);
440 pthread_mutex_destroy(&tracker->lock);
441 free(tracker);
442 end:
443 return ret;
444 }
445
446 struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
447 const char *path, int flags, mode_t *mode)
448 {
449 int ret;
450 struct fs_handle *handle = NULL;
451 struct stat fd_stat;
452 struct open_properties properties = {
453 .path = strdup(path),
454 .flags = flags,
455 .mode.is_set = !!mode,
456 .mode.value = mode ? *mode : 0,
457 };
458
459 if (!properties.path) {
460 goto end;
461 }
462
463 pthread_mutex_lock(&tracker->lock);
464 if (ACTIVE_COUNT(tracker) == tracker->capacity) {
465 if (tracker->count.suspendable.active > 0) {
466 ret = fd_tracker_suspend_handles(tracker, 1);
467 if (ret) {
468 goto error_destroy;
469 }
470 } else {
471 /*
472 * There are not enough active suspendable file
473 * descriptors to open a new fd and still accomodate the
474 * tracker's capacity.
475 */
476 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
477 tracker->count.unsuspendable);
478 ret = -EMFILE;
479 goto error_destroy;
480 }
481 }
482
483 handle = zmalloc(sizeof(*handle));
484 if (!handle) {
485 goto end;
486 }
487
488 ret = pthread_mutex_init(&handle->lock, NULL);
489 if (ret) {
490 PERROR("Failed to initialize handle mutex while creating fs handle");
491 free(handle);
492 goto end;
493 }
494
495 handle->fd = open_from_properties(&properties);
496 if (handle->fd < 0) {
497 PERROR("Failed to open fs handle to %s, open() returned", path);
498 ret = -errno;
499 goto error_destroy;
500 }
501
502 /*
503 * Clear the create flag from the open flags as it would make no sense
504 * to use it when restoring a fs handle.
505 */
506 properties.flags &= ~O_CREAT;
507 handle->properties = properties;
508 properties.path = NULL;
509
510 if (fstat(handle->fd, &fd_stat)) {
511 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
512 ret = -errno;
513 goto error_destroy;
514 }
515 handle->ino = fd_stat.st_ino;
516
517 fd_tracker_track(tracker, handle);
518 handle->tracker = tracker;
519 pthread_mutex_unlock(&tracker->lock);
520 end:
521 free(properties.path);
522 return handle;
523 error_destroy:
524 pthread_mutex_unlock(&tracker->lock);
525 (void) fs_handle_close(handle);
526 handle = NULL;
527 goto end;
528 }
529
530 /* Caller must hold the tracker's lock. */
531 static
532 int fd_tracker_suspend_handles(struct fd_tracker *tracker,
533 unsigned int count)
534 {
535 unsigned int left_to_close = count;
536 struct fs_handle *handle, *tmp;
537
538 cds_list_for_each_entry_safe(handle, tmp, &tracker->active_handles,
539 handles_list_node) {
540 int ret;
541
542 fd_tracker_untrack(tracker, handle);
543 ret = fs_handle_suspend(handle);
544 fd_tracker_track(tracker, handle);
545 if (!ret) {
546 left_to_close--;
547 }
548
549 if (!left_to_close) {
550 break;
551 }
552 }
553 return left_to_close ? -EMFILE : 0;
554 }
555
556 int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
557 int *out_fds, const char **names, unsigned int fd_count,
558 fd_open_cb open, void *user_data)
559 {
560 int ret, user_ret, i, fds_to_suspend;
561 unsigned int active_fds;
562 struct unsuspendable_fd *entries[fd_count];
563
564 memset(entries, 0, sizeof(entries));
565
566 pthread_mutex_lock(&tracker->lock);
567
568 active_fds = ACTIVE_COUNT(tracker);
569 fds_to_suspend = (int) active_fds + (int) fd_count - (int) tracker->capacity;
570 if (fds_to_suspend > 0) {
571 if (fds_to_suspend <= tracker->count.suspendable.active) {
572 ret = fd_tracker_suspend_handles(tracker, fds_to_suspend);
573 if (ret) {
574 goto end;
575 }
576 } else {
577 /*
578 * There are not enough active suspendable file
579 * descriptors to open a new fd and still accomodate the
580 * tracker's capacity.
581 */
582 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
583 tracker->count.unsuspendable);
584 ret = -EMFILE;
585 goto end;
586 }
587 }
588
589 user_ret = open(user_data, out_fds);
590 if (user_ret) {
591 ret = user_ret;
592 goto end;
593 }
594
595 /*
596 * Add the fds returned by the user's callback to the hashtable
597 * of unsuspendable fds.
598 */
599 for (i = 0; i < fd_count; i++) {
600 struct unsuspendable_fd *entry =
601 unsuspendable_fd_create(names ? names[i] : NULL,
602 out_fds[i]);
603
604 if (!entry) {
605 ret = -1;
606 goto end_free_entries;
607 }
608 entries[i] = entry;
609 }
610
611 rcu_read_lock();
612 for (i = 0; i < fd_count; i++) {
613 struct cds_lfht_node *node;
614 struct unsuspendable_fd *entry = entries[i];
615
616 node = cds_lfht_add_unique(
617 tracker->unsuspendable_fds,
618 hash_key_ulong((void *) (unsigned long) out_fds[i],
619 seed.value),
620 match_fd,
621 (void *) (unsigned long) out_fds[i],
622 &entry->tracker_node);
623
624 if (node != &entry->tracker_node) {
625 ret = -EEXIST;
626 rcu_read_unlock();
627 goto end_free_entries;
628 }
629 entries[i] = NULL;
630 }
631 tracker->count.unsuspendable += fd_count;
632 rcu_read_unlock();
633 ret = user_ret;
634 end:
635 pthread_mutex_unlock(&tracker->lock);
636 return ret;
637 end_free_entries:
638 for (i = 0; i < fd_count; i++) {
639 unsuspendable_fd_destroy(entries[i]);
640 }
641 goto end;
642 }
643
644 int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
645 int *fds_in, unsigned int fd_count, fd_close_cb close,
646 void *user_data)
647 {
648 int i, ret, user_ret;
649 int fds[fd_count];
650
651 /*
652 * Maintain a local copy of fds_in as the user's callback may modify its
653 * contents (e.g. setting the fd(s) to -1 after close).
654 */
655 memcpy(fds, fds_in, sizeof(*fds) * fd_count);
656
657 pthread_mutex_lock(&tracker->lock);
658 rcu_read_lock();
659
660 /* Let the user close the file descriptors. */
661 user_ret = close(user_data, fds_in);
662 if (user_ret) {
663 ret = user_ret;
664 goto end;
665 }
666
667 /* Untrack the fds that were just closed by the user's callback. */
668 for (i = 0; i < fd_count; i++) {
669 struct cds_lfht_node *node;
670 struct cds_lfht_iter iter;
671 struct unsuspendable_fd *entry;
672
673 cds_lfht_lookup(tracker->unsuspendable_fds,
674 hash_key_ulong((void *) (unsigned long) fds[i],
675 seed.value),
676 match_fd,
677 (void *) (unsigned long) fds[i],
678 &iter);
679 node = cds_lfht_iter_get_node(&iter);
680 if (!node) {
681 /* Unknown file descriptor. */
682 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
683 fds[i]);
684 ret = -EINVAL;
685 goto end;
686 }
687 entry = caa_container_of(node,
688 struct unsuspendable_fd,
689 tracker_node);
690
691 cds_lfht_del(tracker->unsuspendable_fds, node);
692 unsuspendable_fd_destroy(entry);
693 fds[i] = -1;
694 }
695
696 tracker->count.unsuspendable -= fd_count;
697 ret = 0;
698 end:
699 rcu_read_unlock();
700 pthread_mutex_unlock(&tracker->lock);
701 return ret;
702 }
703
704 /* Caller must have taken the tracker's and handle's locks. */
705 static
706 void fd_tracker_track(struct fd_tracker *tracker, struct fs_handle *handle)
707 {
708 if (handle->fd >= 0) {
709 tracker->count.suspendable.active++;
710 cds_list_add_tail(&handle->handles_list_node,
711 &tracker->active_handles);
712 } else {
713 tracker->count.suspendable.suspended++;
714 cds_list_add_tail(&handle->handles_list_node,
715 &tracker->suspended_handles);
716 }
717 }
718
719 /* Caller must have taken the tracker's and handle's locks. */
720 static
721 void fd_tracker_untrack(struct fd_tracker *tracker, struct fs_handle *handle)
722 {
723 if (handle->fd >= 0) {
724 tracker->count.suspendable.active--;
725 } else {
726 tracker->count.suspendable.suspended--;
727 }
728 cds_list_del(&handle->handles_list_node);
729 }
730
731 /* Caller must have taken the tracker's and handle's locks. */
732 static
733 int fd_tracker_restore_handle(struct fd_tracker *tracker,
734 struct fs_handle *handle)
735 {
736 int ret;
737
738 fd_tracker_untrack(tracker, handle);
739 if (ACTIVE_COUNT(tracker) >= tracker->capacity) {
740 ret = fd_tracker_suspend_handles(tracker, 1);
741 if (ret) {
742 goto end;
743 }
744 }
745 ret = fs_handle_restore(handle);
746 end:
747 fd_tracker_track(tracker, handle);
748 return ret ? ret : handle->fd;
749 }
750
751 int fs_handle_get_fd(struct fs_handle *handle)
752 {
753 int ret;
754
755 pthread_mutex_lock(&handle->tracker->lock);
756 pthread_mutex_lock(&handle->lock);
757 assert(!handle->in_use);
758
759 handle->tracker->stats.uses++;
760 if (handle->fd >= 0) {
761 ret = handle->fd;
762 /* Mark as most recently used. */
763 fd_tracker_untrack(handle->tracker, handle);
764 fd_tracker_track(handle->tracker, handle);
765 } else {
766 handle->tracker->stats.misses++;
767 ret = fd_tracker_restore_handle(handle->tracker, handle);
768 if (ret < 0) {
769 handle->tracker->stats.errors++;
770 goto end;
771 }
772 }
773 handle->in_use = true;
774 end:
775 pthread_mutex_unlock(&handle->lock);
776 pthread_mutex_unlock(&handle->tracker->lock);
777 return ret;
778 }
779
780 void fs_handle_put_fd(struct fs_handle *handle)
781 {
782 pthread_mutex_lock(&handle->lock);
783 handle->in_use = false;
784 pthread_mutex_unlock(&handle->lock);
785 }
786
787 int fs_handle_close(struct fs_handle *handle)
788 {
789 int ret = 0;
790
791 if (!handle) {
792 ret = -EINVAL;
793 goto end;
794 }
795
796 pthread_mutex_lock(&handle->tracker->lock);
797 pthread_mutex_lock(&handle->lock);
798 fd_tracker_untrack(handle->tracker, handle);
799 if (handle->fd >= 0) {
800 assert(!handle->in_use);
801 /*
802 * The return value of close() is not propagated as there
803 * isn't much the user can do about it.
804 */
805 if (close(handle->fd)) {
806 PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
807 handle->fd, handle->properties.path);
808 }
809 handle->fd = -1;
810 }
811 pthread_mutex_unlock(&handle->lock);
812 pthread_mutex_destroy(&handle->lock);
813 pthread_mutex_unlock(&handle->tracker->lock);
814 free(handle->properties.path);
815 free(handle);
816 end:
817 return ret;
818 }
This page took 0.047734 seconds and 4 git commands to generate.