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>
36 #include <sys/inotify.h>
41 #include "sd-daemon.h"
43 #include "readahead-common.h"
45 static off_t arg_file_size_max = READAHEAD_FILE_SIZE_MAX;
47 static ReadaheadShared *shared = NULL;
49 static int unpack_file(FILE *pack) {
57 if (!fgets(fn, sizeof(fn), pack))
63 if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0)
64 log_warning("open(%s) failed: %m", fn);
65 else if (file_verify(fd, fn, arg_file_size_max, &st) <= 0) {
66 close_nointr_nofail(fd);
73 if (fread(&b, sizeof(b), 1, pack) != 1 ||
74 fread(&c, sizeof(c), 1, pack) != 1) {
75 log_error("Premature end of pack file.");
84 log_error("Invalid pack file.");
89 log_debug("%s: page %u to %u", fn, b, c);
94 if (posix_fadvise(fd, b * PAGE_SIZE, (c - b) * PAGE_SIZE, POSIX_FADV_WILLNEED) < 0) {
95 log_warning("posix_fadvise() failed: %m");
100 if (!any && fd >= 0) {
101 /* if no range is encoded in the pack file this is
102 * intended to mean that the whole file shall be
105 if (posix_fadvise(fd, 0, st.st_size, POSIX_FADV_WILLNEED) < 0) {
106 log_warning("posix_fadvise() failed: %m");
113 close_nointr_nofail(fd);
118 static int replay(const char *root) {
122 char *pack_fn = NULL, c;
123 bool on_ssd, ready = false;
129 write_one_line_file("/proc/self/oom_score_adj", "1000");
131 if (asprintf(&pack_fn, "%s/.readahead", root) < 0) {
132 log_error("Out of memory");
137 if ((!(pack = fopen(pack_fn, "re")))) {
138 if (errno == -ENOENT)
139 log_debug("No pack file found.");
141 log_error("Failed to open pack file: %m");
148 if ((inotify_fd = open_inotify()) < 0) {
153 if (!(fgets(line, sizeof(line), pack))) {
154 log_error("Premature end of pack file.");
161 if (!streq(line, CANONICAL_HOST "\n")) {
162 log_debug("Pack file host type mismatch.");
166 if ((c = getc(pack)) == EOF) {
167 log_debug("Premature end of pack file.");
172 /* We do not retest SSD here, so that we can start replaying
173 * before udev is up.*/
175 log_debug("On SSD: %s", yes_no(on_ssd));
178 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0);
180 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 7);
182 if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), prio) < 0)
183 log_warning("Failed to set IDLE IO priority class: %m");
185 sd_notify(0, "STATUS=Replaying readahead data");
187 log_debug("Replaying...");
189 if (access("/dev/.systemd/readahead/noreplay", F_OK) >= 0) {
190 log_debug("Got termination request");
194 while (!feof(pack) && !ferror(pack)) {
195 uint8_t inotify_buffer[sizeof(struct inotify_event) + FILENAME_MAX];
199 if ((n = read(inotify_fd, &inotify_buffer, sizeof(inotify_buffer))) < 0) {
200 if (errno != EINTR && errno != EAGAIN) {
201 log_error("Failed to read inotify event: %m");
206 struct inotify_event *e = (struct inotify_event*) inotify_buffer;
211 if ((e->mask & IN_CREATE) && streq(e->name, "noreplay")) {
212 log_debug("Got termination request");
216 step = sizeof(struct inotify_event) + e->len;
217 assert(step <= (size_t) n);
219 e = (struct inotify_event*) ((uint8_t*) e + step);
224 if ((k = unpack_file(pack)) < 0) {
230 /* We delay the ready notification until we
231 * queued at least one read */
232 sd_notify(0, "READY=1");
239 sd_notify(0, "READY=1");
242 log_error("Failed to read pack file.");
254 close_nointr_nofail(inotify_fd);
262 static int help(void) {
264 printf("%s [OPTIONS...] [DIRECTORY]\n\n"
265 "Replay collected read-ahead data on early boot.\n\n"
266 " -h --help Show this help\n"
267 " --max-file-size=BYTES Maximum size of files to read ahead\n",
268 program_invocation_short_name);
273 static int parse_argv(int argc, char *argv[]) {
279 static const struct option options[] = {
280 { "help", no_argument, NULL, 'h' },
281 { "file-size-max", required_argument, NULL, ARG_FILE_SIZE_MAX },
290 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
298 case ARG_FILE_SIZE_MAX: {
299 unsigned long long ull;
301 if (safe_atollu(optarg, &ull) < 0 || ull <= 0) {
302 log_error("Failed to parse maximum file size %s.", optarg);
306 arg_file_size_max = (off_t) ull;
314 log_error("Unknown option code %c", c);
319 if (optind != argc &&
328 int main(int argc, char*argv[]) {
331 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
332 log_parse_environment();
335 if ((r = parse_argv(argc, argv)) <= 0)
336 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
339 log_info("Disabling readahead replay due to low memory.");
343 if (!(shared = shared_get()))
346 shared->replay = getpid();
347 __sync_synchronize();
349 if (replay(optind < argc ? argv[optind] : "/") < 0)