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