Debian Codegraph @

src/plugins/fs/btrfs.c

raw
1 /*
2 * Copyright (C) 2020 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Vojtech Trefny <[email protected]>
18 */
19
20 #include <blockdev/utils.h>
21 #include <check_deps.h>
22 #include <stdio.h>
23
24 #include "btrfs.h"
25 #include "fs.h"
26 #include "common.h"
27
28 static volatile guint avail_deps = 0;
29 static GMutex deps_check_lock;
30
31 G_GNUC_INTERNAL
32 void _fs_btrfs_reset_avail_deps (void) {
33 g_atomic_int_set (&avail_deps, 0);
34 }
35
36 #define DEPS_MKFSBTRFS 0
37 #define DEPS_MKFSBTRFS_MASK (1 << DEPS_MKFSBTRFS)
38 #define DEPS_BTRFSCK 1
39 #define DEPS_BTRFSCK_MASK (1 << DEPS_BTRFSCK)
40 #define DEPS_BTRFS 2
41 #define DEPS_BTRFS_MASK (1 << DEPS_BTRFS)
42 #define DEPS_BTRFSTUNE 3
43 #define DEPS_BTRFSTUNE_MASK (1 << DEPS_BTRFSTUNE)
44
45 #define DEPS_LAST 4
46
47 static const UtilDep deps[DEPS_LAST] = {
48 {"mkfs.btrfs", NULL, NULL, NULL},
49 {"btrfsck", NULL, NULL, NULL},
50 {"btrfs", NULL, NULL, NULL},
51 {"btrfstune", NULL, NULL, NULL},
52 };
53
54 static guint32 fs_mode_util[BD_FS_MODE_LAST+1] = {
55 DEPS_MKFSBTRFS_MASK, /* mkfs */
56 0, /* wipe */
57 DEPS_BTRFSCK_MASK, /* check */
58 DEPS_BTRFSCK_MASK, /* repair */
59 DEPS_BTRFS_MASK, /* set-label */
60 DEPS_BTRFS_MASK, /* query */
61 DEPS_BTRFS_MASK, /* resize */
62 DEPS_BTRFSTUNE_MASK, /* set-uuid */
63 };
64
65
66 /**
67 * bd_fs_btrfs_is_tech_avail:
68 * @tech: the queried tech
69 * @mode: a bit mask of queried modes of operation (#BDFSTechMode) for @tech
70 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
71 *
72 * Returns: whether the @tech-@mode combination is available -- supported by the
73 * plugin implementation and having all the runtime dependencies available
74 */
75 G_GNUC_INTERNAL gboolean
76 bd_fs_btrfs_is_tech_avail (BDFSTech tech G_GNUC_UNUSED, guint64 mode, GError **error) {
77 guint32 required = 0;
78 guint i = 0;
79
80 for (i = 0; i <= BD_FS_MODE_LAST; i++)
81 if (mode & (1 << i))
82 required |= fs_mode_util[i];
83
84 return check_deps (&avail_deps, required, deps, DEPS_LAST, &deps_check_lock, error);
85 }
86
87 /**
88 * bd_fs_btrfs_info_copy: (skip)
89 * @data: (nullable): %BDFSBtrfsInfo to copy
90 *
91 * Creates a new copy of @data.
92 */
93 BDFSBtrfsInfo* bd_fs_btrfs_info_copy (BDFSBtrfsInfo *data) {
94 if (data == NULL)
95 return NULL;
96
97 BDFSBtrfsInfo *ret = g_new0 (BDFSBtrfsInfo, 1);
98
99 ret->label = g_strdup (data->label);
100 ret->uuid = g_strdup (data->uuid);
101 ret->size = data->size;
102 ret->free_space = data->free_space;
103
104 return ret;
105 }
106
107 /**
108 * bd_fs_btrfs_info_free: (skip)
109 * @data: (nullable): %BDFSBtrfsInfo to free
110 *
111 * Frees @data.
112 */
113 void bd_fs_btrfs_info_free (BDFSBtrfsInfo *data) {
114 if (data == NULL)
115 return;
116
117 g_free (data->label);
118 g_free (data->uuid);
119 g_free (data);
120 }
121
122 G_GNUC_INTERNAL BDExtraArg **
123 bd_fs_btrfs_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
124 GPtrArray *options_array = g_ptr_array_new ();
125 const BDExtraArg **extra_p = NULL;
126
127 if (options->label && g_strcmp0 (options->label, "") != 0)
128 g_ptr_array_add (options_array, bd_extra_arg_new ("-L", options->label));
129
130 if (options->uuid && g_strcmp0 (options->uuid, "") != 0)
131 g_ptr_array_add (options_array, bd_extra_arg_new ("-U", options->uuid));
132
133 if (options->no_discard)
134 g_ptr_array_add (options_array, bd_extra_arg_new ("-K", ""));
135
136 if (options->force)
137 g_ptr_array_add (options_array, bd_extra_arg_new ("-f", ""));
138
139 if (extra) {
140 for (extra_p = extra; *extra_p; extra_p++)
141 g_ptr_array_add (options_array, bd_extra_arg_copy ((BDExtraArg *) *extra_p));
142 }
143
144 g_ptr_array_add (options_array, NULL);
145
146 return (BDExtraArg **) g_ptr_array_free (options_array, FALSE);
147 }
148
149 /**
150 * bd_fs_btrfs_mkfs:
151 * @device: the device to create a new btrfs fs on
152 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
153 * passed to the 'mkfs.btrfs' utility)
154 * @error: (out) (optional): place to store error (if any)
155 *
156 * Returns: whether a new btrfs fs was successfully created on @device or not
157 *
158 * Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_MKFS
159 *
160 */
161 gboolean bd_fs_btrfs_mkfs (const gchar *device, const BDExtraArg **extra, GError **error) {
162 const gchar *args[3] = {"mkfs.btrfs", device, NULL};
163
164 if (!check_deps (&avail_deps, DEPS_MKFSBTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
165 return FALSE;
166
167 return bd_utils_exec_and_report_error (args, extra, error);
168 }
169
170 /**
171 * bd_fs_btrfs_check:
172 * @device: the device containing the file system to check
173 * @extra: (nullable) (array zero-terminated=1): extra options for the check (right now
174 * passed to the 'btrfsck' utility)
175 * @error: (out) (optional): place to store error (if any)
176 *
177 * Returns: whether the filesystem was successfully checked or not
178 *
179 * Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_CHECK
180 */
181 gboolean bd_fs_btrfs_check (const gchar *device, const BDExtraArg **extra, GError **error) {
182 const gchar *argv[4] = {"btrfsck", device, NULL};
183
184 if (!check_deps (&avail_deps, DEPS_BTRFSCK_MASK, deps, DEPS_LAST, &deps_check_lock, error))
185 return FALSE;
186
187 return bd_utils_exec_and_report_error (argv, extra, error);
188 }
189
190 /**
191 * bd_fs_btrfs_repair:
192 * @device: the device containing the file system to repair
193 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
194 * passed to the 'btrfsck' utility)
195 * @error: (out) (optional): place to store error (if any)
196 *
197 * Returns: whether the filesystem was successfully checked and repaired or not
198 *
199 * Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_REPAIR
200 */
201 gboolean bd_fs_btrfs_repair (const gchar *device, const BDExtraArg **extra, GError **error) {
202 const gchar *argv[5] = {"btrfsck", "--repair", device, NULL};
203
204 if (!check_deps (&avail_deps, DEPS_BTRFSCK_MASK, deps, DEPS_LAST, &deps_check_lock, error))
205 return FALSE;
206
207 return bd_utils_exec_and_report_error (argv, extra, error);
208 }
209
210 /**
211 * bd_fs_btrfs_set_label:
212 * @mpoint: the mount point of the file system to set label for
213 * @label: label to set
214 * @error: (out) (optional): place to store error (if any)
215 *
216 * Returns: whether the label of btrfs file system on the @mpoint was
217 * successfully set or not
218 *
219 * Note: This function is intended to be used for btrfs filesystem on a single device,
220 * for more complicated setups use the btrfs plugin instead.
221 *
222 * Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_SET_LABEL
223 */
224 gboolean bd_fs_btrfs_set_label (const gchar *mpoint, const gchar *label, GError **error) {
225 const gchar *argv[6] = {"btrfs", "filesystem", "label", mpoint, label, NULL};
226
227 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
228 return FALSE;
229
230 return bd_utils_exec_and_report_error (argv, NULL, error);
231 }
232
233 /**
234 * bd_fs_btrfs_check_label:
235 * @label: label to check
236 * @error: (out) (optional): place to store error
237 *
238 * Returns: whether @label is a valid label for the btrfs file system or not
239 * (reason is provided in @error)
240 *
241 * Note: This function is intended to be used for btrfs filesystem on a single device,
242 * for more complicated setups use the btrfs plugin instead.
243 *
244 * Tech category: always available
245 */
246 gboolean bd_fs_btrfs_check_label (const gchar *label, GError **error) {
247 if (strlen (label) > 256) {
248 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
249 "Label for btrfs filesystem must be at most 256 characters long.");
250 return FALSE;
251 }
252
253 if (strchr (label, '\n') != NULL) {
254 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
255 "Label for btrfs filesystem cannot contain new lines.");
256 return FALSE;
257 }
258
259 return TRUE;
260 }
261
262 /**
263 * bd_fs_btrfs_set_uuid:
264 * @device: the device containing the file system to set the UUID (serial number) for
265 * @uuid: (nullable): UUID to set or %NULL to generate a new one
266 * @error: (out) (optional): place to store error (if any)
267 *
268 * Returns: whether the UUID of the btrfs file system on the @device was
269 * successfully set or not
270 *
271 * Note: This function is intended to be used for btrfs filesystem on a single device,
272 * for more complicated setups use the btrfs plugin instead.
273 *
274 * Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_SET_UUID
275 */
276 gboolean bd_fs_btrfs_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
277 const gchar *args[5] = {"btrfstune", NULL, NULL, NULL, NULL};
278
279 if (!check_deps (&avail_deps, DEPS_BTRFSTUNE_MASK, deps, DEPS_LAST, &deps_check_lock, error))
280 return FALSE;
281
282 if (!uuid) {
283 args[1] = "-u";
284 args[2] = device;
285 } else {
286 args[1] = "-U";
287 args[2] = uuid;
288 args[3] = device;
289 }
290
291 return bd_utils_exec_with_input (args, "y\n", NULL, error);
292 }
293
294 /**
295 * bd_fs_btrfs_check_uuid:
296 * @uuid: UUID to check
297 * @error: (out) (optional): place to store error
298 *
299 * Returns: whether @uuid is a valid UUID for the btrfs file system or not
300 * (reason is provided in @error)
301 *
302 * Note: This function is intended to be used for btrfs filesystem on a single device,
303 * for more complicated setups use the btrfs plugin instead.
304 *
305 * Tech category: always available
306 */
307 gboolean bd_fs_btrfs_check_uuid (const gchar *uuid, GError **error) {
308 return check_uuid (uuid, error);
309 }
310
311 /**
312 * bd_fs_btrfs_get_info:
313 * @mpoint: a mountpoint of the btrfs filesystem to get information about
314 * @error: (out) (optional): place to store error (if any)
315 *
316 * Returns: (transfer full): information about the file system on @device or
317 * %NULL in case of error
318 *
319 * Note: This function WON'T WORK for multi device btrfs filesystems,
320 * for more complicated setups use the btrfs plugin instead.
321 *
322 * Tech category: %BD_FS_TECH_BTRFS-%BD_FS_TECH_MODE_QUERY
323 */
324 BDFSBtrfsInfo* bd_fs_btrfs_get_info (const gchar *mpoint, GError **error) {
325 const gchar *argv[6] = {"btrfs", "filesystem", "show", "--raw", mpoint, NULL};
326 g_autofree gchar *output = NULL;
327 gboolean success = FALSE;
328 gchar const * const pattern = "Label:\\s+(none|'(?P<label>.+)')\\s+" \
329 "uuid:\\s+(?P<uuid>\\S+)\\s+" \
330 "Total\\sdevices\\s+(?P<num_devices>\\d+)\\s+" \
331 "FS\\sbytes\\sused\\s+(?P<used>\\S+)\\s+" \
332 "devid\\s+1\\s+size\\s+(?P<size>\\S+)\\s+\\S+";
333 GRegex *regex = NULL;
334 GMatchInfo *match_info = NULL;
335 BDFSBtrfsInfo *ret = NULL;
336 gchar *item = NULL;
337 guint64 num_devices = 0;
338 guint64 min_size = 0;
339 gint scanned = 0;
340
341 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
342 return NULL;
343
344 regex = g_regex_new (pattern, G_REGEX_EXTENDED, 0, error);
345 if (!regex) {
346 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to create new GRegex");
347 /* error is already populated */
348 return NULL;
349 }
350
351 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
352 if (!success) {
353 /* error is already populated from the call above or just empty
354 output */
355 g_regex_unref (regex);
356 return NULL;
357 }
358
359 success = g_regex_match (regex, output, 0, &match_info);
360 if (!success) {
361 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE,
362 "Failed to parse btrfs filesystem information");
363 g_regex_unref (regex);
364 g_match_info_free (match_info);
365 return NULL;
366 }
367
368 ret = g_new0 (BDFSBtrfsInfo, 1);
369
370 ret->label = g_match_info_fetch_named (match_info, "label");
371 ret->uuid = g_match_info_fetch_named (match_info, "uuid");
372
373 item = g_match_info_fetch_named (match_info, "num_devices");
374 num_devices = g_ascii_strtoull (item, NULL, 0);
375 if (num_devices != 1) {
376 g_set_error (error, BD_FS_ERROR, BD_FS_ERROR_FAIL,
377 "Btrfs filesystem mounted on %s spans multiple devices (%"G_GUINT64_FORMAT")." \
378 "Filesystem plugin is not suitable for multidevice Btrfs volumes, please use " \
379 "Btrfs plugin instead.", mpoint, num_devices);
380 g_free (item);
381 g_match_info_free (match_info);
382 g_regex_unref (regex);
383 bd_fs_btrfs_info_free (ret);
384 return NULL;
385 }
386
387 g_free (item);
388 item = g_match_info_fetch_named (match_info, "size");
389 ret->size = g_ascii_strtoull (item, NULL, 0);
390 g_free (item);
391
392 g_match_info_free (match_info);
393 g_regex_unref (regex);
394
395 argv[1] = "inspect-internal";
396 argv[2] = "min-dev-size";
397 argv[3] = mpoint;
398 argv[4] = NULL;
399
400 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
401 if (!success) {
402 /* error is already populated from the call above or just empty
403 output */
404 bd_fs_btrfs_info_free (ret);
405 return NULL;
406 }
407
408 /* 114032640 bytes (108.75MiB) */
409 scanned = sscanf (output, " %" G_GUINT64_FORMAT " bytes", &min_size);
410 if (scanned != 1) {
411 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE,
412 "Failed to parse btrfs filesystem min size.");
413 bd_fs_btrfs_info_free (ret);
414 return NULL;
415 }
416
417 ret->free_space = ret->size - min_size;
418
419 return ret;
420 }
421
422 /**
423 * bd_fs_btrfs_resize:
424 * @mpoint: a mountpoint of the to be resized btrfs filesystem
425 * @new_size: requested new size
426 * @extra: (nullable) (array zero-terminated=1): extra options for the volume resize (right now
427 * passed to the 'btrfs' utility)
428 * @error: (out) (optional): place to store error (if any)
429 *
430 * Returns: whether the @mpoint filesystem was successfully resized to @new_size
431 * or not
432 *
433 * Note: This function WON'T WORK for multi device btrfs filesystems,
434 * for more complicated setups use the btrfs plugin instead.
435 *
436 * Tech category: %BD_BTRFS_TECH_FS-%BD_BTRFS_TECH_MODE_MODIFY
437 */
438 gboolean bd_fs_btrfs_resize (const gchar *mpoint, guint64 new_size, const BDExtraArg **extra, GError **error) {
439 const gchar *argv[6] = {"btrfs", "filesystem", "resize", NULL, mpoint, NULL};
440 gboolean ret = FALSE;
441 BDFSBtrfsInfo *info = NULL;
442
443 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
444 return FALSE;
445
446 /* we don't want to allow resizing multidevice btrfs volumes and get_info
447 returns error for these so just try to get the info here */
448 info = bd_fs_btrfs_get_info (mpoint, error);
449 if (!info)
450 return FALSE;
451 bd_fs_btrfs_info_free (info);
452
453 if (new_size == 0)
454 argv[3] = g_strdup ("max");
455 else
456 argv[3] = g_strdup_printf ("%"G_GUINT64_FORMAT, new_size);
457
458 ret = bd_utils_exec_and_report_error (argv, extra, error);
459 g_free ((gchar *) argv[3]);
460
461 return ret;
462 }