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