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