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