]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/hibernate-util.c
hwdb: Add mapping for Xiaomi Mipad 2 bottom bezel capacitive buttons
[thirdparty/systemd.git] / src / shared / hibernate-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2018 Dell Inc.
4 ***/
5
6 #include <linux/fs.h>
7 #include <linux/magic.h>
8 #include <stddef.h>
9 #include <sys/ioctl.h>
10 #include <unistd.h>
11
12 #include "alloc-util.h"
13 #include "blockdev-util.h"
14 #include "btrfs-util.h"
15 #include "device-util.h"
16 #include "devnum-util.h"
17 #include "efivars.h"
18 #include "env-util.h"
19 #include "errno-util.h"
20 #include "fd-util.h"
21 #include "fileio.h"
22 #include "hibernate-util.h"
23 #include "log.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "proc-cmdline.h"
27 #include "stat-util.h"
28 #include "string-util.h"
29 #include "strv.h"
30
31 #define HIBERNATION_SWAP_THRESHOLD 0.98
32
33 void hibernation_device_done(HibernationDevice *device) {
34 assert(device);
35
36 free(device->path);
37 }
38
39 int read_fiemap(int fd, struct fiemap **ret) {
40 _cleanup_free_ struct fiemap *fiemap = NULL, *result_fiemap = NULL;
41 struct stat statinfo;
42 uint32_t result_extents = 0;
43 uint64_t fiemap_start = 0, fiemap_length;
44 const size_t n_extra = DIV_ROUND_UP(sizeof(struct fiemap), sizeof(struct fiemap_extent));
45
46 assert(fd >= 0);
47 assert(ret);
48
49 if (fstat(fd, &statinfo) < 0)
50 return log_debug_errno(errno, "Cannot determine file size: %m");
51 if (!S_ISREG(statinfo.st_mode))
52 return -ENOTTY;
53 fiemap_length = statinfo.st_size;
54
55 /* Zero this out in case we run on a file with no extents */
56 fiemap = calloc(n_extra, sizeof(struct fiemap_extent));
57 if (!fiemap)
58 return -ENOMEM;
59
60 result_fiemap = malloc_multiply(n_extra, sizeof(struct fiemap_extent));
61 if (!result_fiemap)
62 return -ENOMEM;
63
64 /* XFS filesystem has incorrect implementation of fiemap ioctl and
65 * returns extents for only one block-group at a time, so we need
66 * to handle it manually, starting the next fiemap call from the end
67 * of the last extent
68 */
69 while (fiemap_start < fiemap_length) {
70 *fiemap = (struct fiemap) {
71 .fm_start = fiemap_start,
72 .fm_length = fiemap_length,
73 .fm_flags = FIEMAP_FLAG_SYNC,
74 };
75
76 /* Find out how many extents there are */
77 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0)
78 return log_debug_errno(errno, "Failed to read extents: %m");
79
80 /* Nothing to process */
81 if (fiemap->fm_mapped_extents == 0)
82 break;
83
84 /* Resize fiemap to allow us to read in the extents, result fiemap has to hold all
85 * the extents for the whole file. Add space for the initial struct fiemap. */
86 if (!greedy_realloc0((void**) &fiemap, n_extra + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
87 return -ENOMEM;
88
89 fiemap->fm_extent_count = fiemap->fm_mapped_extents;
90 fiemap->fm_mapped_extents = 0;
91
92 if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0)
93 return log_debug_errno(errno, "Failed to read extents: %m");
94
95 /* Resize result_fiemap to allow us to copy in the extents */
96 if (!greedy_realloc((void**) &result_fiemap,
97 n_extra + result_extents + fiemap->fm_mapped_extents, sizeof(struct fiemap_extent)))
98 return -ENOMEM;
99
100 memcpy(result_fiemap->fm_extents + result_extents,
101 fiemap->fm_extents,
102 sizeof(struct fiemap_extent) * fiemap->fm_mapped_extents);
103
104 result_extents += fiemap->fm_mapped_extents;
105
106 /* Highly unlikely that it is zero */
107 if (_likely_(fiemap->fm_mapped_extents > 0)) {
108 uint32_t i = fiemap->fm_mapped_extents - 1;
109
110 fiemap_start = fiemap->fm_extents[i].fe_logical +
111 fiemap->fm_extents[i].fe_length;
112
113 if (fiemap->fm_extents[i].fe_flags & FIEMAP_EXTENT_LAST)
114 break;
115 }
116 }
117
118 memcpy(result_fiemap, fiemap, sizeof(struct fiemap));
119 result_fiemap->fm_mapped_extents = result_extents;
120 *ret = TAKE_PTR(result_fiemap);
121 return 0;
122 }
123
124 static int read_resume_config(dev_t *ret_devno, uint64_t *ret_offset) {
125 _cleanup_free_ char *devno_str = NULL, *offset_str = NULL;
126 uint64_t offset;
127 dev_t devno;
128 int r;
129
130 assert(ret_devno);
131 assert(ret_offset);
132
133 r = proc_cmdline_get_key("noresume", /* flags = */ 0, /* ret_value = */ NULL);
134 if (r < 0)
135 return log_debug_errno(r, "Failed to check if 'noresume' kernel command line option is set: %m");
136 if (r > 0)
137 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
138 "'noresume' kernel command line option is set, refusing hibernation device lookup.");
139
140 r = read_one_line_file("/sys/power/resume", &devno_str);
141 if (r < 0)
142 return log_debug_errno(r, "Failed to read /sys/power/resume: %m");
143
144 r = parse_devnum(devno_str, &devno);
145 if (r < 0)
146 return log_debug_errno(r, "Failed to parse /sys/power/resume devno '%s': %m", devno_str);
147
148 r = read_one_line_file("/sys/power/resume_offset", &offset_str);
149 if (r == -ENOENT) {
150 log_debug_errno(r, "Kernel does not expose resume_offset, skipping.");
151 offset = UINT64_MAX;
152 } else if (r < 0)
153 return log_debug_errno(r, "Failed to read /sys/power/resume_offset: %m");
154 else {
155 r = safe_atou64(offset_str, &offset);
156 if (r < 0)
157 return log_debug_errno(r,
158 "Failed to parse /sys/power/resume_offset '%s': %m", offset_str);
159 }
160
161 if (devno == 0 && offset > 0 && offset != UINT64_MAX)
162 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
163 "Found populated /sys/power/resume_offset (%" PRIu64 ") but /sys/power/resume is not set, refusing.",
164 offset);
165
166 *ret_devno = devno;
167 *ret_offset = offset;
168
169 return 0;
170 }
171
172 /* entry in /proc/swaps */
173 typedef struct SwapEntry {
174 char *path;
175 bool swapfile;
176
177 uint64_t size;
178 uint64_t used;
179 int priority;
180
181 /* Not present in original entry */
182 dev_t devno;
183 uint64_t offset;
184 } SwapEntry;
185
186 typedef struct SwapEntries {
187 SwapEntry *swaps;
188 size_t n_swaps;
189 } SwapEntries;
190
191 static void swap_entry_done(SwapEntry *entry) {
192 assert(entry);
193
194 free(entry->path);
195 }
196
197 static void swap_entries_done(SwapEntries *entries) {
198 assert(entries);
199
200 FOREACH_ARRAY(i, entries->swaps, entries->n_swaps)
201 swap_entry_done(i);
202
203 free(entries->swaps);
204 }
205
206 static int swap_entry_get_resume_config(SwapEntry *swap) {
207 _cleanup_close_ int fd = -EBADF;
208 uint64_t offset_raw;
209 struct stat st;
210 int r;
211
212 assert(swap);
213 assert(swap->path);
214
215 fd = open(swap->path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
216 if (fd < 0)
217 return -errno;
218
219 if (fstat(fd, &st) < 0)
220 return -errno;
221
222 if (!swap->swapfile) {
223 if (!S_ISBLK(st.st_mode))
224 return -ENOTBLK;
225
226 swap->devno = st.st_rdev;
227 swap->offset = 0;
228 return 0;
229 }
230
231 r = stat_verify_regular(&st);
232 if (r < 0)
233 return r;
234
235 r = get_block_device_fd(fd, &swap->devno);
236 if (r < 0)
237 return r;
238
239 r = fd_is_fs_type(fd, BTRFS_SUPER_MAGIC);
240 if (r < 0)
241 return log_debug_errno(r, "Failed to check if swap file '%s' is on Btrfs: %m", swap->path);
242 if (r > 0) {
243 r = btrfs_get_file_physical_offset_fd(fd, &offset_raw);
244 if (r < 0)
245 return r;
246 } else {
247 _cleanup_free_ struct fiemap *fiemap = NULL;
248
249 r = read_fiemap(fd, &fiemap);
250 if (r < 0)
251 return log_debug_errno(r, "Failed to read extent map for swap file '%s': %m", swap->path);
252
253 offset_raw = fiemap->fm_extents[0].fe_physical;
254 }
255
256 swap->offset = offset_raw / page_size();
257 return 0;
258 }
259
260 static int read_swap_entries(SwapEntries *ret) {
261 _cleanup_(swap_entries_done) SwapEntries entries = {};
262 _cleanup_fclose_ FILE *f = NULL;
263
264 assert(ret);
265
266 f = fopen("/proc/swaps", "re");
267 if (!f)
268 return log_debug_errno(errno, "Failed to open /proc/swaps: %m");
269
270 /* Remove header */
271 (void) fscanf(f, "%*s %*s %*s %*s %*s\n");
272
273 for (unsigned i = 1;; i++) {
274 _cleanup_(swap_entry_done) SwapEntry swap = {};
275 _cleanup_free_ char *type = NULL;
276 int k;
277
278 k = fscanf(f,
279 "%ms " /* device/file path */
280 "%ms " /* type of swap */
281 "%" PRIu64 /* swap size */
282 "%" PRIu64 /* used */
283 "%i" /* priority */
284 "\n",
285 &swap.path, &type, &swap.size, &swap.used, &swap.priority);
286 if (k == EOF)
287 break;
288 if (k != 5)
289 return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Failed to parse /proc/swaps line %u.", i);
290
291 if (streq(type, "file")) {
292 if (endswith(swap.path, "\\040(deleted)")) {
293 log_debug("Swap file '%s' has been deleted, ignoring.", swap.path);
294 continue;
295 }
296
297 swap.swapfile = true;
298
299 } else if (streq(type, "partition")) {
300 const char *node;
301
302 node = path_startswith(swap.path, "/dev/");
303 if (node && startswith(node, "zram")) {
304 log_debug("Swap partition '%s' is a zram device, ignoring.", swap.path);
305 continue;
306 }
307
308 swap.swapfile = false;
309
310 } else {
311 log_debug("Swap type %s is not supported for hibernation, ignoring device: %s",
312 type, swap.path);
313 continue;
314 }
315
316 if (!GREEDY_REALLOC(entries.swaps, entries.n_swaps + 1))
317 return log_oom_debug();
318
319 entries.swaps[entries.n_swaps++] = TAKE_STRUCT(swap);
320 }
321
322 *ret = TAKE_STRUCT(entries);
323 return 0;
324 }
325
326 /* Attempt to find a suitable device for hibernation by parsing /proc/swaps, /sys/power/resume, and
327 * /sys/power/resume_offset.
328 *
329 * Beware:
330 * Never use a device or file that hasn't been somehow specified by a user who would also be entrusted
331 * with full system memory access (for example via /sys/power/resume) or that isn't an already active
332 * swap area! Otherwise various security attacks might become possible, for example an attacker could
333 * silently attach such a device and circumvent full disk encryption when it would be automatically used
334 * for hibernation. Also, having a swap area on top of encryption is not per se enough to protect from all
335 * such attacks.
336 *
337 * Returns:
338 * 1 - Values are set in /sys/power/resume and /sys/power/resume_offset.
339 *
340 * 0 - No values are set in /sys/power/resume and /sys/power/resume_offset.
341 * ret will represent the highest priority swap with most remaining space discovered in /proc/swaps.
342 *
343 * Negative value in the case of error */
344 int find_suitable_hibernation_device_full(HibernationDevice *ret_device, uint64_t *ret_size, uint64_t *ret_used) {
345 _cleanup_(swap_entries_done) SwapEntries entries = {};
346 SwapEntry *entry = NULL;
347 uint64_t resume_config_offset;
348 dev_t resume_config_devno;
349 int r;
350
351 assert(!ret_size == !ret_used);
352
353 r = read_resume_config(&resume_config_devno, &resume_config_offset);
354 if (r < 0)
355 return r;
356
357 r = read_swap_entries(&entries);
358 if (r < 0)
359 return r;
360 if (entries.n_swaps == 0)
361 return log_debug_errno(SYNTHETIC_ERRNO(ENOSPC), "No swap space available for hibernation.");
362
363 FOREACH_ARRAY(swap, entries.swaps, entries.n_swaps) {
364 r = swap_entry_get_resume_config(swap);
365 if (r < 0)
366 return log_debug_errno(r, "Failed to get devno and offset for swap '%s': %m", swap->path);
367 if (swap->devno == 0) {
368 assert(swap->swapfile);
369
370 log_debug("Swap file '%s' is not backed by block device, ignoring: %m", swap->path);
371 continue;
372 }
373
374 if (resume_config_devno > 0) {
375 if (swap->devno == resume_config_devno &&
376 (!swap->swapfile || resume_config_offset == UINT64_MAX || swap->offset == resume_config_offset)) {
377 /* /sys/power/resume (resume=) is set, and the calculated swap file offset
378 * matches with /sys/power/resume_offset. If /sys/power/resume_offset is not
379 * exposed, we can't do proper check anyway, so use the found swap file too. */
380 entry = swap;
381 break;
382 }
383
384 /* If resume= is set, don't try to use other swap spaces. */
385 continue;
386 }
387
388 if (!entry ||
389 swap->priority > entry->priority ||
390 swap->size - swap->used > entry->size - entry->used)
391 entry = swap;
392 }
393
394 if (!entry) {
395 /* No need to check n_swaps == 0, since it's rejected early */
396 assert(resume_config_devno > 0);
397 return log_debug_errno(SYNTHETIC_ERRNO(ENOSPC), "Cannot find swap entry corresponding to /sys/power/resume.");
398 }
399
400 if (ret_device) {
401 char *path;
402
403 if (entry->swapfile) {
404 r = device_path_make_canonical(S_IFBLK, entry->devno, &path);
405 if (r < 0)
406 return log_debug_errno(r,
407 "Failed to format canonical device path for devno '" DEVNUM_FORMAT_STR "': %m",
408 DEVNUM_FORMAT_VAL(entry->devno));
409 } else
410 path = TAKE_PTR(entry->path);
411
412 *ret_device = (HibernationDevice) {
413 .devno = entry->devno,
414 .offset = entry->offset,
415 .path = path,
416 };
417 }
418
419 if (ret_size) {
420 *ret_size = entry->size;
421 *ret_used = entry->used;
422 }
423
424 return resume_config_devno > 0;
425 }
426
427 static int get_proc_meminfo_active(unsigned long long *ret) {
428 _cleanup_free_ char *active_str = NULL;
429 unsigned long long active;
430 int r;
431
432 assert(ret);
433
434 r = get_proc_field("/proc/meminfo", "Active(anon)", WHITESPACE, &active_str);
435 if (r < 0)
436 return log_debug_errno(r, "Failed to retrieve Active(anon) from /proc/meminfo: %m");
437
438 r = safe_atollu(active_str, &active);
439 if (r < 0)
440 return log_debug_errno(r, "Failed to parse Active(anon) '%s' from /proc/meminfo: %m", active_str);
441
442 *ret = active;
443 return 0;
444 }
445
446 int hibernation_is_safe(void) {
447 unsigned long long active;
448 uint64_t size, used;
449 bool resume_set, bypass_space_check;
450 int r;
451
452 bypass_space_check = getenv_bool("SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK") > 0;
453
454 r = find_suitable_hibernation_device_full(NULL, &size, &used);
455 if (r == -ENOSPC && bypass_space_check)
456 /* If we don't have any available swap space at all, and SYSTEMD_BYPASS_HIBERNATION_MEMORY_CHECK
457 * is set, skip all remaining checks since we can't do that properly anyway. It is quite
458 * possible that the user is using a setup similar to #30083. When we actually perform
459 * hibernation in sleep.c we'll check everything again. */
460 return 0;
461 if (r < 0)
462 return r;
463 resume_set = r > 0;
464
465 if (!resume_set && !is_efi_boot())
466 return log_debug_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
467 "Not running on EFI and resume= is not set. Hibernation is not safe.");
468
469 if (bypass_space_check)
470 return 0;
471
472 r = get_proc_meminfo_active(&active);
473 if (r < 0)
474 return r;
475
476 r = active <= (size - used) * HIBERNATION_SWAP_THRESHOLD;
477 log_debug("Detected %s swap for hibernation: Active(anon)=%llu kB, size=%" PRIu64 " kB, used=%" PRIu64 " kB, threshold=%.2g%%",
478 r ? "enough" : "not enough", active, size, used, 100 * HIBERNATION_SWAP_THRESHOLD);
479 if (!r)
480 return -ENOSPC;
481
482 return resume_set;
483 }
484
485 int write_resume_config(dev_t devno, uint64_t offset, const char *device) {
486 char offset_str[DECIMAL_STR_MAX(uint64_t)];
487 const char *devno_str;
488 int r;
489
490 assert(devno > 0);
491 assert(device);
492
493 devno_str = FORMAT_DEVNUM(devno);
494 xsprintf(offset_str, "%" PRIu64, offset);
495
496 /* We write the offset first since it's safer. Note that this file is only available in 4.17+, so
497 * fail gracefully if it doesn't exist and we're only overwriting it with 0. */
498 r = write_string_file("/sys/power/resume_offset", offset_str, WRITE_STRING_FILE_DISABLE_BUFFER);
499 if (r == -ENOENT) {
500 if (offset != 0)
501 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
502 "Can't configure swap file offset %s, kernel does not support /sys/power/resume_offset. Refusing.",
503 offset_str);
504
505 log_warning_errno(r, "/sys/power/resume_offset is unavailable, skipping writing swap file offset.");
506 } else if (r < 0)
507 return log_error_errno(r,
508 "Failed to write swap file offset %s to /sys/power/resume_offset for device '%s': %m",
509 offset_str, device);
510 else
511 log_debug("Wrote resume_offset=%s for device '%s' to /sys/power/resume_offset.",
512 offset_str, device);
513
514 r = write_string_file("/sys/power/resume", devno_str, WRITE_STRING_FILE_DISABLE_BUFFER);
515 if (r < 0)
516 return log_error_errno(r,
517 "Failed to write device '%s' (%s) to /sys/power/resume: %m",
518 device, devno_str);
519 log_debug("Wrote resume=%s for device '%s' to /sys/power/resume.", devno_str, device);
520
521 return 0;
522 }
523
524 int clear_efi_hibernate_location_and_warn(void) {
525 int r;
526
527 if (!is_efi_boot())
528 return 0;
529
530 r = efi_set_variable(EFI_SYSTEMD_VARIABLE(HibernateLocation), NULL, 0);
531 if (r == -ENOENT)
532 return 0;
533 if (r < 0)
534 return log_warning_errno(r, "Failed to clear HibernateLocation EFI variable: %m");
535
536 return 1;
537 }