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