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