Debian Codegraph @

src/plugins/fs/xfs.c

raw
1 /*
2 * Copyright (C) 2017 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 <blockdev/utils.h>
21 #include <check_deps.h>
22 #include <string.h>
23 #include <ctype.h>
24
25 #include "xfs.h"
26 #include "fs.h"
27 #include "common.h"
28
29 static volatile guint avail_deps = 0;
30 static GMutex deps_check_lock;
31
32 G_GNUC_INTERNAL
33 void _fs_xfs_reset_avail_deps (void) {
34 g_atomic_int_set (&avail_deps, 0);
35 }
36
37 #define DEPS_MKFSXFS 0
38 #define DEPS_MKFSXFS_MASK (1 << DEPS_MKFSXFS)
39 #define DEPS_XFS_DB 1
40 #define DEPS_XFS_DB_MASK (1 << DEPS_XFS_DB)
41 #define DEPS_XFS_REPAIR 2
42 #define DEPS_XFS_REPAIR_MASK (1 << DEPS_XFS_REPAIR)
43 #define DEPS_XFS_ADMIN 3
44 #define DEPS_XFS_ADMIN_MASK (1 << DEPS_XFS_ADMIN)
45 #define DEPS_XFS_GROWFS 4
46 #define DEPS_XFS_GROWFS_MASK (1 << DEPS_XFS_GROWFS)
47
48 #define DEPS_LAST 5
49
50 static const UtilDep deps[DEPS_LAST] = {
51 {"mkfs.xfs", NULL, NULL, NULL},
52 {"xfs_db", NULL, NULL, NULL},
53 {"xfs_repair", NULL, NULL, NULL},
54 {"xfs_admin", NULL, NULL, NULL},
55 {"xfs_growfs", NULL, NULL, NULL},
56 };
57
58 static guint32 fs_mode_util[BD_FS_MODE_LAST+1] = {
59 DEPS_MKFSXFS_MASK, /* mkfs */
60 0, /* wipe */
61 DEPS_XFS_DB_MASK, /* check */
62 DEPS_XFS_REPAIR_MASK, /* repair */
63 DEPS_XFS_ADMIN_MASK, /* set-label */
64 DEPS_XFS_ADMIN_MASK, /* query */
65 DEPS_XFS_GROWFS_MASK, /* resize */
66 DEPS_XFS_ADMIN_MASK /* set-uuid */
67 };
68
69
70 #ifdef __clang__
71 #define ZERO_INIT {}
72 #else
73 #define ZERO_INIT {0}
74 #endif
75
76 /**
77 * bd_fs_xfs_is_tech_avail:
78 * @tech: the queried tech
79 * @mode: a bit mask of queried modes of operation (#BDFSTechMode) for @tech
80 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
81 *
82 * Returns: whether the @tech-@mode combination is available -- supported by the
83 * plugin implementation and having all the runtime dependencies available
84 */
85 G_GNUC_INTERNAL gboolean
86 bd_fs_xfs_is_tech_avail (BDFSTech tech G_GNUC_UNUSED, guint64 mode, GError **error) {
87 guint32 required = 0;
88 guint i = 0;
89 for (i = 0; i <= BD_FS_MODE_LAST; i++)
90 if (mode & (1 << i))
91 required |= fs_mode_util[i];
92
93 return check_deps (&avail_deps, required, deps, DEPS_LAST, &deps_check_lock, error);
94 }
95
96 /**
97 * bd_fs_xfs_info_copy: (skip)
98 * @data: (nullable): %BDFSXfsInfo to copy
99 *
100 * Creates a new copy of @data.
101 */
102 BDFSXfsInfo* bd_fs_xfs_info_copy (BDFSXfsInfo *data) {
103 if (data == NULL)
104 return NULL;
105
106 BDFSXfsInfo *ret = g_new0 (BDFSXfsInfo, 1);
107
108 ret->label = g_strdup (data->label);
109 ret->uuid = g_strdup (data->uuid);
110 ret->block_size = data->block_size;
111 ret->block_count = data->block_count;
112
113 return ret;
114 }
115
116 /**
117 * bd_fs_xfs_info_free: (skip)
118 * @data: (nullable): %BDFSXfsInfo to free
119 *
120 * Frees @data.
121 */
122 void bd_fs_xfs_info_free (BDFSXfsInfo *data) {
123 if (data == NULL)
124 return;
125
126 g_free (data->label);
127 g_free (data->uuid);
128 g_free (data);
129 }
130
131 G_GNUC_INTERNAL BDExtraArg **
132 bd_fs_xfs_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
133 GPtrArray *options_array = g_ptr_array_new ();
134 const BDExtraArg **extra_p = NULL;
135 gchar *uuid_option = NULL;
136
137 if (options->label && g_strcmp0 (options->label, "") != 0)
138 g_ptr_array_add (options_array, bd_extra_arg_new ("-L", options->label));
139
140 if (options->uuid && g_strcmp0 (options->uuid, "") != 0) {
141 uuid_option = g_strdup_printf ("uuid=%s", options->uuid);
142 g_ptr_array_add (options_array, bd_extra_arg_new ("-m", uuid_option));
143 g_free (uuid_option);
144 }
145
146 if (options->dry_run)
147 g_ptr_array_add (options_array, bd_extra_arg_new ("-N", ""));
148
149 if (options->no_discard)
150 g_ptr_array_add (options_array, bd_extra_arg_new ("-K", ""));
151
152 if (options->force)
153 g_ptr_array_add (options_array, bd_extra_arg_new ("-f", ""));
154
155 if (extra) {
156 for (extra_p = extra; *extra_p; extra_p++)
157 g_ptr_array_add (options_array, bd_extra_arg_copy ((BDExtraArg *) *extra_p));
158 }
159
160 g_ptr_array_add (options_array, NULL);
161
162 return (BDExtraArg **) g_ptr_array_free (options_array, FALSE);
163 }
164
165
166 /**
167 * bd_fs_xfs_mkfs:
168 * @device: the device to create a new xfs fs on
169 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
170 * passed to the 'mkfs.xfs' utility)
171 * @error: (out) (optional): place to store error (if any)
172 *
173 * Returns: whether a new xfs fs was successfully created on @device or not
174 *
175 * Tech category: %BD_FS_TECH_XFS-%BD_FS_TECH_MODE_MKFS
176 */
177 gboolean bd_fs_xfs_mkfs (const gchar *device, const BDExtraArg **extra, GError **error) {
178 const gchar *args[3] = {"mkfs.xfs", device, NULL};
179
180 if (!check_deps (&avail_deps, DEPS_MKFSXFS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
181 return FALSE;
182
183 return bd_utils_exec_and_report_error (args, extra, error);
184 }
185
186 /**
187 * bd_fs_xfs_check:
188 * @device: the device containing the file system to check
189 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
190 * passed to the 'xfs_repair' utility)
191 * @error: (out) (optional): place to store error (if any)
192 *
193 * Returns: whether an xfs file system on the @device is clean or not
194 *
195 * Note: If the file system is mounted RW, it will always be reported as not
196 * clean!
197 *
198 * Tech category: %BD_FS_TECH_XFS-%BD_FS_TECH_MODE_CHECK
199 */
200 gboolean bd_fs_xfs_check (const gchar *device, const BDExtraArg **extra, GError **error) {
201 const gchar *args[4] = {"xfs_repair", "-n", device, NULL};
202 gboolean ret = FALSE;
203 GError *l_error = NULL;
204
205 if (!check_deps (&avail_deps, DEPS_XFS_REPAIR_MASK, deps, DEPS_LAST, &deps_check_lock, error))
206 return FALSE;
207
208 ret = bd_utils_exec_and_report_error (args, extra, &l_error);
209 if (!ret) {
210 if (l_error && g_error_matches (l_error, BD_UTILS_EXEC_ERROR, BD_UTILS_EXEC_ERROR_FAILED)) {
211 /* non-zero exit status -> the fs is not clean, but not an error */
212 /* TODO: should we check that the device exists and contains an XFS FS beforehand? */
213 g_clear_error (&l_error);
214 } else
215 g_propagate_error (error, l_error);
216 }
217 return ret;
218 }
219
220 /**
221 * bd_fs_xfs_repair:
222 * @device: the device containing the file system to repair
223 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
224 * passed to the 'xfs_repair' utility)
225 * @error: (out) (optional): place to store error (if any)
226 *
227 * Returns: whether an xfs file system on the @device was successfully repaired
228 * (if needed) or not (error is set in that case)
229 *
230 * Tech category: %BD_FS_TECH_XFS-%BD_FS_TECH_MODE_REPAIR
231 */
232 gboolean bd_fs_xfs_repair (const gchar *device, const BDExtraArg **extra, GError **error) {
233 const gchar *args[3] = {"xfs_repair", device, NULL};
234
235 if (!check_deps (&avail_deps, DEPS_XFS_REPAIR_MASK, deps, DEPS_LAST, &deps_check_lock, error))
236 return FALSE;
237
238 return bd_utils_exec_and_report_error (args, extra, error);
239 }
240
241 /**
242 * bd_fs_xfs_set_label:
243 * @device: the device containing the file system to set label for
244 * @label: label to set
245 * @error: (out) (optional): place to store error (if any)
246 *
247 * Returns: whether the label of xfs file system on the @device was
248 * successfully set or not
249 *
250 * Tech category: %BD_FS_TECH_XFS-%BD_FS_TECH_MODE_SET_LABEL
251 */
252 gboolean bd_fs_xfs_set_label (const gchar *device, const gchar *label, GError **error) {
253 const gchar *args[5] = {"xfs_admin", "-L", label, device, NULL};
254 if (!label || (strncmp (label, "", 1) == 0))
255 args[2] = "--";
256
257 if (!check_deps (&avail_deps, DEPS_XFS_ADMIN_MASK, deps, DEPS_LAST, &deps_check_lock, error))
258 return FALSE;
259
260 return bd_utils_exec_and_report_error (args, NULL, error);
261 }
262
263 /**
264 * bd_fs_xfs_check_label:
265 * @label: label to check
266 * @error: (out) (optional): place to store error
267 *
268 * Returns: whether @label is a valid label for the xfs file system or not
269 * (reason is provided in @error)
270 *
271 * Tech category: always available
272 */
273 gboolean bd_fs_xfs_check_label (const gchar *label, GError **error) {
274 size_t len = 0;
275
276 len = strlen (label);
277 if (len > 12) {
278 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
279 "Label for XFS filesystem must be at most 12 characters long.");
280 return FALSE;
281 }
282
283 if (strchr (label, ' ') != NULL) {
284 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
285 "Label for XFS filesystem cannot contain spaces.");
286 return FALSE;
287 }
288
289 return TRUE;
290 }
291
292 /**
293 * bd_fs_xfs_set_uuid:
294 * @device: the device containing the file system to set uuid for
295 * @uuid: (nullable): UUID to set %NULL to generate a new one
296 * UUID can also be one of "nil" and "generate" to clear or
297 * generate a new UUID
298 * @error: (out) (optional): place to store error (if any)
299 *
300 * Returns: whether the UUID of xfs file system on the @device was
301 * successfully set or not
302 *
303 * Tech category: %BD_FS_TECH_XFS-%BD_FS_TECH_MODE_SET_UUID
304 */
305 gboolean bd_fs_xfs_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
306 const gchar *args[5] = {"xfs_admin", "-U", uuid, device, NULL};
307 if (!uuid)
308 args[2] = "generate";
309
310 if (!check_deps (&avail_deps, DEPS_XFS_ADMIN_MASK, deps, DEPS_LAST, &deps_check_lock, error))
311 return FALSE;
312
313 return bd_utils_exec_and_report_error (args, NULL, error);
314 }
315
316 /**
317 * bd_fs_xfs_check_uuid:
318 * @uuid: UUID to check
319 * @error: (out) (optional): place to store error
320 *
321 * Returns: whether @uuid is a valid UUID for the xfs file system or not
322 * (reason is provided in @error)
323 *
324 * Tech category: always available
325 */
326 gboolean bd_fs_xfs_check_uuid (const gchar *uuid, GError **error) {
327 return check_uuid (uuid, error);
328 }
329
330 /**
331 * bd_fs_xfs_get_info:
332 * @device: the device containing the file system to get info for
333 * @error: (out) (optional): place to store error (if any)
334 *
335 * Returns: (transfer full): information about the file system on @device or
336 * %NULL in case of error
337 *
338 * Tech category: %BD_FS_TECH_XFS-%BD_FS_TECH_MODE_QUERY
339 */
340 BDFSXfsInfo* bd_fs_xfs_get_info (const gchar *device, GError **error) {
341 const gchar *args[6] = ZERO_INIT;
342 gboolean success = FALSE;
343 gchar *output = NULL;
344 BDFSXfsInfo *ret = NULL;
345 gchar **lines = NULL;
346 gchar **line_p = NULL;
347 gchar *val_start = NULL;
348 g_autofree gchar* mountpoint = NULL;
349
350 if (!check_deps (&avail_deps, DEPS_XFS_ADMIN_MASK, deps, DEPS_LAST, &deps_check_lock, error))
351 return NULL;
352
353 ret = g_new0 (BDFSXfsInfo, 1);
354
355 success = get_uuid_label (device, &(ret->uuid), &(ret->label), error);
356 if (!success) {
357 /* error is already populated */
358 bd_fs_xfs_info_free (ret);
359 return NULL;
360 }
361
362 /* It is important to use xfs_spaceman for a mounted filesystem
363 since xfs_db might return old information. xfs_info would be
364 able to do the job for us (running xfs_spaceman or xfs_db
365 depending on whether the fs is mounted), but it doesn't pass
366 "-r" to xfs_db, which is important to avoid spurious udev
367 events just for reading information.
368 */
369
370 mountpoint = bd_fs_get_mountpoint (device, NULL);
371 if (mountpoint) {
372 args[0] = "xfs_spaceman";
373 args[1] = "-c";
374 args[2] = "info";
375 args[3] = mountpoint;
376 args[4] = NULL;
377 } else {
378 args[0] = "xfs_db";
379 args[1] = "-r";
380 args[2] = "-c";
381 args[3] = "info";
382 args[4] = device;
383 args[5] = NULL;
384 }
385
386 success = bd_utils_exec_and_capture_output (args, NULL, &output, error);
387 if (!success) {
388 /* error is already populated */
389 bd_fs_xfs_info_free (ret);
390 return NULL;
391 }
392
393 lines = g_strsplit (output, "\n", 0);
394 g_free (output);
395 line_p = lines;
396 /* find the beginning of the (data) section we are interested in */
397 while (line_p && *line_p && !g_str_has_prefix (*line_p, "data"))
398 line_p++;
399 if (!line_p || !(*line_p)) {
400 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse xfs file system information");
401 g_strfreev (lines);
402 bd_fs_xfs_info_free (ret);
403 return NULL;
404 }
405
406 /* extract data from something like this: "data = bsize=4096 blocks=262400, imaxpct=25" */
407 val_start = strchr (*line_p, '=');
408 if (!val_start) {
409 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse xfs file system information");
410 g_strfreev (lines);
411 bd_fs_xfs_info_free (ret);
412 return NULL;
413 }
414 val_start++;
415 while (isspace (*val_start))
416 val_start++;
417 if (g_str_has_prefix (val_start, "bsize")) {
418 val_start = strchr (val_start, '=');
419 if (!val_start) {
420 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse xfs file system information");
421 g_strfreev (lines);
422 bd_fs_xfs_info_free (ret);
423 return NULL;
424 }
425 val_start++;
426 ret->block_size = g_ascii_strtoull (val_start, NULL, 0);
427 } else {
428 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse xfs file system information");
429 g_strfreev (lines);
430 bd_fs_xfs_info_free (ret);
431 return NULL;
432 }
433 while (isdigit (*val_start) || isspace(*val_start))
434 val_start++;
435 if (g_str_has_prefix (val_start, "blocks")) {
436 val_start = strchr (val_start, '=');
437 if (!val_start) {
438 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse xfs file system information");
439 g_strfreev (lines);
440 bd_fs_xfs_info_free (ret);
441 return NULL;
442 }
443 val_start++;
444 ret->block_count = g_ascii_strtoull (val_start, NULL, 0);
445 } else {
446 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse xfs file system information");
447 g_strfreev (lines);
448 bd_fs_xfs_info_free (ret);
449 return NULL;
450 }
451 g_strfreev (lines);
452
453 return ret;
454 }
455
456 /**
457 * bd_fs_xfs_resize:
458 * @mpoint: the mount point of the file system to resize
459 * @new_size: new requested size for the file system *in file system blocks* (see bd_fs_xfs_get_info())
460 * (if 0, the file system is adapted to the underlying block device)
461 * @extra: (nullable) (array zero-terminated=1): extra options for the resize (right now
462 * passed to the 'xfs_growfs' utility)
463 * @error: (out) (optional): place to store error (if any)
464 *
465 * Returns: whether the file system mounted on @mpoint was successfully resized or not
466 *
467 * Tech category: %BD_FS_TECH_XFS-%BD_FS_TECH_MODE_RESIZE
468 */
469 gboolean bd_fs_xfs_resize (const gchar *mpoint, guint64 new_size, const BDExtraArg **extra, GError **error) {
470 const gchar *args[5] = {"xfs_growfs", NULL, NULL, NULL, NULL};
471 gchar *size_str = NULL;
472 gboolean ret = FALSE;
473
474 if (!check_deps (&avail_deps, DEPS_XFS_GROWFS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
475 return FALSE;
476
477 if (new_size != 0) {
478 args[1] = "-D";
479 /* xfs_growfs doesn't understand bytes, just a number of blocks */
480 size_str = g_strdup_printf ("%"G_GUINT64_FORMAT, new_size);
481 args[2] = size_str;
482 args[3] = mpoint;
483 } else
484 args[1] = mpoint;
485
486 ret = bd_utils_exec_and_report_error (args, extra, error);
487
488 g_free (size_str);
489 return ret;
490 }