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