fd-tracker: remove use of VLA for unsuspendable_fd APIs
[lttng-tools.git] / src / common / fd-tracker / fd-tracker.c
CommitLineData
df038819 1/*
b13a2b17 2 * Copyright (C) 2018, 2020 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
df038819
JG
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
df038819 22#include <fcntl.h>
df038819 23#include <inttypes.h>
5c1f54d1
JG
24#include <pthread.h>
25#include <stdbool.h>
26#include <sys/stat.h>
27#include <sys/types.h>
df038819 28
df038819 29#include "common/defaults.h"
5c1f54d1 30#include "common/error.h"
df038819 31#include "common/hashtable/hashtable.h"
5c1f54d1
JG
32#include "common/hashtable/utils.h"
33#include "common/macros.h"
df038819
JG
34
35#include "fd-tracker.h"
874a3441 36#include "inode.h"
df038819
JG
37
38/* Tracker lock must be taken by the user. */
5c1f54d1
JG
39#define TRACKED_COUNT(tracker) \
40 (tracker->count.suspendable.active + \
41 tracker->count.suspendable.suspended + \
42 tracker->count.unsuspendable)
df038819
JG
43
44/* Tracker lock must be taken by the user. */
5c1f54d1
JG
45#define ACTIVE_COUNT(tracker) \
46 (tracker->count.suspendable.active + tracker->count.unsuspendable)
df038819
JG
47
48/* Tracker lock must be taken by the user. */
5c1f54d1 49#define SUSPENDED_COUNT(tracker) (tracker->count.suspendable.suspended)
df038819
JG
50
51/* Tracker lock must be taken by the user. */
5c1f54d1
JG
52#define SUSPENDABLE_COUNT(tracker) \
53 (tracker->count.suspendable.active + \
54 tracker->count.suspendable.suspended)
df038819
JG
55
56/* Tracker lock must be taken by the user. */
5c1f54d1 57#define UNSUSPENDABLE_COUNT(tracker) (tracker->count.unsuspendable)
df038819
JG
58
59struct fd_tracker {
60 pthread_mutex_t lock;
61 struct {
5c1f54d1 62 struct {
df038819
JG
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;
874a3441 85 struct lttng_inode_registry *inode_registry;
df038819
JG
86};
87
88struct open_properties {
df038819
JG
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 */
105struct 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;
874a3441 113 struct lttng_inode *inode;
df038819
JG
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
123struct 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
134static struct {
135 pthread_mutex_t lock;
136 bool initialized;
137 unsigned long value;
138} seed = {
139 .lock = PTHREAD_MUTEX_INITIALIZER,
140};
141
142static int match_fd(struct cds_lfht_node *node, const void *key);
143static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry);
5c1f54d1
JG
144static struct unsuspendable_fd *unsuspendable_fd_create(
145 const char *name, int fd);
146static int open_from_properties(
147 const char *path, struct open_properties *properties);
df038819
JG
148
149static void fs_handle_log(struct fs_handle *handle);
150static int fs_handle_suspend(struct fs_handle *handle);
151static int fs_handle_restore(struct fs_handle *handle);
152
5c1f54d1
JG
153static void fd_tracker_track(
154 struct fd_tracker *tracker, struct fs_handle *handle);
155static void fd_tracker_untrack(
156 struct fd_tracker *tracker, struct fs_handle *handle);
157static int fd_tracker_suspend_handles(
158 struct fd_tracker *tracker, unsigned int count);
159static int fd_tracker_restore_handle(
160 struct fd_tracker *tracker, struct fs_handle *handle);
df038819
JG
161
162/* Match function of the tracker's unsuspendable_fds hash table. */
5c1f54d1 163static int match_fd(struct cds_lfht_node *node, const void *key)
df038819 164{
5c1f54d1
JG
165 struct unsuspendable_fd *entry = caa_container_of(
166 node, struct unsuspendable_fd, tracker_node);
df038819 167
5c1f54d1
JG
168 return hash_match_key_ulong(
169 (void *) (unsigned long) entry->fd, (void *) key);
df038819
JG
170}
171
5c1f54d1 172static void delete_unsuspendable_fd(struct rcu_head *head)
df038819 173{
5c1f54d1
JG
174 struct unsuspendable_fd *fd = caa_container_of(
175 head, struct unsuspendable_fd, rcu_head);
df038819
JG
176
177 free(fd->name);
178 free(fd);
179}
180
5c1f54d1 181static void unsuspendable_fd_destroy(struct unsuspendable_fd *entry)
df038819
JG
182{
183 if (!entry) {
184 return;
185 }
186 call_rcu(&entry->rcu_head, delete_unsuspendable_fd);
187}
188
5c1f54d1
JG
189static struct unsuspendable_fd *unsuspendable_fd_create(
190 const char *name, int fd)
df038819 191{
5c1f54d1 192 struct unsuspendable_fd *entry = zmalloc(sizeof(*entry));
df038819
JG
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;
206error:
207 unsuspendable_fd_destroy(entry);
208 return NULL;
209}
210
5c1f54d1 211static void fs_handle_log(struct fs_handle *handle)
df038819 212{
874a3441
JG
213 const char *path;
214
df038819 215 pthread_mutex_lock(&handle->lock);
874a3441
JG
216 path = lttng_inode_get_path(handle->inode);
217
df038819 218 if (handle->fd >= 0) {
5c1f54d1 219 DBG_NO_LOC(" %s [active, fd %d%s]", path, handle->fd,
df038819
JG
220 handle->in_use ? ", in use" : "");
221 } else {
874a3441 222 DBG_NO_LOC(" %s [suspended]", path);
df038819
JG
223 }
224 pthread_mutex_unlock(&handle->lock);
225}
226
874a3441 227/* Tracker lock must be held by the caller. */
5c1f54d1 228static int fs_handle_suspend(struct fs_handle *handle)
df038819
JG
229{
230 int ret = 0;
231 struct stat fs_stat;
874a3441 232 const char *path;
df038819
JG
233
234 pthread_mutex_lock(&handle->lock);
874a3441 235 path = lttng_inode_get_path(handle->inode);
df038819
JG
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
874a3441 243 ret = stat(path, &fs_stat);
df038819 244 if (ret) {
5c1f54d1 245 PERROR("Filesystem handle to %s cannot be suspended as stat() failed",
874a3441 246 path);
df038819
JG
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",
874a3441 254 path);
df038819
JG
255 ret = -ENOENT;
256 goto end;
257 }
258
5c1f54d1 259 handle->offset = lseek(handle->fd, 0, SEEK_CUR);
df038819
JG
260 if (handle->offset == -1) {
261 WARN("Filesystem handle to %s cannot be suspended as lseek() failed to sample its current position",
874a3441 262 path);
df038819
JG
263 ret = -errno;
264 goto end;
265 }
266
267 ret = close(handle->fd);
268 if (ret) {
5c1f54d1 269 PERROR("Filesystem handle to %s cannot be suspended as close() failed",
874a3441 270 path);
df038819
JG
271 ret = -errno;
272 goto end;
273 }
274 DBG("Suspended filesystem handle to %s (fd %i) at position %" PRId64,
874a3441 275 path, handle->fd, handle->offset);
df038819
JG
276 handle->fd = -1;
277end:
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. */
5c1f54d1 286static int fs_handle_restore(struct fs_handle *handle)
df038819
JG
287{
288 int ret, fd = -1;
874a3441 289 const char *path = lttng_inode_get_path(handle->inode);
df038819
JG
290
291 assert(handle->fd == -1);
874a3441 292 assert(path);
5c1f54d1 293 ret = open_from_properties(path, &handle->properties);
df038819 294 if (ret < 0) {
5c1f54d1 295 PERROR("Failed to restore filesystem handle to %s, open() failed",
874a3441 296 path);
df038819
JG
297 ret = -errno;
298 goto end;
299 }
300 fd = ret;
301
302 ret = lseek(fd, handle->offset, SEEK_SET);
303 if (ret < 0) {
5c1f54d1 304 PERROR("Failed to restore filesystem handle to %s, lseek() failed",
874a3441 305 path);
df038819
JG
306 ret = -errno;
307 goto end;
308 }
309 DBG("Restored filesystem handle to %s (fd %i) at position %" PRId64,
874a3441 310 path, fd, handle->offset);
df038819
JG
311 ret = 0;
312 handle->fd = fd;
313 fd = -1;
314end:
315 if (fd >= 0) {
316 (void) close(fd);
317 }
318 return ret;
319}
320
5c1f54d1
JG
321static int open_from_properties(
322 const char *path, struct open_properties *properties)
df038819
JG
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) {
5c1f54d1 333 ret = open(path, properties->flags, properties->mode.value);
df038819 334 } else {
874a3441 335 ret = open(path, properties->flags);
df038819
JG
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 }
352end:
353 return ret;
354}
355
356struct 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);
874a3441
JG
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 }
df038819
JG
385 DBG("File descriptor tracker created with a limit of %u simultaneously-opened FDs",
386 capacity);
387end:
388 return tracker;
874a3441
JG
389error:
390 fd_tracker_destroy(tracker);
391 return NULL;
df038819
JG
392}
393
394void 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");
5c1f54d1
JG
414 cds_list_for_each_entry (
415 handle, &tracker->active_handles, handles_list_node) {
df038819
JG
416 fs_handle_log(handle);
417 }
5c1f54d1 418 cds_list_for_each_entry (handle, &tracker->suspended_handles,
df038819
JG
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();
5c1f54d1 428 cds_lfht_for_each_entry (tracker->unsuspendable_fds, &iter,
df038819 429 unsuspendable_fd, tracker_node) {
5c1f54d1
JG
430 DBG_NO_LOC(" %s [active, fd %d]",
431 unsuspendable_fd->name ?: "Unnamed",
df038819
JG
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
442int 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
874a3441
JG
461 if (tracker->unsuspendable_fds) {
462 ret = cds_lfht_destroy(tracker->unsuspendable_fds, NULL);
463 assert(!ret);
464 }
660a216c
JG
465
466 lttng_inode_registry_destroy(tracker->inode_registry);
df038819
JG
467 pthread_mutex_destroy(&tracker->lock);
468 free(tracker);
469end:
470 return ret;
471}
472
473struct fs_handle *fd_tracker_open_fs_handle(struct fd_tracker *tracker,
5c1f54d1
JG
474 const char *path,
475 int flags,
476 mode_t *mode)
df038819
JG
477{
478 int ret;
479 struct fs_handle *handle = NULL;
480 struct stat fd_stat;
481 struct open_properties properties = {
df038819
JG
482 .flags = flags,
483 .mode.is_set = !!mode,
484 .mode.value = mode ? *mode : 0,
485 };
486
df038819
JG
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) {
19380ea8 492 goto end;
df038819
JG
493 }
494 } else {
495 /*
496 * There are not enough active suspendable file
19380ea8
JG
497 * descriptors to open a new fd and still accommodate
498 * the tracker's capacity.
df038819
JG
499 */
500 WARN("Cannot open file system handle, too many unsuspendable file descriptors are opened (%u)",
501 tracker->count.unsuspendable);
19380ea8 502 goto end;
df038819
JG
503 }
504 }
505
506 handle = zmalloc(sizeof(*handle));
507 if (!handle) {
508 goto end;
509 }
660a216c 510 handle->tracker = tracker;
df038819
JG
511
512 ret = pthread_mutex_init(&handle->lock, NULL);
513 if (ret) {
514 PERROR("Failed to initialize handle mutex while creating fs handle");
19380ea8 515 goto error_mutex_init;
df038819
JG
516 }
517
874a3441 518 handle->fd = open_from_properties(path, &properties);
df038819
JG
519 if (handle->fd < 0) {
520 PERROR("Failed to open fs handle to %s, open() returned", path);
19380ea8 521 goto error;
df038819
JG
522 }
523
524 handle->properties = properties;
874a3441 525
5c1f54d1
JG
526 handle->inode = lttng_inode_registry_get_inode(
527 tracker->inode_registry, handle->fd, path);
874a3441 528 if (!handle->inode) {
5c1f54d1 529 ERR("Failed to get lttng_inode corresponding to file %s", path);
19380ea8 530 goto error;
874a3441 531 }
df038819
JG
532
533 if (fstat(handle->fd, &fd_stat)) {
534 PERROR("Failed to retrieve file descriptor inode while creating fs handle, fstat() returned");
19380ea8 535 goto error;
df038819
JG
536 }
537 handle->ino = fd_stat.st_ino;
538
539 fd_tracker_track(tracker, handle);
df038819 540end:
19380ea8 541 pthread_mutex_unlock(&tracker->lock);
df038819 542 return handle;
19380ea8 543error:
660a216c
JG
544 if (handle->inode) {
545 lttng_inode_put(handle->inode);
546 }
19380ea8
JG
547 pthread_mutex_destroy(&handle->lock);
548error_mutex_init:
660a216c 549 free(handle);
df038819
JG
550 handle = NULL;
551 goto end;
552}
553
554/* Caller must hold the tracker's lock. */
5c1f54d1
JG
555static int fd_tracker_suspend_handles(
556 struct fd_tracker *tracker, unsigned int count)
df038819
JG
557{
558 unsigned int left_to_close = count;
559 struct fs_handle *handle, *tmp;
560
5c1f54d1 561 cds_list_for_each_entry_safe (handle, tmp, &tracker->active_handles,
df038819
JG
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
579int fd_tracker_open_unsuspendable_fd(struct fd_tracker *tracker,
5c1f54d1
JG
580 int *out_fds,
581 const char **names,
582 unsigned int fd_count,
583 fd_open_cb open,
584 void *user_data)
df038819
JG
585{
586 int ret, user_ret, i, fds_to_suspend;
587 unsigned int active_fds;
b13a2b17 588 struct unsuspendable_fd **entries;
df038819 589
b13a2b17
JG
590 entries = zmalloc(fd_count * sizeof(*entries));
591 if (!entries) {
592 ret = -1;
593 goto end;
594 }
df038819
JG
595
596 pthread_mutex_lock(&tracker->lock);
597
598 active_fds = ACTIVE_COUNT(tracker);
5c1f54d1
JG
599 fds_to_suspend = (int) active_fds + (int) fd_count -
600 (int) tracker->capacity;
df038819
JG
601 if (fds_to_suspend > 0) {
602 if (fds_to_suspend <= tracker->count.suspendable.active) {
5c1f54d1
JG
603 ret = fd_tracker_suspend_handles(
604 tracker, fds_to_suspend);
df038819 605 if (ret) {
b13a2b17 606 goto end_unlock;
df038819
JG
607 }
608 } else {
609 /*
610 * There are not enough active suspendable file
611 * descriptors to open a new fd and still accomodate the
612 * tracker's capacity.
613 */
614 WARN("Cannot open unsuspendable fd, too many unsuspendable file descriptors are opened (%u)",
615 tracker->count.unsuspendable);
616 ret = -EMFILE;
b13a2b17 617 goto end_unlock;
df038819
JG
618 }
619 }
620
621 user_ret = open(user_data, out_fds);
622 if (user_ret) {
623 ret = user_ret;
b13a2b17 624 goto end_unlock;
df038819
JG
625 }
626
627 /*
628 * Add the fds returned by the user's callback to the hashtable
629 * of unsuspendable fds.
630 */
631 for (i = 0; i < fd_count; i++) {
5c1f54d1
JG
632 struct unsuspendable_fd *entry = unsuspendable_fd_create(
633 names ? names[i] : NULL, out_fds[i]);
df038819
JG
634
635 if (!entry) {
636 ret = -1;
637 goto end_free_entries;
638 }
639 entries[i] = entry;
640 }
641
642 rcu_read_lock();
643 for (i = 0; i < fd_count; i++) {
644 struct cds_lfht_node *node;
645 struct unsuspendable_fd *entry = entries[i];
646
5c1f54d1
JG
647 node = cds_lfht_add_unique(tracker->unsuspendable_fds,
648 hash_key_ulong((void *) (unsigned long)
649 out_fds[i],
df038819 650 seed.value),
5c1f54d1 651 match_fd, (void *) (unsigned long) out_fds[i],
df038819
JG
652 &entry->tracker_node);
653
654 if (node != &entry->tracker_node) {
655 ret = -EEXIST;
656 rcu_read_unlock();
657 goto end_free_entries;
658 }
659 entries[i] = NULL;
660 }
661 tracker->count.unsuspendable += fd_count;
662 rcu_read_unlock();
663 ret = user_ret;
b13a2b17 664end_unlock:
df038819 665 pthread_mutex_unlock(&tracker->lock);
b13a2b17
JG
666end:
667 free(entries);
df038819
JG
668 return ret;
669end_free_entries:
670 for (i = 0; i < fd_count; i++) {
671 unsuspendable_fd_destroy(entries[i]);
672 }
b13a2b17 673 goto end_unlock;
df038819
JG
674}
675
676int fd_tracker_close_unsuspendable_fd(struct fd_tracker *tracker,
5c1f54d1
JG
677 int *fds_in,
678 unsigned int fd_count,
679 fd_close_cb close,
df038819
JG
680 void *user_data)
681{
682 int i, ret, user_ret;
b13a2b17 683 int *fds = NULL;
df038819
JG
684
685 /*
686 * Maintain a local copy of fds_in as the user's callback may modify its
687 * contents (e.g. setting the fd(s) to -1 after close).
688 */
b13a2b17
JG
689 fds = malloc(sizeof(*fds) * fd_count);
690 if (!fds) {
691 ret = -1;
692 goto end;
693 }
df038819
JG
694 memcpy(fds, fds_in, sizeof(*fds) * fd_count);
695
696 pthread_mutex_lock(&tracker->lock);
697 rcu_read_lock();
698
699 /* Let the user close the file descriptors. */
700 user_ret = close(user_data, fds_in);
701 if (user_ret) {
702 ret = user_ret;
b13a2b17 703 goto end_unlock;
df038819
JG
704 }
705
706 /* Untrack the fds that were just closed by the user's callback. */
707 for (i = 0; i < fd_count; i++) {
708 struct cds_lfht_node *node;
709 struct cds_lfht_iter iter;
710 struct unsuspendable_fd *entry;
711
712 cds_lfht_lookup(tracker->unsuspendable_fds,
713 hash_key_ulong((void *) (unsigned long) fds[i],
714 seed.value),
5c1f54d1 715 match_fd, (void *) (unsigned long) fds[i],
df038819
JG
716 &iter);
717 node = cds_lfht_iter_get_node(&iter);
718 if (!node) {
719 /* Unknown file descriptor. */
720 WARN("Untracked file descriptor %d passed to fd_tracker_close_unsuspendable_fd()",
721 fds[i]);
722 ret = -EINVAL;
b13a2b17 723 goto end_unlock;
df038819 724 }
5c1f54d1
JG
725 entry = caa_container_of(
726 node, struct unsuspendable_fd, tracker_node);
df038819
JG
727
728 cds_lfht_del(tracker->unsuspendable_fds, node);
729 unsuspendable_fd_destroy(entry);
730 fds[i] = -1;
731 }
732
733 tracker->count.unsuspendable -= fd_count;
734 ret = 0;
b13a2b17 735end_unlock:
df038819
JG
736 rcu_read_unlock();
737 pthread_mutex_unlock(&tracker->lock);
b13a2b17
JG
738 free(fds);
739end:
df038819
JG
740 return ret;
741}
742
743/* Caller must have taken the tracker's and handle's locks. */
5c1f54d1
JG
744static void fd_tracker_track(
745 struct fd_tracker *tracker, struct fs_handle *handle)
df038819
JG
746{
747 if (handle->fd >= 0) {
748 tracker->count.suspendable.active++;
749 cds_list_add_tail(&handle->handles_list_node,
750 &tracker->active_handles);
751 } else {
752 tracker->count.suspendable.suspended++;
753 cds_list_add_tail(&handle->handles_list_node,
754 &tracker->suspended_handles);
755 }
756}
757
758/* Caller must have taken the tracker's and handle's locks. */
5c1f54d1
JG
759static void fd_tracker_untrack(
760 struct fd_tracker *tracker, struct fs_handle *handle)
df038819
JG
761{
762 if (handle->fd >= 0) {
763 tracker->count.suspendable.active--;
764 } else {
765 tracker->count.suspendable.suspended--;
766 }
767 cds_list_del(&handle->handles_list_node);
768}
769
770/* Caller must have taken the tracker's and handle's locks. */
5c1f54d1
JG
771static int fd_tracker_restore_handle(
772 struct fd_tracker *tracker, struct fs_handle *handle)
df038819
JG
773{
774 int ret;
775
776 fd_tracker_untrack(tracker, handle);
777 if (ACTIVE_COUNT(tracker) >= tracker->capacity) {
778 ret = fd_tracker_suspend_handles(tracker, 1);
779 if (ret) {
780 goto end;
781 }
782 }
783 ret = fs_handle_restore(handle);
784end:
785 fd_tracker_track(tracker, handle);
786 return ret ? ret : handle->fd;
787}
788
789int fs_handle_get_fd(struct fs_handle *handle)
790{
791 int ret;
792
793 /*
794 * TODO This should be optimized as it is a fairly hot path.
795 * The fd-tracker's lock should only be taken when a fs_handle is
796 * restored (slow path). On the fast path (fs_handle is active),
797 * the only effect on the fd_tracker is marking the handle as the
798 * most recently used. Currently, it is done by a call to the
799 * track/untrack helpers, but it should be done atomically.
800 *
801 * Note that the lock's nesting order must still be respected here.
802 * The handle's lock nests inside the tracker's lock.
803 */
804 pthread_mutex_lock(&handle->tracker->lock);
805 pthread_mutex_lock(&handle->lock);
806 assert(!handle->in_use);
807
808 handle->tracker->stats.uses++;
809 if (handle->fd >= 0) {
810 ret = handle->fd;
811 /* Mark as most recently used. */
812 fd_tracker_untrack(handle->tracker, handle);
813 fd_tracker_track(handle->tracker, handle);
814 } else {
815 handle->tracker->stats.misses++;
816 ret = fd_tracker_restore_handle(handle->tracker, handle);
817 if (ret < 0) {
818 handle->tracker->stats.errors++;
819 goto end;
820 }
821 }
822 handle->in_use = true;
823end:
824 pthread_mutex_unlock(&handle->lock);
825 pthread_mutex_unlock(&handle->tracker->lock);
826 return ret;
827}
828
829void fs_handle_put_fd(struct fs_handle *handle)
830{
831 pthread_mutex_lock(&handle->lock);
832 handle->in_use = false;
833 pthread_mutex_unlock(&handle->lock);
834}
835
9d16fc7f
JG
836int fs_handle_unlink(struct fs_handle *handle)
837{
838 int ret;
839
840 pthread_mutex_lock(&handle->tracker->lock);
841 pthread_mutex_lock(&handle->lock);
842 ret = lttng_inode_defer_unlink(handle->inode);
843 pthread_mutex_unlock(&handle->lock);
844 pthread_mutex_unlock(&handle->tracker->lock);
845 return ret;
846}
847
df038819
JG
848int fs_handle_close(struct fs_handle *handle)
849{
850 int ret = 0;
660a216c 851 const char *path = NULL;
df038819
JG
852
853 if (!handle) {
854 ret = -EINVAL;
855 goto end;
856 }
857
858 pthread_mutex_lock(&handle->tracker->lock);
859 pthread_mutex_lock(&handle->lock);
660a216c
JG
860 if (handle->inode) {
861 path = lttng_inode_get_path(handle->inode);
862 }
df038819
JG
863 fd_tracker_untrack(handle->tracker, handle);
864 if (handle->fd >= 0) {
865 /*
866 * The return value of close() is not propagated as there
867 * isn't much the user can do about it.
868 */
869 if (close(handle->fd)) {
870 PERROR("Failed to close the file descritptor (%d) of fs handle to %s, close() returned",
660a216c 871 handle->fd, path ? path : "Unknown");
df038819
JG
872 }
873 handle->fd = -1;
874 }
4c372c54
JG
875 if (handle->inode) {
876 lttng_inode_put(handle->inode);
877 }
df038819
JG
878 pthread_mutex_unlock(&handle->lock);
879 pthread_mutex_destroy(&handle->lock);
880 pthread_mutex_unlock(&handle->tracker->lock);
df038819
JG
881 free(handle);
882end:
883 return ret;
884}
This page took 0.063418 seconds and 5 git commands to generate.