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>
40 #include "sd-daemon.h"
42 #include "readahead-common.h"
44 static off_t arg_file_size_max = READAHEAD_FILE_SIZE_MAX;
46 static int unpack_file(FILE *pack) {
54 if (!fgets(fn, sizeof(fn), pack))
60 if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0)
61 log_warning("open(%s) failed: %m", fn);
62 else if (file_verify(fd, fn, arg_file_size_max, &st) <= 0) {
63 close_nointr_nofail(fd);
70 if (fread(&b, sizeof(b), 1, pack) != 1 ||
71 fread(&c, sizeof(c), 1, pack) != 1) {
72 log_error("Premature end of pack file.");
81 log_error("Invalid pack file.");
86 log_debug("%s: page %u to %u", fn, b, c);
91 if (readahead(fd, b * PAGE_SIZE, (c - b) * PAGE_SIZE) < 0) {
92 log_warning("readahead() failed: %m");
97 if (!any && fd >= 0) {
98 /* if no range is encoded in the pack file this is
99 * intended to mean that the whole file shall be
102 if (readahead(fd, 0, st.st_size) < 0) {
103 log_warning("readahead() failed: %m");
110 close_nointr_nofail(fd);
115 static int replay(const char *root) {
119 char *pack_fn = NULL, c;
120 bool on_ssd, ready = false;
125 write_one_line_file("/proc/self/oom_score_adj", "1000");
127 if (asprintf(&pack_fn, "%s/.readahead", root) < 0) {
128 log_error("Out of memory");
133 if ((!(pack = fopen(pack_fn, "re")))) {
134 if (errno == -ENOENT)
135 log_debug("No pack file found.");
137 log_error("Failed to open pack file: %m");
144 if (!(fgets(line, sizeof(line), pack))) {
145 log_error("Premature end of pack file.");
152 if (!streq(line, CANONICAL_HOST "\n")) {
153 log_debug("Pack file host type mismatch.");
157 if ((c = getc(pack)) == EOF) {
158 log_debug("Premature end of pack file.");
163 /* We do not retest SSD here, so that we can start replaying
164 * before udev is up.*/
166 log_debug("On SSD: %s", yes_no(on_ssd));
169 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0);
171 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 7);
173 if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), prio) < 0)
174 log_warning("Failed to set IDLE IO priority class: %m");
176 sd_notify(0, "STATUS=Replaying readahead data");
178 log_debug("Replaying...");
180 while (!feof(pack) && !ferror(pack)) {
183 if ((k = unpack_file(pack)) < 0) {
189 /* We delay the ready notification until we
190 * queued at least one read */
191 sd_notify(0, "READY=1");
197 sd_notify(0, "READY=1");
200 log_error("Failed to read pack file.");
217 static int help(void) {
219 printf("%s [OPTIONS...] [DIRECTORY]\n\n"
220 "Replay collected read-ahead data on early boot.\n\n"
221 " -h --help Show this help\n"
222 " --max-file-size=BYTES Maximum size of files to read ahead\n",
223 program_invocation_short_name);
228 static int parse_argv(int argc, char *argv[]) {
234 static const struct option options[] = {
235 { "help", no_argument, NULL, 'h' },
236 { "file-size-max", required_argument, NULL, ARG_FILE_SIZE_MAX },
245 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
253 case ARG_FILE_SIZE_MAX: {
254 unsigned long long ull;
256 if (safe_atollu(optarg, &ull) < 0 || ull <= 0) {
257 log_error("Failed to parse maximum file size %s.", optarg);
261 arg_file_size_max = (off_t) ull;
269 log_error("Unknown option code %c", c);
274 if (optind != argc &&
283 int main(int argc, char*argv[]) {
286 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
287 log_parse_environment();
290 if ((r = parse_argv(argc, argv)) <= 0)
291 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
294 log_info("Disabling readahead replay due to low memory.");
298 if (replay(optind < argc ? argv[optind] : "/") < 0)