chiark / gitweb /
readahead: implement minimal readahead logic based on fanotify(), mincore() and reada...
[elogind.git] / src / readahead-common.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 <libudev.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "log.h"
28 #include "readahead-common.h"
29 #include "util.h"
30
31 int file_verify(int fd, const char *fn, struct stat *st) {
32         assert(fd >= 0);
33         assert(fn);
34         assert(st);
35
36         if (fstat(fd, st) < 0) {
37                 log_warning("fstat(%s) failed: %m", fn);
38                 return -errno;
39         }
40
41         if (!S_ISREG(st->st_mode)) {
42                 log_debug("Not preloading special file %s", fn);
43                 return 0;
44         }
45
46         if (st->st_size <= 0 || st->st_size > READAHEAD_FILE_SIZE_MAX) {
47                 log_debug("Not preloading file %s with size out of bounds %zi", fn, st->st_size);
48                 return 0;
49         }
50
51         return 1;
52 }
53
54 int fs_on_ssd(const char *p) {
55         struct stat st;
56         struct udev *udev = NULL;
57         struct udev_device *udev_device = NULL, *look_at = NULL;
58         bool b = false;
59         const char *devtype, *rotational, *model, *id;
60
61         assert(p);
62
63         if (stat(p, &st) < 0)
64                 return -errno;
65
66         if (!(udev = udev_new()))
67                 return -ENOMEM;
68
69         if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev)))
70                 goto finish;
71
72         if ((devtype = udev_device_get_property_value(udev_device, "DEVTYPE")) &&
73             streq(devtype, "partition"))
74                 look_at = udev_device_get_parent(udev_device);
75         else
76                 look_at = udev_device;
77
78         if (!look_at)
79                 goto finish;
80
81         /* First, try high-level property */
82         if ((id = udev_device_get_property_value(look_at, "ID_SSD"))) {
83                 b = streq(id, "1");
84                 goto finish;
85         }
86
87         /* Second, try kernel attribute */
88         if ((rotational = udev_device_get_sysattr_value(look_at, "queue/rotational")))
89                 if ((b = streq(rotational, "0")))
90                         goto finish;
91
92         /* Finally, fallback to heuristics */
93         if (!(look_at = udev_device_get_parent(look_at)))
94                 goto finish;
95
96         if ((model = udev_device_get_sysattr_value(look_at, "model")))
97                 b = !!strstr(model, "SSD");
98
99 finish:
100         if (udev_device)
101                 udev_device_unref(udev_device);
102
103         if (udev)
104                 udev_unref(udev);
105
106         return b;
107 }