chiark / gitweb /
58d9468c43010d04d17ed9120608f907ff4ec5db
[elogind.git] / src / readahead-replay.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <fcntl.h>
25 #include <linux/limits.h>
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/select.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35
36 #include "missing.h"
37 #include "util.h"
38 #include "set.h"
39 #include "sd-daemon.h"
40 #include "ioprio.h"
41 #include "readahead-common.h"
42
43 static int unpack_file(FILE *pack) {
44         char fn[PATH_MAX];
45         int r = 0, fd = -1;
46         bool any = false;
47         struct stat st;
48
49         assert(pack);
50
51         if (!fgets(fn, sizeof(fn), pack))
52                 return 0;
53
54         char_array_0(fn);
55         truncate_nl(fn);
56
57         if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0)
58                 log_warning("open(%s) failed: %m", fn);
59         else if (file_verify(fd, fn, &st) <= 0) {
60                 close_nointr_nofail(fd);
61                 fd = -1;
62         }
63
64         for (;;) {
65                 uint32_t b, c;
66
67                 if (fread(&b, sizeof(b), 1, pack) != 1 ||
68                     fread(&c, sizeof(c), 1, pack) != 1) {
69                         log_error("Premature end of pack file.");
70                         r = -EIO;
71                         goto finish;
72                 }
73
74                 if (b == 0 && c == 0)
75                         break;
76
77                 if (c <= b) {
78                         log_error("Invalid pack file.");
79                         r = -EIO;
80                         goto finish;
81                 }
82
83                 log_debug("%s: page %u to %u", fn, b, c);
84
85                 any = true;
86
87                 if (fd >= 0)
88                         if (readahead(fd, b * PAGE_SIZE, (c - b) * PAGE_SIZE) < 0) {
89                                 log_warning("readahead() failed: %m");
90                                 goto finish;
91                         }
92         }
93
94         if (!any && fd >= 0) {
95                 /* if no range is encoded in the pack file this is
96                  * intended to mean that the whole file shall be
97                  * read */
98
99                 if (readahead(fd, 0, st.st_size) < 0) {
100                         log_warning("readahead() failed: %m");
101                         goto finish;
102                 }
103         }
104
105 finish:
106         if (fd >= 0)
107                 close_nointr_nofail(fd);
108
109         return r;
110 }
111
112 static int replay(const char *root) {
113         FILE *pack;
114         char line[LINE_MAX];
115         int r = 0;
116         char *pack_fn = NULL, c;
117         bool on_ssd;
118         int prio;
119
120         assert(root);
121
122         if (asprintf(&pack_fn, "%s/.readahead", root) < 0) {
123                 log_error("Out of memory");
124                 r = -ENOMEM;
125                 goto finish;
126         }
127
128         if ((!(pack = fopen(pack_fn, "re")))) {
129                 if (errno == -ENOENT)
130                         log_debug("No pack file found.");
131                 else {
132                         log_error("Failed to open pack file: %m");
133                         r = -errno;
134                 }
135
136                 goto finish;
137         }
138
139         if (!(fgets(line, sizeof(line), pack))) {
140                 log_error("Premature end of pack file.");
141                 r = -EIO;
142                 goto finish;
143         }
144
145         char_array_0(line);
146
147         if (!streq(line, CANONICAL_HOST "\n")) {
148                 log_debug("Pack file host type mismatch.");
149                 goto finish;
150         }
151
152         if ((c = getc(pack)) == EOF) {
153                 log_debug("Premature end of pack file.");
154                 r = -EIO;
155                 goto finish;
156         }
157
158         /* We do not retest SSD here, so that we can start replaying
159          * before udev is up.*/
160         on_ssd = c == 'S';
161         log_debug("On SSD: %s", yes_no(on_ssd));
162
163         if (on_ssd)
164                 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0);
165         else
166                 prio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_RT, 7);
167
168         if (ioprio_set(IOPRIO_WHO_PROCESS, getpid(), prio) < 0)
169                 log_warning("Failed to set IDLE IO priority class: %m");
170
171         sd_notify(0,
172                   "READY=1\n"
173                   "STATUS=Replaying readahead data");
174
175         log_debug("Replaying...");
176
177         while (!feof(pack) && !ferror(pack)) {
178                 int k;
179
180                 if ((k = unpack_file(pack)) < 0) {
181                         r = k;
182                         goto finish;
183                 }
184         }
185
186         if (ferror(pack)) {
187                 log_error("Failed to read pack file.");
188                 r = -EIO;
189                 goto finish;
190         }
191
192         log_debug("Done.");
193
194 finish:
195         if (pack)
196                 fclose(pack);
197
198         free(pack_fn);
199
200         return r;
201 }
202
203 int main(int argc, char*argv[]) {
204
205         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
206         log_parse_environment();
207         log_open();
208
209         if (!enough_ram()) {
210                 log_info("Disabling readahead replay due to low memory.");
211                 return 0;
212         }
213
214         if (replay(argc >= 2 ? argv[1] : "/") < 0)
215                 return 1;
216
217         return 0;
218 }