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/>.
26 #include <sys/sysinfo.h>
27 #include <sys/inotify.h>
30 #include "readahead-common.h"
33 int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
38 if (fstat(fd, st) < 0) {
39 log_warning("fstat(%s) failed: %m", fn);
43 if (!S_ISREG(st->st_mode)) {
44 log_debug("Not preloading special file %s", fn);
48 if (st->st_size <= 0 || st->st_size > file_size_max) {
49 log_debug("Not preloading file %s with size out of bounds %zi", fn, st->st_size);
56 int fs_on_ssd(const char *p) {
58 struct udev *udev = NULL;
59 struct udev_device *udev_device = NULL, *look_at = NULL;
61 const char *devtype, *rotational, *model, *id;
68 if (!(udev = udev_new()))
71 if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev)))
74 if ((devtype = udev_device_get_property_value(udev_device, "DEVTYPE")) &&
75 streq(devtype, "partition"))
76 look_at = udev_device_get_parent(udev_device);
78 look_at = udev_device;
83 /* First, try high-level property */
84 if ((id = udev_device_get_property_value(look_at, "ID_SSD"))) {
89 /* Second, try kernel attribute */
90 if ((rotational = udev_device_get_sysattr_value(look_at, "queue/rotational")))
91 if ((b = streq(rotational, "0")))
94 /* Finally, fallback to heuristics */
95 if (!(look_at = udev_device_get_parent(look_at)))
98 if ((model = udev_device_get_sysattr_value(look_at, "model")))
99 b = !!strstr(model, "SSD");
103 udev_device_unref(udev_device);
111 bool enough_ram(void) {
114 assert_se(sysinfo(&si) >= 0);
116 return si.totalram > 127 * 1024*1024; /* Enable readahead only
117 * with at least 128MB
121 int open_inotify(void) {
124 if ((fd = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
125 log_error("Failed to create inotify handle: %m");
129 mkdir("/dev/.systemd", 0755);
130 mkdir("/dev/.systemd/readahead", 0755);
132 if (inotify_add_watch(fd, "/dev/.systemd/readahead", IN_CREATE) < 0) {
133 log_error("Failed to watch /dev/.systemd/readahead: %m");
134 close_nointr_nofail(fd);