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