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