]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
19adb8a3 | 2 | /*** |
96b2fb93 | 3 | Copyright © 2018 Dell Inc. |
19adb8a3 ZJS |
4 | ***/ |
5 | ||
a8fbdf54 | 6 | #include <errno.h> |
ca78ad1d | 7 | #include <fcntl.h> |
17c40b3a | 8 | #include <linux/fs.h> |
a8fbdf54 TA |
9 | #include <stdbool.h> |
10 | #include <stddef.h> | |
36dd5ffd | 11 | #include <sys/ioctl.h> |
ca78ad1d ZJS |
12 | #include <sys/stat.h> |
13 | #include <sys/types.h> | |
a8fbdf54 TA |
14 | #include <syslog.h> |
15 | #include <unistd.h> | |
19adb8a3 | 16 | |
b5efdb8a | 17 | #include "alloc-util.h" |
7bdf56a2 | 18 | #include "btrfs-util.h" |
19adb8a3 | 19 | #include "conf-parser.h" |
a0f29c76 | 20 | #include "def.h" |
490d20e6 | 21 | #include "env-util.h" |
427646ea | 22 | #include "errno-util.h" |
3ffd4af2 | 23 | #include "fd-util.h" |
19adb8a3 ZJS |
24 | #include "fileio.h" |
25 | #include "log.h" | |
a8fbdf54 | 26 | #include "macro.h" |
a0f29c76 | 27 | #include "parse-util.h" |
411ae92b | 28 | #include "path-util.h" |
3ffd4af2 | 29 | #include "sleep-config.h" |
7bdf56a2 | 30 | #include "stdio-util.h" |
07630cea | 31 | #include "string-util.h" |
19adb8a3 | 32 | #include "strv.h" |
1bbbefe7 | 33 | #include "time-util.h" |
19adb8a3 | 34 | |
28ca9c24 ZS |
35 | int parse_sleep_config(SleepConfig **ret_sleep_config) { |
36 | _cleanup_(free_sleep_configp) SleepConfig *sc; | |
e8f1d00d ZJS |
37 | int allow_suspend = -1, allow_hibernate = -1, |
38 | allow_s2h = -1, allow_hybrid_sleep = -1; | |
28ca9c24 ZS |
39 | |
40 | sc = new0(SleepConfig, 1); | |
41 | if (!sc) | |
42 | return log_oom(); | |
19adb8a3 ZJS |
43 | |
44 | const ConfigTableItem items[] = { | |
e8f1d00d ZJS |
45 | { "Sleep", "AllowSuspend", config_parse_tristate, 0, &allow_suspend }, |
46 | { "Sleep", "AllowHibernation", config_parse_tristate, 0, &allow_hibernate }, | |
47 | { "Sleep", "AllowSuspendThenHibernate", config_parse_tristate, 0, &allow_s2h }, | |
48 | { "Sleep", "AllowHybridSleep", config_parse_tristate, 0, &allow_hybrid_sleep }, | |
49 | ||
28ca9c24 ZS |
50 | { "Sleep", "SuspendMode", config_parse_strv, 0, &sc->suspend_modes }, |
51 | { "Sleep", "SuspendState", config_parse_strv, 0, &sc->suspend_states }, | |
52 | { "Sleep", "HibernateMode", config_parse_strv, 0, &sc->hibernate_modes }, | |
53 | { "Sleep", "HibernateState", config_parse_strv, 0, &sc->hibernate_states }, | |
54 | { "Sleep", "HybridSleepMode", config_parse_strv, 0, &sc->hybrid_modes }, | |
55 | { "Sleep", "HybridSleepState", config_parse_strv, 0, &sc->hybrid_states }, | |
e8f1d00d | 56 | |
28ca9c24 | 57 | { "Sleep", "HibernateDelaySec", config_parse_sec, 0, &sc->hibernate_delay_sec}, |
34c10968 LP |
58 | {} |
59 | }; | |
19adb8a3 | 60 | |
bcde742e LP |
61 | (void) config_parse_many_nulstr(PKGSYSCONFDIR "/sleep.conf", |
62 | CONF_PATHS_NULSTR("systemd/sleep.conf.d"), | |
63 | "Sleep\0", config_item_table_lookup, items, | |
64 | CONFIG_PARSE_WARN, NULL); | |
19adb8a3 | 65 | |
28ca9c24 ZS |
66 | /* use default values unless set */ |
67 | sc->allow_suspend = allow_suspend != 0; | |
68 | sc->allow_hibernate = allow_hibernate != 0; | |
7874583d ZS |
69 | sc->allow_hybrid_sleep = allow_hybrid_sleep >= 0 ? allow_hybrid_sleep |
70 | : (allow_suspend != 0 && allow_hibernate != 0); | |
71 | sc->allow_s2h = allow_s2h >= 0 ? allow_s2h | |
72 | : (allow_suspend != 0 && allow_hibernate != 0); | |
28ca9c24 ZS |
73 | |
74 | if (!sc->suspend_states) | |
75 | sc->suspend_states = strv_new("mem", "standby", "freeze"); | |
76 | if (!sc->hibernate_modes) | |
77 | sc->hibernate_modes = strv_new("platform", "shutdown"); | |
78 | if (!sc->hibernate_states) | |
79 | sc->hibernate_states = strv_new("disk"); | |
80 | if (!sc->hybrid_modes) | |
81 | sc->hybrid_modes = strv_new("suspend", "platform", "shutdown"); | |
82 | if (!sc->hybrid_states) | |
83 | sc->hybrid_states = strv_new("disk"); | |
84 | if (sc->hibernate_delay_sec == 0) | |
a077755a | 85 | sc->hibernate_delay_sec = 2 * USEC_PER_HOUR; |
28ca9c24 ZS |
86 | |
87 | /* ensure values set for all required fields */ | |
88 | if (!sc->suspend_states || !sc->hibernate_modes | |
89 | || !sc->hibernate_states || !sc->hybrid_modes || !sc->hybrid_states) | |
19adb8a3 | 90 | return log_oom(); |
19adb8a3 | 91 | |
28ca9c24 | 92 | *ret_sleep_config = TAKE_PTR(sc); |
c58493c0 | 93 | |
19adb8a3 ZJS |
94 | return 0; |
95 | } | |
96 | ||
97 | int can_sleep_state(char **types) { | |
a2a5291b | 98 | char **type; |
19adb8a3 ZJS |
99 | int r; |
100 | _cleanup_free_ char *p = NULL; | |
101 | ||
102 | if (strv_isempty(types)) | |
103 | return true; | |
104 | ||
105 | /* If /sys is read-only we cannot sleep */ | |
106 | if (access("/sys/power/state", W_OK) < 0) | |
107 | return false; | |
108 | ||
109 | r = read_one_line_file("/sys/power/state", &p); | |
110 | if (r < 0) | |
111 | return false; | |
112 | ||
113 | STRV_FOREACH(type, types) { | |
a2a5291b | 114 | const char *word, *state; |
19adb8a3 ZJS |
115 | size_t l, k; |
116 | ||
117 | k = strlen(*type); | |
a2a5291b ZJS |
118 | FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) |
119 | if (l == k && memcmp(word, *type, l) == 0) | |
19adb8a3 ZJS |
120 | return true; |
121 | } | |
122 | ||
123 | return false; | |
124 | } | |
125 | ||
126 | int can_sleep_disk(char **types) { | |
a2a5291b | 127 | char **type; |
19adb8a3 ZJS |
128 | int r; |
129 | _cleanup_free_ char *p = NULL; | |
130 | ||
131 | if (strv_isempty(types)) | |
132 | return true; | |
133 | ||
134 | /* If /sys is read-only we cannot sleep */ | |
7474f15b LP |
135 | if (access("/sys/power/disk", W_OK) < 0) { |
136 | log_debug_errno(errno, "/sys/power/disk is not writable: %m"); | |
19adb8a3 | 137 | return false; |
7474f15b | 138 | } |
19adb8a3 ZJS |
139 | |
140 | r = read_one_line_file("/sys/power/disk", &p); | |
7474f15b LP |
141 | if (r < 0) { |
142 | log_debug_errno(r, "Couldn't read /sys/power/disk: %m"); | |
19adb8a3 | 143 | return false; |
7474f15b | 144 | } |
19adb8a3 ZJS |
145 | |
146 | STRV_FOREACH(type, types) { | |
a2a5291b | 147 | const char *word, *state; |
19adb8a3 ZJS |
148 | size_t l, k; |
149 | ||
150 | k = strlen(*type); | |
a2a5291b ZJS |
151 | FOREACH_WORD_SEPARATOR(word, l, p, WHITESPACE, state) { |
152 | if (l == k && memcmp(word, *type, l) == 0) | |
19adb8a3 ZJS |
153 | return true; |
154 | ||
a2a5291b ZJS |
155 | if (l == k + 2 && |
156 | word[0] == '[' && | |
157 | memcmp(word + 1, *type, l - 2) == 0 && | |
158 | word[l-1] == ']') | |
19adb8a3 ZJS |
159 | return true; |
160 | } | |
161 | } | |
162 | ||
163 | return false; | |
164 | } | |
165 | ||
69ab8088 ZJS |
166 | #define HIBERNATION_SWAP_THRESHOLD 0.98 |
167 | ||
7bdf56a2 | 168 | SwapEntry* swap_entry_free(SwapEntry *se) { |
88bc86fc ZS |
169 | if (!se) |
170 | return NULL; | |
171 | ||
172 | free(se->device); | |
173 | free(se->type); | |
174 | ||
175 | return mfree(se); | |
176 | } | |
177 | ||
7bdf56a2 ZS |
178 | HibernateLocation* hibernate_location_free(HibernateLocation *hl) { |
179 | if (!hl) | |
180 | return NULL; | |
181 | ||
182 | swap_entry_free(hl->swap); | |
183 | free(hl->resume); | |
184 | ||
185 | return mfree(hl); | |
186 | } | |
187 | ||
188 | static int swap_device_to_major_minor(const SwapEntry *swap, char **ret) { | |
189 | _cleanup_free_ char *major_minor = NULL; | |
190 | _cleanup_close_ int fd = -1; | |
191 | struct stat sb; | |
192 | dev_t swap_dev; | |
193 | int r; | |
194 | ||
195 | assert(swap); | |
196 | assert(swap->device); | |
197 | assert(swap->type); | |
198 | ||
199 | fd = open(swap->device, O_RDONLY | O_CLOEXEC | O_NONBLOCK); | |
200 | if (fd < 0) | |
201 | return log_debug_errno(errno, "Unable to open '%s': %m", swap->device); | |
202 | ||
203 | r = fstat(fd, &sb); | |
204 | if (r < 0) | |
205 | return log_debug_errno(errno, "Unable to stat %s: %m", swap->device); | |
206 | ||
207 | swap_dev = streq(swap->type, "partition") ? sb.st_rdev : sb.st_dev; | |
208 | if (asprintf(&major_minor, "%u:%u", major(swap_dev), minor(swap_dev)) < 0) | |
209 | return log_oom(); | |
210 | ||
211 | *ret = TAKE_PTR(major_minor); | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
216 | static int calculate_swap_file_offset(const SwapEntry *swap, uint64_t *ret_offset) { | |
217 | _cleanup_close_ int fd = -1; | |
218 | _cleanup_free_ struct fiemap *fiemap = NULL; | |
219 | struct stat sb; | |
220 | int r, btrfs; | |
221 | ||
222 | assert(swap); | |
223 | assert(swap->device); | |
224 | assert(streq(swap->type, "file")); | |
225 | ||
226 | fd = open(swap->device, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
e97c3691 | 227 | if (fd < 0) |
7bdf56a2 ZS |
228 | return log_error_errno(errno, "Failed to open %s: %m", swap->device); |
229 | ||
e97c3691 | 230 | if (fstat(fd, &sb) < 0) |
7bdf56a2 ZS |
231 | return log_error_errno(errno, "Failed to stat %s: %m", swap->device); |
232 | ||
233 | btrfs = btrfs_is_filesystem(fd); | |
234 | if (btrfs < 0) | |
b72571e0 | 235 | return log_error_errno(btrfs, "Error checking %s for Btrfs filesystem: %m", swap->device); |
7bdf56a2 ZS |
236 | else if (btrfs > 0) { |
237 | log_debug("Detection of swap file offset on Btrfs is not supported: %s; skipping", swap->device); | |
238 | *ret_offset = 0; | |
239 | return 0; | |
240 | } | |
241 | ||
242 | r = read_fiemap(fd, &fiemap); | |
243 | if (r < 0) | |
244 | return log_debug_errno(r, "Unable to read extent map for '%s': %m", swap->device); | |
245 | ||
246 | *ret_offset = fiemap->fm_extents[0].fe_physical / page_size(); | |
247 | ||
248 | return 0; | |
249 | } | |
88bc86fc | 250 | |
7bdf56a2 ZS |
251 | static int read_resume_files(char **ret_resume, uint64_t *ret_resume_offset) { |
252 | _cleanup_free_ char *resume, *resume_offset_str = NULL; | |
253 | uint64_t resume_offset = 0; | |
254 | int r; | |
255 | ||
256 | r = read_one_line_file("/sys/power/resume", &resume); | |
257 | if (r < 0) | |
258 | return log_debug_errno(r, "Error reading from /sys/power/resume: %m"); | |
259 | ||
260 | r = read_one_line_file("/sys/power/resume_offset", &resume_offset_str); | |
b72571e0 ZJS |
261 | if (r == -ENOENT) |
262 | log_debug("Kernel does not support resume_offset; swap file offset detection will be skipped."); | |
263 | else if (r < 0) | |
264 | return log_debug_errno(r, "Error reading from /sys/power/resume_offset: %m"); | |
265 | else { | |
7bdf56a2 ZS |
266 | r = safe_atou64(resume_offset_str, &resume_offset); |
267 | if (r < 0) | |
b72571e0 | 268 | return log_error_errno(r, "Failed to parse value in /sys/power/resume_offset \"%s\": %m", resume_offset_str); |
7bdf56a2 ZS |
269 | } |
270 | ||
271 | if (resume_offset > 0 && streq(*ret_resume, "0:0")) { | |
272 | log_debug("Found offset in /sys/power/resume_offset: %" PRIu64 "; no device id found in /sys/power/resume; ignoring resume_offset", resume_offset); | |
273 | resume_offset = 0; | |
274 | } | |
275 | ||
276 | *ret_resume = TAKE_PTR(resume); | |
277 | *ret_resume_offset = resume_offset; | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
282 | static bool location_is_resume_device(const HibernateLocation *location, const char *sys_resume, const uint64_t sys_offset) { | |
283 | assert(location); | |
284 | assert(location->resume); | |
285 | assert(sys_resume); | |
286 | ||
287 | return streq(sys_resume, location->resume) && sys_offset == location->resume_offset; | |
288 | } | |
289 | ||
290 | /* | |
291 | * Attempt to find the hibernation location by parsing /proc/swaps, /sys/power/resume, and | |
292 | * /sys/power/resume_offset. | |
293 | * | |
294 | * Returns: | |
295 | * 1 - HibernateLocation matches values found in /sys/power/resume & /sys/power/resume_offset | |
296 | * 0 - HibernateLocation is highest priority swap with most remaining space; no valid values exist in /sys/power/resume & /sys/power/resume_offset | |
297 | * negative value in the case of error | |
298 | */ | |
299 | int find_hibernate_location(HibernateLocation **ret_hibernate_location) { | |
b72571e0 | 300 | _cleanup_fclose_ FILE *f = NULL; |
7bdf56a2 ZS |
301 | _cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL; |
302 | _cleanup_free_ char *sys_resume = NULL; | |
303 | uint64_t sys_offset = 0; | |
1fa2f38f | 304 | unsigned i; |
7bdf56a2 ZS |
305 | int r; |
306 | ||
307 | /* read the /sys/power/resume & /sys/power/resume_offset values */ | |
308 | r = read_resume_files(&sys_resume, &sys_offset); | |
309 | if (r < 0) | |
310 | return r; | |
9fb3675e | 311 | |
c8a202b7 | 312 | f = fopen("/proc/swaps", "re"); |
9fb3675e ZJS |
313 | if (!f) { |
314 | log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, | |
ea470720 | 315 | "Failed to open /proc/swaps: %m"); |
427646ea | 316 | return negative_errno(); |
9fb3675e | 317 | } |
69ab8088 | 318 | |
9fb3675e | 319 | (void) fscanf(f, "%*s %*s %*s %*s %*s\n"); |
9fb3675e | 320 | for (i = 1;; i++) { |
88bc86fc | 321 | _cleanup_(swap_entry_freep) SwapEntry *swap = NULL; |
7bdf56a2 | 322 | uint64_t swap_offset = 0; |
9fb3675e ZJS |
323 | int k; |
324 | ||
88bc86fc ZS |
325 | swap = new0(SwapEntry, 1); |
326 | if (!swap) | |
327 | return log_oom(); | |
328 | ||
9fb3675e | 329 | k = fscanf(f, |
88bc86fc ZS |
330 | "%ms " /* device/file */ |
331 | "%ms " /* type of swap */ | |
332 | "%" PRIu64 /* swap size */ | |
333 | "%" PRIu64 /* used */ | |
334 | "%i\n", /* priority */ | |
335 | &swap->device, &swap->type, &swap->size, &swap->used, &swap->priority); | |
3dea6886 LP |
336 | if (k == EOF) |
337 | break; | |
88bc86fc | 338 | if (k != 5) { |
9fb3675e ZJS |
339 | log_warning("Failed to parse /proc/swaps:%u", i); |
340 | continue; | |
341 | } | |
342 | ||
88bc86fc | 343 | if (streq(swap->type, "file")) { |
88bc86fc ZS |
344 | if (endswith(swap->device, "\\040(deleted)")) { |
345 | log_warning("Ignoring deleted swap file '%s'.", swap->device); | |
411ae92b AJ |
346 | continue; |
347 | } | |
348 | ||
7bdf56a2 ZS |
349 | r = calculate_swap_file_offset(swap, &swap_offset); |
350 | if (r < 0) | |
351 | return r; | |
88bc86fc | 352 | } else if (streq(swap->type, "partition")) { |
411ae92b | 353 | const char *fn; |
3dea6886 | 354 | |
88bc86fc | 355 | fn = path_startswith(swap->device, "/dev/"); |
411ae92b | 356 | if (fn && startswith(fn, "zram")) { |
88bc86fc | 357 | log_debug("Ignoring compressed RAM swap device '%s'.", swap->device); |
411ae92b AJ |
358 | continue; |
359 | } | |
7bdf56a2 ZS |
360 | } else { |
361 | log_debug("Swap type %s is unsupported for hibernation: %s; skipping", swap->type, swap->device); | |
362 | continue; | |
9fb3675e | 363 | } |
3dea6886 | 364 | |
7bdf56a2 ZS |
365 | /* prefer resume device or highest priority swap with most remaining space */ |
366 | if (!hibernate_location || swap->priority > hibernate_location->swap->priority | |
367 | || ((swap->priority == hibernate_location->swap->priority) | |
368 | && (swap->size - swap->used) > (hibernate_location->swap->size - hibernate_location->swap->used))) { | |
369 | ||
370 | _cleanup_free_ char *swap_device_id = NULL; | |
371 | r = swap_device_to_major_minor(swap, &swap_device_id); | |
372 | if (r < 0) | |
373 | return r; | |
374 | ||
375 | hibernate_location = hibernate_location_free(hibernate_location); | |
b72571e0 | 376 | hibernate_location = new(HibernateLocation, 1); |
7bdf56a2 ZS |
377 | if (!hibernate_location) |
378 | return log_oom(); | |
379 | ||
b72571e0 ZJS |
380 | *hibernate_location = (HibernateLocation) { |
381 | .resume = TAKE_PTR(swap_device_id), | |
382 | .resume_offset = swap_offset, | |
383 | .swap = TAKE_PTR(swap), | |
384 | }; | |
7bdf56a2 ZS |
385 | |
386 | /* if the swap is the resume device, stop looping swaps */ | |
387 | if (location_is_resume_device(hibernate_location, sys_resume, sys_offset)) | |
388 | break; | |
88bc86fc | 389 | } |
69ab8088 ZJS |
390 | } |
391 | ||
7bdf56a2 ZS |
392 | if (!hibernate_location) |
393 | return log_debug_errno(SYNTHETIC_ERRNO(ENOSYS), "No swap partitions or files were found"); | |
394 | ||
395 | if (!streq(sys_resume, "0:0") && !location_is_resume_device(hibernate_location, sys_resume, sys_offset)) | |
396 | return log_warning_errno(SYNTHETIC_ERRNO(ENOSYS), "/sys/power/resume and /sys/power/resume_offset has no matching entry in /proc/swaps; Hibernation will fail: resume=%s, resume_offset=%" PRIu64, | |
397 | sys_resume, sys_offset); | |
398 | ||
399 | log_debug("Hibernation will attempt to use swap entry with path: %s, device: %s, offset: %" PRIu64 ", priority: %i", | |
400 | hibernate_location->swap->device, hibernate_location->resume, hibernate_location->resume_offset, hibernate_location->swap->priority); | |
88bc86fc | 401 | |
7bdf56a2 | 402 | *ret_hibernate_location = TAKE_PTR(hibernate_location); |
88bc86fc | 403 | |
7bdf56a2 ZS |
404 | if (location_is_resume_device(*ret_hibernate_location, sys_resume, sys_offset)) |
405 | return 1; | |
88bc86fc ZS |
406 | |
407 | return 0; | |
9fb3675e ZJS |
408 | } |
409 | ||
4638cd39 | 410 | static bool enough_swap_for_hibernation(void) { |
9fb3675e | 411 | _cleanup_free_ char *active = NULL; |
7bdf56a2 | 412 | _cleanup_(hibernate_location_freep) HibernateLocation *hibernate_location = NULL; |
39883f62 | 413 | unsigned long long act = 0; |
9fb3675e ZJS |
414 | int r; |
415 | ||
490d20e6 VV |
416 | if (getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0) |
417 | return true; | |
418 | ||
7bdf56a2 | 419 | r = find_hibernate_location(&hibernate_location); |
9fb3675e | 420 | if (r < 0) |
69ab8088 | 421 | return false; |
69ab8088 | 422 | |
c4cd1d4d | 423 | r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active); |
69ab8088 | 424 | if (r < 0) { |
904865b8 | 425 | log_debug_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m"); |
69ab8088 ZJS |
426 | return false; |
427 | } | |
428 | ||
429 | r = safe_atollu(active, &act); | |
430 | if (r < 0) { | |
904865b8 | 431 | log_debug_errno(r, "Failed to parse Active(anon) from /proc/meminfo: %s: %m", active); |
69ab8088 ZJS |
432 | return false; |
433 | } | |
434 | ||
7bdf56a2 | 435 | r = act <= (hibernate_location->swap->size - hibernate_location->swap->used) * HIBERNATION_SWAP_THRESHOLD; |
88bc86fc | 436 | log_debug("%s swap for hibernation, Active(anon)=%llu kB, size=%" PRIu64 " kB, used=%" PRIu64 " kB, threshold=%.2g%%", |
7bdf56a2 | 437 | r ? "Enough" : "Not enough", act, hibernate_location->swap->size, hibernate_location->swap->used, 100*HIBERNATION_SWAP_THRESHOLD); |
69ab8088 ZJS |
438 | |
439 | return r; | |
440 | } | |
441 | ||
17c40b3a ML |
442 | int read_fiemap(int fd, struct fiemap **ret) { |
443 | _cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL; | |
17c40b3a ML |
444 | struct stat statinfo; |
445 | uint32_t result_extents = 0; | |
446 | uint64_t fiemap_start = 0, fiemap_length; | |
6524f1a8 ZJS |
447 | const size_t n_extra = DIV_ROUND_UP(sizeof(struct fiemap), sizeof(struct fiemap_extent)); |
448 | size_t fiemap_allocated = n_extra, result_fiemap_allocated = n_extra; | |
17c40b3a ML |
449 | |
450 | if (fstat(fd, &statinfo) < 0) | |
451 | return log_debug_errno(errno, "Cannot determine file size: %m"); | |
452 | if (!S_ISREG(statinfo.st_mode)) | |
453 | return -ENOTTY; | |
454 | fiemap_length = statinfo.st_size; | |
455 | ||
6524f1a8 ZJS |
456 | /* Zero this out in case we run on a file with no extents */ |
457 | fiemap = calloc(n_extra, sizeof(struct fiemap_extent)); | |
17c40b3a ML |
458 | if (!fiemap) |
459 | return -ENOMEM; | |
460 | ||
6524f1a8 | 461 | result_fiemap = malloc_multiply(n_extra, sizeof(struct fiemap_extent)); |
17c40b3a ML |
462 | if (!result_fiemap) |
463 | return -ENOMEM; | |
464 | ||
465 | /* XFS filesystem has incorrect implementation of fiemap ioctl and | |
466 | * returns extents for only one block-group at a time, so we need | |
467 | * to handle it manually, starting the next fiemap call from the end | |
468 | * of the last extent | |
469 | */ | |
470 | while (fiemap_start < fiemap_length) { | |
471 | *fiemap = (struct fiemap) { | |
472 | .fm_start = fiemap_start, | |
473 | .fm_length = fiemap_length, | |
474 | .fm_flags = FIEMAP_FLAG_SYNC, | |
475 | }; | |
476 | ||
477 | /* Find out how many extents there are */ | |
478 | if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) | |
479 | return log_debug_errno(errno, "Failed to read extents: %m"); | |
480 | ||
481 | /* Nothing to process */ | |
482 | if (fiemap->fm_mapped_extents == 0) | |
483 | break; | |
484 | ||
6524f1a8 ZJS |
485 | /* Resize fiemap to allow us to read in the extents, result fiemap has to hold all |
486 | * the extents for the whole file. Add space for the initial struct fiemap. */ | |
487 | if (!greedy_realloc0((void**) &fiemap, &fiemap_allocated, | |
488 | n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent))) | |
17c40b3a ML |
489 | return -ENOMEM; |
490 | ||
491 | fiemap->fm_extent_count = fiemap->fm_mapped_extents; | |
492 | fiemap->fm_mapped_extents = 0; | |
493 | ||
494 | if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) | |
495 | return log_debug_errno(errno, "Failed to read extents: %m"); | |
496 | ||
6524f1a8 ZJS |
497 | /* Resize result_fiemap to allow us to copy in the extents */ |
498 | if (!greedy_realloc((void**) &result_fiemap, &result_fiemap_allocated, | |
499 | n_extra + result_extents + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent))) | |
17c40b3a ML |
500 | return -ENOMEM; |
501 | ||
502 | memcpy(result_fiemap->fm_extents + result_extents, | |
503 | fiemap->fm_extents, | |
504 | sizeof(struct fiemap_extent) * fiemap->fm_mapped_extents); | |
505 | ||
506 | result_extents += fiemap->fm_mapped_extents; | |
507 | ||
508 | /* Highly unlikely that it is zero */ | |
6524f1a8 | 509 | if (_likely_(fiemap->fm_mapped_extents > 0)) { |
17c40b3a ML |
510 | uint32_t i = fiemap->fm_mapped_extents - 1; |
511 | ||
512 | fiemap_start = fiemap->fm_extents[i].fe_logical + | |
513 | fiemap->fm_extents[i].fe_length; | |
514 | ||
515 | if (fiemap->fm_extents[i].fe_flags & FIEMAP_EXTENT_LAST) | |
516 | break; | |
517 | } | |
518 | } | |
519 | ||
520 | memcpy(result_fiemap, fiemap, sizeof(struct fiemap)); | |
521 | result_fiemap->fm_mapped_extents = result_extents; | |
522 | *ret = TAKE_PTR(result_fiemap); | |
523 | return 0; | |
524 | } | |
525 | ||
28ca9c24 | 526 | static int can_sleep_internal(const char *verb, bool check_allowed, const SleepConfig *sleep_config); |
e8f1d00d | 527 | |
28ca9c24 | 528 | static bool can_s2h(const SleepConfig *sleep_config) { |
c863dc05 | 529 | const char *p; |
c58493c0 ML |
530 | int r; |
531 | ||
1bbbefe7 | 532 | if (!clock_supported(CLOCK_BOOTTIME_ALARM)) { |
c58493c0 | 533 | log_full(errno == ENOENT ? LOG_DEBUG : LOG_WARNING, |
1bbbefe7 | 534 | "CLOCK_BOOTTIME_ALARM is not supported"); |
c58493c0 ML |
535 | return false; |
536 | } | |
537 | ||
c863dc05 | 538 | FOREACH_STRING(p, "suspend", "hibernate") { |
28ca9c24 | 539 | r = can_sleep_internal(p, false, sleep_config); |
8340b762 | 540 | if (IN_SET(r, 0, -ENOSPC, -EADV)) { |
c863dc05 ZJS |
541 | log_debug("Unable to %s system.", p); |
542 | return false; | |
543 | } | |
b71c9758 ZJS |
544 | if (r < 0) |
545 | return log_debug_errno(r, "Failed to check if %s is possible: %m", p); | |
c58493c0 ML |
546 | } |
547 | ||
548 | return true; | |
549 | } | |
550 | ||
28ca9c24 | 551 | static int can_sleep_internal(const char *verb, bool check_allowed, const SleepConfig *sleep_config) { |
e8f1d00d | 552 | bool allow; |
28ca9c24 | 553 | char **modes = NULL, **states = NULL; |
19adb8a3 ZJS |
554 | int r; |
555 | ||
e68c79db | 556 | assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")); |
c58493c0 | 557 | |
28ca9c24 | 558 | r = sleep_settings(verb, sleep_config, &allow, &modes, &states); |
19adb8a3 ZJS |
559 | if (r < 0) |
560 | return false; | |
561 | ||
e8f1d00d ZJS |
562 | if (check_allowed && !allow) { |
563 | log_debug("Sleep mode \"%s\" is disabled by configuration.", verb); | |
564 | return false; | |
565 | } | |
566 | ||
567 | if (streq(verb, "suspend-then-hibernate")) | |
28ca9c24 | 568 | return can_s2h(sleep_config); |
e8f1d00d | 569 | |
69ab8088 ZJS |
570 | if (!can_sleep_state(states) || !can_sleep_disk(modes)) |
571 | return false; | |
572 | ||
b71c9758 ZJS |
573 | if (streq(verb, "suspend")) |
574 | return true; | |
575 | ||
4638cd39 | 576 | if (!enough_swap_for_hibernation()) |
b71c9758 ZJS |
577 | return -ENOSPC; |
578 | ||
579 | return true; | |
19adb8a3 | 580 | } |
e8f1d00d ZJS |
581 | |
582 | int can_sleep(const char *verb) { | |
28ca9c24 ZS |
583 | _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL; |
584 | int r; | |
585 | ||
586 | r = parse_sleep_config(&sleep_config); | |
587 | if (r < 0) | |
588 | return r; | |
589 | ||
590 | return can_sleep_internal(verb, true, sleep_config); | |
591 | } | |
592 | ||
593 | int sleep_settings(const char *verb, const SleepConfig *sleep_config, bool *ret_allow, char ***ret_modes, char ***ret_states) { | |
594 | ||
595 | assert(verb); | |
596 | assert(sleep_config); | |
597 | assert(STR_IN_SET(verb, "suspend", "hibernate", "hybrid-sleep", "suspend-then-hibernate")); | |
598 | ||
599 | if (streq(verb, "suspend")) { | |
600 | *ret_allow = sleep_config->allow_suspend; | |
601 | *ret_modes = sleep_config->suspend_modes; | |
602 | *ret_states = sleep_config->suspend_states; | |
603 | } else if (streq(verb, "hibernate")) { | |
604 | *ret_allow = sleep_config->allow_hibernate; | |
605 | *ret_modes = sleep_config->hibernate_modes; | |
606 | *ret_states = sleep_config->hibernate_states; | |
607 | } else if (streq(verb, "hybrid-sleep")) { | |
608 | *ret_allow = sleep_config->allow_hybrid_sleep; | |
609 | *ret_modes = sleep_config->hybrid_modes; | |
610 | *ret_states = sleep_config->hybrid_states; | |
611 | } else if (streq(verb, "suspend-then-hibernate")) { | |
612 | *ret_allow = sleep_config->allow_s2h; | |
613 | *ret_modes = *ret_states = NULL; | |
614 | } | |
615 | ||
616 | /* suspend modes empty by default */ | |
617 | if ((!ret_modes && !streq(verb, "suspend")) || !ret_states) | |
618 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No modes or states set for %s; Check sleep.conf", verb); | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
623 | void free_sleep_config(SleepConfig *sc) { | |
624 | if (!sc) | |
625 | return; | |
626 | ||
627 | strv_free(sc->suspend_modes); | |
628 | strv_free(sc->suspend_states); | |
629 | ||
630 | strv_free(sc->hibernate_modes); | |
631 | strv_free(sc->hibernate_states); | |
632 | ||
633 | strv_free(sc->hybrid_modes); | |
634 | strv_free(sc->hybrid_states); | |
635 | ||
636 | free(sc); | |
e8f1d00d | 637 | } |