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