chiark / gitweb /
readahead: store inode numbers in pack file
[elogind.git] / src / readahead / readahead-replay.c
index 7c263f6e5382e4053795278bebc762996ee055e8..886e16f296ded4c7dc95289b19f9b9331dfbd925 100644 (file)
@@ -53,6 +53,7 @@ static int unpack_file(FILE *pack) {
         int r = 0, fd = -1;
         bool any = false;
         struct stat st;
+        uint64_t inode;
 
         assert(pack);
 
@@ -62,7 +63,8 @@ static int unpack_file(FILE *pack) {
         char_array_0(fn);
         truncate_nl(fn);
 
-        if ((fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW)) < 0) {
+        fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOATIME|O_NOCTTY|O_NOFOLLOW);
+        if (fd < 0) {
 
                 if (errno != ENOENT && errno != EPERM && errno != EACCES)
                         log_warning("open(%s) failed: %m", fn);
@@ -72,6 +74,21 @@ static int unpack_file(FILE *pack) {
                 fd = -1;
         }
 
+        if (fread(&inode, sizeof(inode), 1, pack) != 1) {
+                log_error("Premature end of pack file.");
+                r = -EIO;
+                goto finish;
+        }
+
+        if (fd >= 0) {
+                /* If the inode changed the file got deleted, so just
+                 * ignore this entry */
+                if (st.st_ino != (uint64_t) inode) {
+                        close_nointr_nofail(fd);
+                        fd = -1;
+                }
+        }
+
         for (;;) {
                 uint32_t b, c;
 
@@ -132,8 +149,7 @@ static int replay(const char *root) {
 
         assert(root);
 
-        write_one_line_file("/proc/self/oom_score_adj", "1000");
-        bump_request_nr(root);
+        block_bump_request_nr(root);
 
         if (asprintf(&pack_fn, "%s/.readahead", root) < 0) {
                 log_error("Out of memory");
@@ -167,8 +183,8 @@ static int replay(const char *root) {
 
         char_array_0(line);
 
-        if (!streq(line, CANONICAL_HOST "\n")) {
-                log_debug("Pack file host type mismatch.");
+        if (!streq(line, CANONICAL_HOST ";VERSION=2\n")) {
+                log_debug("Pack file host or version type mismatch.");
                 goto finish;
         }
 
@@ -350,7 +366,8 @@ int main(int argc, char*argv[]) {
 
         umask(0022);
 
-        if ((r = parse_argv(argc, argv)) <= 0)
+        r = parse_argv(argc, argv);
+        if (r <= 0)
                 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 
         root = optind < argc ? argv[optind] : "/";
@@ -360,12 +377,8 @@ int main(int argc, char*argv[]) {
                 return 0;
         }
 
-        if (detect_virtualization(NULL) > 0) {
-                log_info("Disabling readahead replay due to execution in virtualized environment.");
-                return 0;
-        }
-
-        if (!(shared = shared_get()))
+        shared = shared_get();
+        if (!shared)
                 return 1;
 
         shared->replay = getpid();