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