Debian Codegraph @

src/plugins/btrfs.c

raw
1 /*
2 * Copyright (C) 2014 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: Vratislav Podzimek <[email protected]>
18 */
19
20 #include <glib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <sys/ioctl.h>
26 #include <linux/btrfs.h>
27 #include <blockdev/utils.h>
28 #include <bs_size.h>
29
30 #include "btrfs.h"
31 #include "check_deps.h"
32
33 #define BTRFS_MIN_VERSION "3.18.2"
34
35 /**
36 * SECTION: btrfs
37 * @short_description: plugin for operations with BTRFS devices
38 * @title: BTRFS
39 * @include: btrfs.h
40 *
41 * A plugin for operations with btrfs devices.
42 */
43
44 /**
45 * bd_btrfs_error_quark: (skip)
46 */
47 GQuark bd_btrfs_error_quark (void)
48 {
49 return g_quark_from_static_string ("g-bd-btrfs-error-quark");
50 }
51
52 BDBtrfsDeviceInfo* bd_btrfs_device_info_copy (BDBtrfsDeviceInfo *info) {
53 if (info == NULL)
54 return NULL;
55
56 BDBtrfsDeviceInfo *new_info = g_new0 (BDBtrfsDeviceInfo, 1);
57
58 new_info->id = info->id;
59 new_info->path = g_strdup (info->path);
60 new_info->size = info->size;
61 new_info->used = info->used;
62
63 return new_info;
64 }
65
66 void bd_btrfs_device_info_free (BDBtrfsDeviceInfo *info) {
67 if (info == NULL)
68 return;
69
70 g_free (info->path);
71 g_free (info);
72 }
73
74 BDBtrfsSubvolumeInfo* bd_btrfs_subvolume_info_copy (BDBtrfsSubvolumeInfo *info) {
75 if (info == NULL)
76 return NULL;
77
78 BDBtrfsSubvolumeInfo *new_info = g_new0 (BDBtrfsSubvolumeInfo, 1);
79
80 new_info->id = info->id;
81 new_info->parent_id = info->parent_id;
82 new_info->path = g_strdup (info->path);
83
84 return new_info;
85 }
86
87 void bd_btrfs_subvolume_info_free (BDBtrfsSubvolumeInfo *info) {
88 if (info == NULL)
89 return;
90
91 g_free (info->path);
92 g_free (info);
93 }
94
95 BDBtrfsFilesystemInfo* bd_btrfs_filesystem_info_copy (BDBtrfsFilesystemInfo *info) {
96 if (info == NULL)
97 return NULL;
98
99 BDBtrfsFilesystemInfo *new_info = g_new0 (BDBtrfsFilesystemInfo, 1);
100
101 new_info->label = g_strdup (info->label);
102 new_info->uuid = g_strdup (info->uuid);
103 new_info->num_devices = info->num_devices;
104 new_info->used = info->used;
105
106 return new_info;
107 }
108
109 void bd_btrfs_filesystem_info_free (BDBtrfsFilesystemInfo *info) {
110 if (info == NULL)
111 return;
112
113 g_free (info->label);
114 g_free (info->uuid);
115 g_free (info);
116 }
117
118 BDBtrfsDeviceStats* bd_btrfs_device_stats_copy (BDBtrfsDeviceStats *stats) {
119 if (stats == NULL)
120 return NULL;
121
122 BDBtrfsDeviceStats *new_stats = g_new0 (BDBtrfsDeviceStats, 1);
123
124 new_stats->id = stats->id;
125 new_stats->path = g_strdup (stats->path);
126 new_stats->write_io_errs = stats->write_io_errs;
127 new_stats->read_io_errs = stats->read_io_errs;
128 new_stats->flush_io_errs = stats->flush_io_errs;
129 new_stats->corruption_errs = stats->corruption_errs;
130 new_stats->generation_errs = stats->generation_errs;
131
132 return new_stats;
133 }
134
135 void bd_btrfs_device_stats_free (BDBtrfsDeviceStats *stats) {
136 if (stats == NULL)
137 return;
138
139 g_free (stats->path);
140 g_free (stats);
141 }
142
143 static volatile guint avail_deps = 0;
144 static volatile guint avail_module_deps = 0;
145 static GMutex deps_check_lock;
146
147 #define DEPS_BTRFS 0
148 #define DEPS_BTRFS_612 1
149 #define DEPS_BTRFS_MASK (1 << DEPS_BTRFS)
150 #define DEPS_BTRFS_612_MASK (1 << DEPS_BTRFS_612)
151 #define DEPS_LAST 2
152
153 static const UtilDep deps[DEPS_LAST] = {
154 {"btrfs", BTRFS_MIN_VERSION, NULL, "[Bb]trfs.* v([\\d\\.]+)"},
155 {"btrfs", "6.12", NULL, "[Bb]trfs.* v([\\d\\.]+)"},
156 };
157
158 #define MODULE_DEPS_BTRFS 0
159 #define MODULE_DEPS_BTRFS_MASK (1 << MODULE_DEPS_BTRFS)
160 #define MODULE_DEPS_LAST 1
161
162 static const gchar*const module_deps[MODULE_DEPS_LAST] = { "btrfs" };
163
164
165 /**
166 * bd_btrfs_init:
167 *
168 * Initializes the plugin. **This function is called automatically by the
169 * library's initialization functions.**
170 *
171 */
172 gboolean bd_btrfs_init (void) {
173 /* nothing to do here */
174 return TRUE;
175 };
176
177 /**
178 * bd_btrfs_close:
179 *
180 * Cleans up after the plugin. **This function is called automatically by the
181 * library's functions that unload it.**
182 *
183 */
184 void bd_btrfs_close (void) {
185 g_atomic_int_set (&avail_deps, 0);
186 g_atomic_int_set (&avail_module_deps, 0);
187 }
188
189
190
191 /**
192 * bd_btrfs_is_tech_avail:
193 * @tech: the queried tech
194 * @mode: a bit mask of queried modes of operation for @tech
195 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
196 *
197 * Returns: whether the @tech-@mode combination is available -- supported by the
198 * plugin implementation and having all the runtime dependencies available
199 */
200 gboolean bd_btrfs_is_tech_avail (BDBtrfsTech tech, guint64 mode, GError **error) {
201 /* all tech-mode combinations are supported by this implementation of the plugin */
202 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
203 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
204 return FALSE;
205
206 if (tech == BD_BTRFS_TECH_SUBVOL && (mode & BD_BTRFS_TECH_MODE_DELETE_RECURSIVE))
207 return check_deps (&avail_deps, DEPS_BTRFS_612_MASK, deps, DEPS_LAST, &deps_check_lock, error);
208
209 return TRUE;
210 }
211
212 static BDBtrfsDeviceInfo* get_device_info_from_match (GMatchInfo *match_info) {
213 BDBtrfsDeviceInfo *ret = g_new0(BDBtrfsDeviceInfo, 1);
214 gchar *item = NULL;
215 BSSize size = NULL;
216 BSError *error = NULL;
217
218 item = g_match_info_fetch_named (match_info, "id");
219 ret->id = g_ascii_strtoull (item, NULL, 0);
220 g_free (item);
221
222 ret->path = g_match_info_fetch_named (match_info, "path");
223
224 item = g_match_info_fetch_named (match_info, "size");
225 if (item) {
226 size = bs_size_new_from_str (item, &error);
227 if (size) {
228 ret->size = bs_size_get_bytes (size, NULL, &error);
229 bs_size_free (size);
230 }
231 if (error)
232 bd_utils_log_format (BD_UTILS_LOG_WARNING, "%s", error->msg);
233 bs_clear_error (&error);
234 g_free (item);
235 }
236
237 item = g_match_info_fetch_named (match_info, "used");
238 if (item) {
239 size = bs_size_new_from_str (item, &error);
240 if (size) {
241 ret->used = bs_size_get_bytes (size, NULL, &error);
242 bs_size_free (size);
243 }
244 if (error)
245 bd_utils_log_format (BD_UTILS_LOG_WARNING, "%s", error->msg);
246 bs_clear_error (&error);
247 g_free (item);
248 }
249
250 return ret;
251 }
252
253 static BDBtrfsSubvolumeInfo* get_subvolume_info_from_match (GMatchInfo *match_info) {
254 BDBtrfsSubvolumeInfo *ret = g_new0(BDBtrfsSubvolumeInfo, 1);
255 gchar *item = NULL;
256
257 item = g_match_info_fetch_named (match_info, "id");
258 ret->id = g_ascii_strtoull (item, NULL, 0);
259 g_free (item);
260
261 item = g_match_info_fetch_named (match_info, "parent_id");
262 ret->parent_id = g_ascii_strtoull (item, NULL, 0);
263 g_free (item);
264
265 ret->path = g_match_info_fetch_named (match_info, "path");
266
267 return ret;
268 }
269
270 static BDBtrfsFilesystemInfo* get_filesystem_info_from_match (GMatchInfo *match_info) {
271 BDBtrfsFilesystemInfo *ret = g_new0(BDBtrfsFilesystemInfo, 1);
272 gchar *item = NULL;
273 BSSize size = NULL;
274 BSError *error = NULL;
275
276 ret->label = g_match_info_fetch_named (match_info, "label");
277 ret->uuid = g_match_info_fetch_named (match_info, "uuid");
278
279 item = g_match_info_fetch_named (match_info, "num_devices");
280 ret->num_devices = g_ascii_strtoull (item, NULL, 0);
281 g_free (item);
282
283 item = g_match_info_fetch_named (match_info, "used");
284 if (item) {
285 size = bs_size_new_from_str (item, &error);
286 if (size) {
287 ret->used = bs_size_get_bytes (size, NULL, &error);
288 bs_size_free (size);
289 }
290 if (error)
291 bd_utils_log_format (BD_UTILS_LOG_WARNING, "%s", error->msg);
292 bs_clear_error (&error);
293 g_free (item);
294 }
295
296 return ret;
297 }
298
299 /**
300 * bd_btrfs_create_volume:
301 * @devices: (array zero-terminated=1): list of devices to create btrfs volume from
302 * @label: (nullable): label for the volume
303 * @data_level: (nullable): RAID level for the data or %NULL to use the default
304 * @md_level: (nullable): RAID level for the metadata or %NULL to use the default
305 * @extra: (nullable) (array zero-terminated=1): extra options for the volume creation (right now
306 * passed to the 'mkfs.btrfs' utility)
307 * @error: (out) (optional): place to store error (if any)
308 *
309 * Returns: whether the new btrfs volume was created from @devices or not
310 *
311 * See mkfs.btrfs(8) for details about @data_level, @md_level and btrfs in general.
312 *
313 * Tech category: %BD_BTRFS_TECH_MULTI_DEV-%BD_BTRFS_TECH_MODE_CREATE
314 */
315 gboolean bd_btrfs_create_volume (const gchar **devices, const gchar *label, const gchar *data_level, const gchar *md_level, const BDExtraArg **extra, GError **error) {
316 const gchar **device_p = NULL;
317 guint8 num_args = 0;
318 const gchar **argv = NULL;
319 guint8 next_arg = 1;
320 gboolean success = FALSE;
321
322 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
323 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
324 return FALSE;
325
326 if (!devices || (g_strv_length ((gchar **) devices) < 1)) {
327 g_set_error_literal (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_DEVICE, "No devices given");
328 return FALSE;
329 }
330
331 for (device_p = devices; *device_p != NULL; device_p++) {
332 if (access (*device_p, F_OK) != 0) {
333 g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_DEVICE, "Device %s does not exist", *device_p);
334 return FALSE;
335 }
336 num_args++;
337 }
338
339 if (label)
340 num_args += 2;
341 if (data_level)
342 num_args += 2;
343 if (md_level)
344 num_args += 2;
345
346 argv = g_new0 (const gchar*, num_args + 2);
347 argv[0] = "mkfs.btrfs";
348 if (label) {
349 argv[next_arg] = "--label";
350 next_arg++;
351 argv[next_arg] = label;
352 next_arg++;
353 }
354 if (data_level) {
355 argv[next_arg] = "--data";
356 next_arg++;
357 argv[next_arg] = data_level;
358 next_arg++;
359 }
360 if (md_level) {
361 argv[next_arg] = "--metadata";
362 next_arg++;
363 argv[next_arg] = md_level;
364 next_arg++;
365 }
366
367 for (device_p = devices; next_arg <= num_args; device_p++, next_arg++)
368 argv[next_arg] = *device_p;
369 argv[next_arg] = NULL;
370
371 success = bd_utils_exec_and_report_error (argv, extra, error);
372 g_free (argv);
373 return success;
374 }
375
376 /**
377 * bd_btrfs_add_device:
378 * @mountpoint: mountpoint of the btrfs volume to add new device to
379 * @device: a device to add to the btrfs volume
380 * @extra: (nullable) (array zero-terminated=1): extra options for the addition (right now
381 * passed to the 'btrfs' utility)
382 * @error: (out) (optional): place to store error (if any)
383 *
384 * Returns: whether the @device was successfully added to the @mountpoint btrfs volume or not
385 *
386 * Tech category: %BD_BTRFS_TECH_MULTI_DEV-%BD_BTRFS_TECH_MODE_MODIFY
387 */
388 gboolean bd_btrfs_add_device (const gchar *mountpoint, const gchar *device, const BDExtraArg **extra, GError **error) {
389 const gchar *argv[6] = {"btrfs", "device", "add", device, mountpoint, NULL};
390 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
391 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
392 return FALSE;
393
394 return bd_utils_exec_and_report_error (argv, extra, error);
395 }
396
397 /**
398 * bd_btrfs_remove_device:
399 * @mountpoint: mountpoint of the btrfs volume to remove device from
400 * @device: a device to remove from the btrfs volume
401 * @extra: (nullable) (array zero-terminated=1): extra options for the removal (right now
402 * passed to the 'btrfs' utility)
403 * @error: (out) (optional): place to store error (if any)
404 *
405 * Returns: whether the @device was successfully removed from the @mountpoint btrfs volume or not
406 *
407 * Tech category: %BD_BTRFS_TECH_MULTI_DEV-%BD_BTRFS_TECH_MODE_MODIFY
408 */
409 gboolean bd_btrfs_remove_device (const gchar *mountpoint, const gchar *device, const BDExtraArg **extra, GError **error) {
410 const gchar *argv[6] = {"btrfs", "device", "delete", device, mountpoint, NULL};
411 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
412 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
413 return FALSE;
414
415 return bd_utils_exec_and_report_error (argv, extra, error);
416 }
417
418 /**
419 * bd_btrfs_create_subvolume:
420 * @mountpoint: mountpoint of the btrfs volume to create subvolume under
421 * @name: name of the subvolume
422 * @extra: (nullable) (array zero-terminated=1): extra options for the subvolume creation (right now
423 * passed to the 'btrfs' utility)
424 * @error: (out) (optional): place to store error (if any)
425 *
426 * Returns: whether the @mountpoint/@name subvolume was successfully created or not
427 *
428 * Tech category: %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_CREATE
429 */
430 gboolean bd_btrfs_create_subvolume (const gchar *mountpoint, const gchar *name, const BDExtraArg **extra, GError **error) {
431 gchar *path = NULL;
432 gboolean success = FALSE;
433 const gchar *argv[5] = {"btrfs", "subvol", "create", NULL, NULL};
434
435 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
436 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
437 return FALSE;
438
439 if (g_str_has_suffix (mountpoint, "/"))
440 path = g_strdup_printf ("%s%s", mountpoint, name);
441 else
442 path = g_strdup_printf ("%s/%s", mountpoint, name);
443 argv[3] = path;
444
445 success = bd_utils_exec_and_report_error (argv, extra, error);
446 g_free (path);
447
448 return success;
449 }
450
451 /**
452 * bd_btrfs_delete_subvolume:
453 * @mountpoint: mountpoint of the btrfs volume to delete subvolume from
454 * @name: name of the subvolume
455 * @extra: (nullable) (array zero-terminated=1): extra options for the subvolume deletion (right now
456 * passed to the 'btrfs' utility)
457 * @error: (out) (optional): place to store error (if any)
458 *
459 * Returns: whether the @mountpoint/@name subvolume was successfully deleted or not
460 *
461 * Tech category: %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_DELETE
462 */
463 gboolean bd_btrfs_delete_subvolume (const gchar *mountpoint, const gchar *name, const BDExtraArg **extra, GError **error) {
464 return bd_btrfs_delete_subvolume_recursive (mountpoint, name, FALSE, extra, error);
465 }
466
467 /**
468 * bd_btrfs_delete_subvolume_recursive:
469 * @mountpoint: mountpoint of the btrfs volume to delete subvolume from
470 * @name: name of the subvolume
471 * @recursive: whether to delete child subvolumes recursively
472 * @extra: (nullable) (array zero-terminated=1): extra options for the subvolume deletion (right now
473 * passed to the 'btrfs' utility)
474 * @error: (out) (optional): place to store error (if any)
475 *
476 * Returns: whether the @mountpoint/@name subvolume was successfully deleted or not
477 *
478 * If @recursive is %TRUE, all child subvolumes will be deleted before deleting
479 * the subvolume itself. This requires btrfs-progs >= 6.12.
480 *
481 * Tech category: %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_DELETE if @recursive is %FALSE,
482 * %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_DELETE_RECURSIVE if @recursive is %TRUE
483 */
484 gboolean bd_btrfs_delete_subvolume_recursive (const gchar *mountpoint, const gchar *name, gboolean recursive, const BDExtraArg **extra, GError **error) {
485 gchar *path = NULL;
486 gboolean success = FALSE;
487 const gchar *argv[6] = {"btrfs", "subvol", "delete", NULL, NULL, NULL};
488
489 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
490 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
491 return FALSE;
492
493 if (recursive) {
494 if (!check_deps (&avail_deps, DEPS_BTRFS_612_MASK, deps, DEPS_LAST, &deps_check_lock, error))
495 return FALSE;
496 }
497
498 if (g_str_has_suffix (mountpoint, "/"))
499 path = g_strdup_printf ("%s%s", mountpoint, name);
500 else
501 path = g_strdup_printf ("%s/%s", mountpoint, name);
502
503 if (recursive) {
504 argv[3] = "--recursive";
505 argv[4] = path;
506 } else {
507 argv[3] = path;
508 }
509
510 success = bd_utils_exec_and_report_error (argv, extra, error);
511 g_free (path);
512
513 return success;
514 }
515
516 /**
517 * bd_btrfs_get_default_subvolume_id:
518 * @mountpoint: mountpoint of the volume to get the default subvolume ID of
519 * @error: (out) (optional): place to store error (if any)
520 *
521 * Returns: ID of the @mountpoint volume's default subvolume. If 0,
522 * @error may be set to indicate error
523 *
524 * Tech category: %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_QUERY
525 */
526 guint64 bd_btrfs_get_default_subvolume_id (const gchar *mountpoint, GError **error) {
527 GRegex *regex = NULL;
528 GMatchInfo *match_info = NULL;
529 gboolean success = FALSE;
530 gchar *output = NULL;
531 gchar *match = NULL;
532 guint64 ret = 0;
533 const gchar *argv[5] = {"btrfs", "subvol", "get-default", mountpoint, NULL};
534
535 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
536 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
537 return 0;
538
539 regex = g_regex_new ("ID (\\d+) .*", 0, 0, error);
540 if (!regex) {
541 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to create new GRegex");
542 /* error is already populated */
543 return 0;
544 }
545
546 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
547 if (!success) {
548 g_regex_unref (regex);
549 return 0;
550 }
551
552 success = g_regex_match (regex, output, 0, &match_info);
553 if (!success) {
554 g_set_error_literal (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_PARSE, "Failed to parse subvolume's ID");
555 g_regex_unref (regex);
556 g_match_info_free (match_info);
557 g_free (output);
558 return 0;
559 }
560
561 match = g_match_info_fetch (match_info, 1);
562 ret = g_ascii_strtoull (match, NULL, 0);
563
564 g_free (match);
565 g_match_info_free (match_info);
566 g_regex_unref (regex);
567 g_free (output);
568
569 return ret;
570 }
571
572 /**
573 * bd_btrfs_set_default_subvolume:
574 * @mountpoint: mountpoint of the volume to set the default subvolume ID of
575 * @subvol_id: ID of the subvolume to be set as the default subvolume
576 * @extra: (nullable) (array zero-terminated=1): extra options for the setting (right now
577 * passed to the 'btrfs' utility)
578 * @error: (out) (optional): place to store error (if any)
579 *
580 * Returns: whether the @mountpoint volume's default subvolume was correctly set
581 * to @subvol_id or not
582 *
583 * Tech category: %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_MODIFY
584 */
585 gboolean bd_btrfs_set_default_subvolume (const gchar *mountpoint, guint64 subvol_id, const BDExtraArg **extra, GError **error) {
586 const gchar *argv[6] = {"btrfs", "subvol", "set-default", NULL, mountpoint, NULL};
587 gboolean ret = FALSE;
588
589 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
590 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
591 return FALSE;
592
593 argv[3] = g_strdup_printf ("%"G_GUINT64_FORMAT, subvol_id);
594 ret = bd_utils_exec_and_report_error (argv, extra, error);
595 g_free ((gchar *) argv[3]);
596
597 return ret;
598 }
599
600 /**
601 * bd_btrfs_create_snapshot:
602 * @source: path to source subvolume
603 * @dest: path to new snapshot volume
604 * @ro: whether the snapshot should be read-only
605 * @extra: (nullable) (array zero-terminated=1): extra options for the snapshot creation (right now
606 * passed to the 'btrfs' utility)
607 * @error: (out) (optional): place to store error (if any)
608 *
609 * Returns: whether the @dest snapshot of @source was successfully created or not
610 *
611 * Tech category: %BD_BTRFS_TECH_SNAPSHOT-%BD_BTRFS_TECH_MODE_CREATE
612 */
613 gboolean bd_btrfs_create_snapshot (const gchar *source, const gchar *dest, gboolean ro, const BDExtraArg **extra, GError **error) {
614 const gchar *argv[7] = {"btrfs", "subvol", "snapshot", NULL, NULL, NULL, NULL};
615 guint next_arg = 3;
616
617 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
618 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
619 return FALSE;
620
621 if (ro) {
622 argv[next_arg] = "-r";
623 next_arg++;
624 }
625 argv[next_arg] = source;
626 next_arg++;
627 argv[next_arg] = dest;
628
629 return bd_utils_exec_and_report_error (argv, extra, error);
630 }
631
632 /**
633 * bd_btrfs_list_devices:
634 * @device: a device that is part of the queried btrfs volume
635 * @error: (out) (optional): place to store error (if any)
636 *
637 * Returns: (array zero-terminated=1): information about the devices that are part of the btrfs volume
638 * containing @device or %NULL in case of error
639 *
640 * Tech category: %BD_BTRFS_TECH_MULTI_DEV-%BD_BTRFS_TECH_MODE_QUERY
641 */
642 BDBtrfsDeviceInfo** bd_btrfs_list_devices (const gchar *device, GError **error) {
643 const gchar *argv[5] = {"btrfs", "filesystem", "show", device, NULL};
644 gchar *output = NULL;
645 gboolean success = FALSE;
646 gchar **lines = NULL;
647 gchar **line_p = NULL;
648 gchar const * const pattern = "devid[ \\t]+(?P<id>\\d+)[ \\t]+" \
649 "size[ \\t]+(?P<size>\\S+)[ \\t]+" \
650 "used[ \\t]+(?P<used>\\S+)[ \\t]+" \
651 "path[ \\t]+(?P<path>\\S+)\n";
652 GRegex *regex = NULL;
653 GMatchInfo *match_info = NULL;
654 GPtrArray *dev_infos;
655
656 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
657 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
658 return NULL;
659
660 regex = g_regex_new (pattern, G_REGEX_EXTENDED, 0, error);
661 if (!regex) {
662 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to create new GRegex");
663 /* error is already populated */
664 return NULL;
665 }
666
667 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
668 if (!success) {
669 g_regex_unref (regex);
670 /* error is already populated from the previous call */
671 return NULL;
672 }
673
674 lines = g_strsplit (output, "\n", 0);
675 g_free (output);
676
677 dev_infos = g_ptr_array_new ();
678 for (line_p = lines; *line_p; line_p++) {
679 success = g_regex_match (regex, *line_p, 0, &match_info);
680 if (!success) {
681 g_match_info_free (match_info);
682 continue;
683 }
684
685 g_ptr_array_add (dev_infos, get_device_info_from_match (match_info));
686 g_match_info_free (match_info);
687 }
688
689 g_strfreev (lines);
690 g_regex_unref (regex);
691
692 if (dev_infos->len == 0) {
693 g_set_error_literal (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_PARSE, "Failed to parse information about devices");
694 g_ptr_array_free (dev_infos, TRUE);
695 return NULL;
696 }
697
698 g_ptr_array_add (dev_infos, NULL);
699 return (BDBtrfsDeviceInfo **) g_ptr_array_free (dev_infos, FALSE);
700 }
701
702 /**
703 * bd_btrfs_list_subvolumes:
704 * @mountpoint: a mountpoint of the queried btrfs volume
705 * @snapshots_only: whether to list only snapshot subvolumes or not
706 * @error: (out) (optional): place to store error (if any)
707 *
708 * Returns: (array zero-terminated=1): information about the subvolumes that are part of the btrfs volume
709 * mounted at @mountpoint or %NULL in case of error
710 *
711 * The subvolumes are sorted in a way that no child subvolume appears in the
712 * list before its parent (sub)volume.
713 *
714 * Tech category: %BD_BTRFS_TECH_SUBVOL-%BD_BTRFS_TECH_MODE_QUERY
715 */
716 BDBtrfsSubvolumeInfo** bd_btrfs_list_subvolumes (const gchar *mountpoint, gboolean snapshots_only, GError **error) {
717 const gchar *argv[8] = {"btrfs", "subvol", "list", "-a", "-p", NULL, NULL, NULL};
718 gchar *output = NULL;
719 gboolean success = FALSE;
720 gchar **lines = NULL;
721 gchar **line_p = NULL;
722 gchar const * const pattern = "ID\\s+(?P<id>\\d+)\\s+gen\\s+\\d+\\s+(cgen\\s+\\d+\\s+)?" \
723 "parent\\s+(?P<parent_id>\\d+)\\s+top\\s+level\\s+\\d+\\s+" \
724 "(otime\\s+(\\d{4}-\\d{2}-\\d{2}\\s+\\d\\d:\\d\\d:\\d\\d|-)\\s+)?"\
725 "path\\s+(<FS_TREE>/)?(?P<path>.+)";
726 GRegex *regex = NULL;
727 GMatchInfo *match_info = NULL;
728 guint64 i = 0;
729 guint64 y = 0;
730 guint64 next_sorted_idx = 0;
731 GPtrArray *subvol_infos;
732 BDBtrfsSubvolumeInfo* item = NULL;
733 BDBtrfsSubvolumeInfo* swap_item = NULL;
734 BDBtrfsSubvolumeInfo** ret = NULL;
735 GError *l_error = NULL;
736
737 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
738 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
739 return NULL;
740
741 if (snapshots_only) {
742 argv[5] = "-s";
743 argv[6] = mountpoint;
744 } else
745 argv[5] = mountpoint;
746
747 regex = g_regex_new (pattern, G_REGEX_EXTENDED, 0, error);
748 if (!regex) {
749 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to create new GRegex");
750 /* error is already populated */
751 return NULL;
752 }
753
754 success = bd_utils_exec_and_capture_output (argv, NULL, &output, &l_error);
755 if (!success) {
756 g_regex_unref (regex);
757 if (g_error_matches (l_error, BD_UTILS_EXEC_ERROR, BD_UTILS_EXEC_ERROR_NOOUT)) {
758 /* no output -> no subvolumes */
759 g_clear_error (&l_error);
760 return g_new0 (BDBtrfsSubvolumeInfo*, 1);
761 } else {
762 g_propagate_error (error, l_error);
763 return NULL;
764 }
765 }
766
767 lines = g_strsplit (output, "\n", 0);
768 g_free (output);
769
770 subvol_infos = g_ptr_array_new ();
771 for (line_p = lines; *line_p; line_p++) {
772 success = g_regex_match (regex, *line_p, 0, &match_info);
773 if (!success) {
774 g_match_info_free (match_info);
775 continue;
776 }
777
778 g_ptr_array_add (subvol_infos, get_subvolume_info_from_match (match_info));
779 g_match_info_free (match_info);
780 }
781
782 g_strfreev (lines);
783 g_regex_unref (regex);
784
785 if (subvol_infos->len == 0) {
786 g_set_error_literal (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_PARSE, "Failed to parse information about subvolumes");
787 g_ptr_array_free (subvol_infos, TRUE);
788 return NULL;
789 }
790
791 /* now we know how much space to allocate for the result (subvols + NULL) */
792 ret = g_new0 (BDBtrfsSubvolumeInfo*, subvol_infos->len + 1);
793
794 /* we need to sort the subvolumes in a way that no child subvolume appears
795 in the list before its parent (sub)volume */
796
797 /* let's start by moving all top-level (sub)volumes to the beginning */
798 for (i=0; i < subvol_infos->len; i++) {
799 item = (BDBtrfsSubvolumeInfo*) g_ptr_array_index (subvol_infos, i);
800 if (item->parent_id == BD_BTRFS_MAIN_VOLUME_ID)
801 /* top-level (sub)volume */
802 ret[next_sorted_idx++] = item;
803 }
804 /* top-level (sub)volumes are now processed */
805 for (i=0; i < next_sorted_idx; i++)
806 g_ptr_array_remove_fast (subvol_infos, ret[i]);
807
808 /* now sort the rest in a way that we search for an already sorted parent or sibling */
809 for (i=0; i < subvol_infos->len; i++) {
810 item = (BDBtrfsSubvolumeInfo*) g_ptr_array_index (subvol_infos, i);
811 ret[next_sorted_idx] = item;
812 /* move the item towards beginning of the array checking if some parent
813 or sibling has been already processed/sorted before or we reached the
814 top-level (sub)volumes */
815 for (y=next_sorted_idx; (y > 0 && (ret[y-1]->id != item->parent_id) && (ret[y-1]->parent_id != item->parent_id) && (ret[y-1]->parent_id != BD_BTRFS_MAIN_VOLUME_ID)); y--) {
816 swap_item = ret[y-1];
817 ret[y-1] = ret[y];
818 ret[y] = swap_item;
819 }
820 next_sorted_idx++;
821 }
822 ret[next_sorted_idx] = NULL;
823
824 /* now just free the pointer array */
825 g_ptr_array_free (subvol_infos, TRUE);
826
827 return ret;
828 }
829
830 /**
831 * bd_btrfs_filesystem_info:
832 * @device: a device that is part of the queried btrfs volume
833 * @error: (out) (optional): place to store error (if any)
834 *
835 * Returns: information about the @device's volume's filesystem or %NULL in case of error
836 *
837 * Tech category: %BD_BTRFS_TECH_FS-%BD_BTRFS_TECH_MODE_QUERY
838 */
839 BDBtrfsFilesystemInfo* bd_btrfs_filesystem_info (const gchar *device, GError **error) {
840 const gchar *argv[5] = {"btrfs", "filesystem", "show", device, NULL};
841 gchar *output = NULL;
842 gboolean success = FALSE;
843 gchar const * const pattern = "Label:\\s+(none|'(?P<label>.+)')\\s+" \
844 "uuid:\\s+(?P<uuid>\\S+)\\s+" \
845 "Total\\sdevices\\s+(?P<num_devices>\\d+)\\s+" \
846 "FS\\sbytes\\sused\\s+(?P<used>\\S+)";
847 GRegex *regex = NULL;
848 GMatchInfo *match_info = NULL;
849 BDBtrfsFilesystemInfo *ret = NULL;
850
851 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
852 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
853 return NULL;
854
855 regex = g_regex_new (pattern, G_REGEX_EXTENDED, 0, error);
856 if (!regex) {
857 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to create new GRegex");
858 /* error is already populated */
859 return NULL;
860 }
861
862 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
863 if (!success) {
864 /* error is already populated from the call above or just empty
865 output */
866 g_regex_unref (regex);
867 return NULL;
868 }
869
870 success = g_regex_match (regex, output, 0, &match_info);
871 if (!success) {
872 g_regex_unref (regex);
873 g_match_info_free (match_info);
874 g_free (output);
875 return NULL;
876 }
877
878 ret = get_filesystem_info_from_match (match_info);
879 g_match_info_free (match_info);
880 g_regex_unref (regex);
881
882 g_free (output);
883
884 return ret;
885 }
886
887 /**
888 * bd_btrfs_mkfs:
889 * @devices: (array zero-terminated=1): list of devices to create btrfs volume from
890 * @label: (nullable): label for the volume
891 * @data_level: (nullable): RAID level for the data or %NULL to use the default
892 * @md_level: (nullable): RAID level for the metadata or %NULL to use the default
893 * @extra: (nullable) (array zero-terminated=1): extra options for the volume creation (right now
894 * passed to the 'btrfs' utility)
895 * @error: (out) (optional): place to store error (if any)
896 *
897 * Returns: whether the new btrfs volume was created from @devices or not
898 *
899 * See mkfs.btrfs(8) for details about @data_level, @md_level and btrfs in general.
900 *
901 * Tech category: %BD_BTRFS_TECH_FS-%BD_BTRFS_TECH_MODE_CREATE
902 */
903 gboolean bd_btrfs_mkfs (const gchar **devices, const gchar *label, const gchar *data_level, const gchar *md_level, const BDExtraArg **extra, GError **error) {
904 return bd_btrfs_create_volume (devices, label, data_level, md_level, extra, error);
905 }
906
907 /**
908 * bd_btrfs_resize:
909 * @mountpoint: a mountpoint of the to be resized btrfs filesystem
910 * @size: requested new size
911 * @extra: (nullable) (array zero-terminated=1): extra options for the volume resize (right now
912 * passed to the 'btrfs' utility)
913 * @error: (out) (optional): place to store error (if any)
914 *
915 * Returns: whether the @mountpoint filesystem was successfully resized to @size
916 * or not
917 *
918 * Tech category: %BD_BTRFS_TECH_FS-%BD_BTRFS_TECH_MODE_MODIFY
919 */
920 gboolean bd_btrfs_resize (const gchar *mountpoint, guint64 size, const BDExtraArg **extra, GError **error) {
921 const gchar *argv[6] = {"btrfs", "filesystem", "resize", NULL, mountpoint, NULL};
922 gboolean ret = FALSE;
923
924 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
925 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
926 return FALSE;
927
928 argv[3] = g_strdup_printf ("%"G_GUINT64_FORMAT, size);
929 ret = bd_utils_exec_and_report_error (argv, extra, error);
930 g_free ((gchar *) argv[3]);
931
932 return ret;
933 }
934
935 /**
936 * bd_btrfs_check:
937 * @device: a device that is part of the checked btrfs volume
938 * @extra: (nullable) (array zero-terminated=1): extra options for the check (right now
939 * passed to the 'btrfs' utility)
940 * @error: (out) (optional): place to store error (if any)
941 *
942 * Returns: whether the filesystem was successfully checked or not
943 *
944 * Tech category: %BD_BTRFS_TECH_FS-%BD_BTRFS_TECH_MODE_QUERY
945 */
946 gboolean bd_btrfs_check (const gchar *device, const BDExtraArg **extra, GError **error) {
947 const gchar *argv[4] = {"btrfs", "check", device, NULL};
948
949 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
950 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
951 return FALSE;
952
953 return bd_utils_exec_and_report_error (argv, extra, error);
954 }
955
956 /**
957 * bd_btrfs_repair:
958 * @device: a device that is part of the to be repaired btrfs volume
959 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
960 * passed to the 'btrfs' utility)
961 * @error: (out) (optional): place to store error (if any)
962 *
963 * Returns: whether the filesystem was successfully checked and repaired or not
964 *
965 * Tech category: %BD_BTRFS_TECH_FS-%BD_BTRFS_TECH_MODE_MODIFY
966 */
967 gboolean bd_btrfs_repair (const gchar *device, const BDExtraArg **extra, GError **error) {
968 const gchar *argv[5] = {"btrfs", "check", "--repair", device, NULL};
969
970 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
971 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
972 return FALSE;
973
974 return bd_utils_exec_and_report_error (argv, extra, error);
975 }
976
977 /**
978 * bd_btrfs_change_label:
979 * @mountpoint: a mountpoint of the btrfs filesystem to change label of
980 * @label: new label for the filesystem
981 * @error: (out) (optional): place to store error (if any)
982 *
983 * Returns: whether the label of the @mountpoint filesystem was successfully set
984 * to @label or not
985 *
986 * Tech category: %BD_BTRFS_TECH_FS-%BD_BTRFS_TECH_MODE_MODIFY
987 */
988 gboolean bd_btrfs_change_label (const gchar *mountpoint, const gchar *label, GError **error) {
989 const gchar *argv[6] = {"btrfs", "filesystem", "label", mountpoint, label, NULL};
990
991 if (!check_deps (&avail_deps, DEPS_BTRFS_MASK, deps, DEPS_LAST, &deps_check_lock, error) ||
992 !check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
993 return FALSE;
994
995 return bd_utils_exec_and_report_error (argv, NULL, error);
996 }
997
998 /**
999 * bd_btrfs_device_stats:
1000 * @mountpoint: a mountpoint of the queried btrfs volume
1001 * @error: (out) (optional): place to store error (if any)
1002 *
1003 * Returns: (array zero-terminated=1): device stats for each device that is part of the btrfs volume
1004 * mounted at @mountpoint or %NULL in case of error
1005 *
1006 * Tech category: %BD_BTRFS_TECH_MULTI_DEV-%BD_BTRFS_TECH_MODE_QUERY
1007 */
1008 BDBtrfsDeviceStats** bd_btrfs_device_stats (const gchar *mountpoint, GError **error) {
1009 int fd = -1;
1010 struct btrfs_ioctl_fs_info_args fs_info;
1011 struct btrfs_ioctl_dev_info_args dev_info;
1012 struct btrfs_ioctl_get_dev_stats dev_stats;
1013 GPtrArray *stats_array = NULL;
1014 BDBtrfsDeviceStats *stats = NULL;
1015
1016 if (!check_module_deps (&avail_module_deps, MODULE_DEPS_BTRFS_MASK, module_deps, MODULE_DEPS_LAST, &deps_check_lock, error))
1017 return NULL;
1018
1019 fd = open (mountpoint, O_RDONLY);
1020 if (fd < 0) {
1021 g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_DEVICE,
1022 "Failed to open mountpoint %s: %s", mountpoint, g_strerror (errno));
1023 return NULL;
1024 }
1025
1026 memset (&fs_info, 0, sizeof (fs_info));
1027 if (ioctl (fd, BTRFS_IOC_FS_INFO, &fs_info) < 0) {
1028 g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_DEVICE,
1029 "Failed to get filesystem info for %s: %s", mountpoint, g_strerror (errno));
1030 close (fd);
1031 return NULL;
1032 }
1033
1034 stats_array = g_ptr_array_new_with_free_func ((GDestroyNotify) bd_btrfs_device_stats_free);
1035
1036 for (guint64 devid = 1; devid <= fs_info.max_id; devid++) {
1037 memset (&dev_info, 0, sizeof (dev_info));
1038 dev_info.devid = devid;
1039 if (ioctl (fd, BTRFS_IOC_DEV_INFO, &dev_info) < 0) {
1040 if (errno == ENODEV)
1041 continue;
1042 g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_DEVICE,
1043 "Failed to get device info for devid %" G_GUINT64_FORMAT " on %s: %s",
1044 devid, mountpoint, g_strerror (errno));
1045 g_ptr_array_free (stats_array, TRUE);
1046 close (fd);
1047 return NULL;
1048 }
1049
1050 memset (&dev_stats, 0, sizeof (dev_stats));
1051 dev_stats.devid = devid;
1052 dev_stats.nr_items = BTRFS_DEV_STAT_VALUES_MAX;
1053 dev_stats.flags = 0;
1054 if (ioctl (fd, BTRFS_IOC_GET_DEV_STATS, &dev_stats) < 0) {
1055 g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_DEVICE,
1056 "Failed to get device stats for devid %" G_GUINT64_FORMAT " on %s: %s",
1057 devid, mountpoint, g_strerror (errno));
1058 g_ptr_array_free (stats_array, TRUE);
1059 close (fd);
1060 return NULL;
1061 }
1062
1063 stats = g_new0 (BDBtrfsDeviceStats, 1);
1064 stats->id = devid;
1065 stats->path = g_strdup ((const gchar *) dev_info.path);
1066 stats->write_io_errs = dev_stats.values[BTRFS_DEV_STAT_WRITE_ERRS];
1067 stats->read_io_errs = dev_stats.values[BTRFS_DEV_STAT_READ_ERRS];
1068 stats->flush_io_errs = dev_stats.values[BTRFS_DEV_STAT_FLUSH_ERRS];
1069 stats->corruption_errs = dev_stats.values[BTRFS_DEV_STAT_CORRUPTION_ERRS];
1070 stats->generation_errs = dev_stats.values[BTRFS_DEV_STAT_GENERATION_ERRS];
1071
1072 g_ptr_array_add (stats_array, stats);
1073
1074 if (stats_array->len == fs_info.num_devices)
1075 break;
1076 }
1077
1078 close (fd);
1079
1080 if (stats_array->len == 0) {
1081 g_set_error (error, BD_BTRFS_ERROR, BD_BTRFS_ERROR_DEVICE,
1082 "No devices found for %s", mountpoint);
1083 g_ptr_array_free (stats_array, TRUE);
1084 return NULL;
1085 }
1086
1087 g_ptr_array_add (stats_array, NULL);
1088 return (BDBtrfsDeviceStats **) g_ptr_array_free (stats_array, FALSE);
1089 }