1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <linux/limits.h>
30 #include <sys/select.h>
32 #include <sys/types.h>
35 #include <linux/fanotify.h>
36 #include <sys/signalfd.h>
40 #include <linux/fiemap.h>
41 #include <sys/ioctl.h>
47 #include "sd-daemon.h"
49 #include "readahead-common.h"
51 #define MINCORE_VEC_SIZE (READAHEAD_FILE_SIZE_MAX/PAGE_SIZE)
53 static int btrfs_defrag(int fd) {
54 struct btrfs_ioctl_vol_args data;
59 return ioctl(fd, BTRFS_IOC_DEFRAG, &data);
62 static int pack_file(FILE *pack, const char *fn, bool on_btrfs) {
64 void *start = MAP_FAILED;
65 uint8_t vec[MINCORE_VEC_SIZE];
69 int r = 0, fd = -1, k;
74 if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0) {
75 log_warning("open(%s) failed: %m", fn);
80 if ((k = file_verify(fd, fn, &st)) <= 0) {
88 l = PAGE_ALIGN(st.st_size);
89 if ((start = mmap(NULL, l, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED) {
90 log_warning("mmap(%s) failed: %m", fn);
95 if (mincore(start, l, vec) < 0) {
96 log_warning("mincore(%s) failed: %m", fn);
104 pages = l / PAGE_SIZE;
106 for (c = 0; c < pages; c++) {
107 bool new_mapped = (vec[c] & 1);
109 if (!mapped && new_mapped)
111 else if (mapped && !new_mapped) {
112 fwrite(&b, sizeof(b), 1, pack);
113 fwrite(&c, sizeof(c), 1, pack);
115 log_debug("%s: page %u to %u", fn, b, c);
121 /* We don't write any range data if we should read the entire file */
122 if (mapped && b > 0) {
123 fwrite(&b, sizeof(b), 1, pack);
124 fwrite(&c, sizeof(c), 1, pack);
126 log_debug("%s: page %u to %u", fn, b, c);
131 fwrite(&b, sizeof(b), 1, pack);
132 fwrite(&b, sizeof(b), 1, pack);
135 if (start != MAP_FAILED)
139 close_nointr_nofail(fd);
144 static unsigned long fd_first_block(int fd) {
146 struct fiemap fiemap;
147 struct fiemap_extent extent;
151 data.fiemap.fm_length = ~0ULL;
152 data.fiemap.fm_extent_count = 1;
154 if (ioctl(fd, FS_IOC_FIEMAP, &data) < 0)
157 if (data.fiemap.fm_mapped_extents <= 0)
160 if (data.fiemap.fm_extents[0].fe_flags & FIEMAP_EXTENT_UNKNOWN)
163 return (unsigned long) data.fiemap.fm_extents[0].fe_physical;
171 static int qsort_compare(const void *a, const void *b) {
172 const struct item *i, *j;
177 if (i->block < j->block)
179 if (i->block > j->block)
182 return strcmp(i->path, j->path);
185 static int collect(const char *root) {
191 struct pollfd pollfd[_FD_MAX];
192 int fanotify_fd = -1, signal_fd = -1, r = 0;
194 Hashmap *files = NULL;
199 char *pack_fn_new = NULL, *pack_fn = NULL;
200 bool on_ssd, on_btrfs;
205 if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)) < 0)
206 log_warning("Failed to set IDLE IO priority class: %m");
208 assert_se(sigemptyset(&mask) == 0);
209 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
210 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
212 if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
213 log_error("signalfd(): %m");
218 if (!(files = hashmap_new(string_hash_func, string_compare_func))) {
219 log_error("Failed to allocate set.");
224 if ((fanotify_fd = fanotify_init(FAN_CLOEXEC, O_RDONLY|O_LARGEFILE|O_CLOEXEC|O_NOATIME)) < 0) {
225 log_error("Failed to create fanotify object: %m");
230 if (fanotify_mark(fanotify_fd, FAN_MARK_ADD|FAN_MARK_MOUNT, FAN_OPEN, AT_FDCWD, root) < 0) {
231 log_error("Failed to mark %s: %m", root);
239 pollfd[FD_FANOTIFY].fd = fanotify_fd;
240 pollfd[FD_FANOTIFY].events = POLLIN;
241 pollfd[FD_SIGNAL].fd = signal_fd;
242 pollfd[FD_SIGNAL].events = POLLIN;
246 "STATUS=Collecting readahead data");
248 log_debug("Collecting...");
252 struct fanotify_event_metadata metadata;
256 struct fanotify_event_metadata *m;
258 if (hashmap_size(files) > READAHEAD_FILES_MAX)
261 if (poll(pollfd, _FD_MAX, -1) < 0) {
266 log_error("poll(): %m");
271 if (pollfd[FD_SIGNAL].revents != 0)
274 if ((n = read(fanotify_fd, &data, sizeof(data))) < 0) {
276 if (errno == EINTR || errno == EAGAIN)
279 log_error("Failed to read event: %m");
285 while (FAN_EVENT_OK(m, n)) {
287 if (m->pid != my_pid && m->fd >= 0) {
291 snprintf(fn, sizeof(fn), "/proc/self/fd/%i", m->fd);
294 if ((k = readlink_malloc(fn, &p)) >= 0) {
296 if (hashmap_get(files, p))
302 ul = fd_first_block(m->fd);
304 if ((k = hashmap_put(files, p, ULONG_TO_PTR(ul))) < 0) {
307 log_warning("set_put() failed: %s", strerror(-k));
314 log_warning("readlink(%s) failed: %s", fn, strerror(-k));
318 close_nointr_nofail(m->fd);
320 m = FAN_EVENT_NEXT(m, n);
324 if (fanotify_fd >= 0) {
325 close_nointr_nofail(fanotify_fd);
329 log_debug("Writing Pack File...");
331 on_ssd = fs_on_ssd(root);
332 log_debug("On SSD: %s", yes_no(on_ssd));
334 on_btrfs = statfs(root, &sfs) >= 0 && sfs.f_type == BTRFS_SUPER_MAGIC;
335 log_debug("On btrfs: %s", yes_no(on_btrfs));
337 asprintf(&pack_fn, "%s/.readahead", root);
338 asprintf(&pack_fn_new, "%s/.readahead.new", root);
340 if (!pack_fn || !pack_fn_new) {
341 log_error("Out of memory");
346 if (!(pack = fopen(pack_fn_new, "we"))) {
347 log_error("Failed to open pack file: %m");
352 fputs(CANONICAL_HOST "\n", pack);
353 putc(on_ssd ? 'S' : 'R', pack);
355 if (on_ssd || on_btrfs) {
357 /* On SSD or on btrfs, just write things out in the
358 * order the files where accessed. */
360 HASHMAP_FOREACH_KEY(q, p, files, i)
361 pack_file(pack, p, on_btrfs);
363 struct item *ordered, *j;
366 /* On rotating media, order things by the block
369 log_debug("Ordering...");
371 n = hashmap_size(files);
372 if (!(ordered = new(struct item, n))) {
373 log_error("Out of memory");
379 HASHMAP_FOREACH_KEY(q, p, files, i) {
381 j->block = PTR_TO_ULONG(q);
385 assert(ordered + n == j);
387 qsort(ordered, n, sizeof(struct item), qsort_compare);
389 for (k = 0; k < n; k++)
390 pack_file(pack, ordered[k].path, on_btrfs);
395 log_debug("Finalizing...");
400 log_error("Failed to write pack file.");
405 if (rename(pack_fn_new, pack_fn) < 0) {
406 log_error("Failed to rename readahead file: %m");
417 if (fanotify_fd >= 0)
418 close_nointr_nofail(fanotify_fd);
421 close_nointr_nofail(signal_fd);
431 while ((p = hashmap_steal_first_key(files)))
439 int main(int argc, char *argv[]) {
441 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
442 log_parse_environment();
445 if (collect(argc >= 2 ? argv[1] : "/") < 0)