Debian Codegraph @

src/plugins/fs/f2fs.c

raw
1 /*
2 * Copyright (C) 2019 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
23 #include "f2fs.h"
24 #include "fs.h"
25 #include "common.h"
26
27 static volatile guint avail_deps = 0;
28 static volatile guint avail_shrink_deps = 0;
29 static GMutex deps_check_lock;
30
31 G_GNUC_INTERNAL
32 void _fs_f2fs_reset_avail_deps (void) {
33 g_atomic_int_set (&avail_deps, 0);
34 g_atomic_int_set (&avail_shrink_deps, 0);
35 }
36
37 #define DEPS_MKFSF2FS 0
38 #define DEPS_MKFSF2FS_MASK (1 << DEPS_MKFSF2FS)
39 #define DEPS_CHECKF2FS 1
40 #define DEPS_CHECKF2FS_MASK (1 << DEPS_CHECKF2FS)
41 #define DEPS_FSCKF2FS 2
42 #define DEPS_FSCKF2FS_MASK (1 << DEPS_FSCKF2FS)
43 #define DEPS_DUMPF2FS 3
44 #define DEPS_DUMPF2FS_MASK (1 << DEPS_DUMPF2FS)
45 #define DEPS_RESIZEF2FS 4
46 #define DEPS_RESIZEF2FS_MASK (1 << DEPS_RESIZEF2FS)
47
48 #define DEPS_LAST 5
49
50 static const UtilDep deps[DEPS_LAST] = {
51 {"mkfs.f2fs", NULL, NULL, NULL},
52 {"fsck.f2fs", "1.11.0", "-V", "fsck.f2fs\\s+([\\d\\.]+).+"},
53 {"fsck.f2fs", NULL, NULL, NULL},
54 {"dump.f2fs", NULL, NULL, NULL},
55 {"resize.f2fs", NULL, NULL, NULL}
56 };
57
58 /* shrinking needs newer version of f2fs */
59 #define SHRINK_DEPS_RESIZEF2FS 0
60 #define SHRINK_DEPS_RESIZEF2FS_MASK (1 << SHRINK_DEPS_RESIZEF2FS)
61
62 #define SHRINK_DEPS_LAST 1
63
64 static const UtilDep shrink_deps[SHRINK_DEPS_LAST] = {
65 {"resize.f2fs", "1.12.0", "-V", "resize.f2fs\\s+([\\d\\.]+).+"}
66 };
67
68 static guint32 fs_mode_util[BD_FS_MODE_LAST+1] = {
69 DEPS_MKFSF2FS_MASK, /* mkfs */
70 0, /* wipe */
71 DEPS_CHECKF2FS_MASK, /* check */
72 DEPS_FSCKF2FS_MASK, /* repair */
73 0, /* set-label */
74 DEPS_DUMPF2FS_MASK, /* query */
75 DEPS_RESIZEF2FS_MASK, /* resize */
76 0 /* set-uuid */
77 };
78
79
80 /* option to get version was added in 1.11.0 so we need to cover situation
81 where the version is too old to check the version */
82 static gboolean can_check_f2fs_version (UtilDep dep, GError **error) {
83 gboolean available = FALSE;
84 GError *loc_error = NULL;
85
86 available = bd_utils_check_util_version (dep.name, dep.version,
87 dep.ver_arg, dep.ver_regexp,
88 &loc_error);
89 if (!available) {
90 if (g_error_matches (loc_error, BD_UTILS_EXEC_ERROR, BD_UTILS_EXEC_ERROR_UTIL_UNKNOWN_VER)) {
91 /* assuming version of f2fs is too low to check version of f2fs */
92 g_clear_error (&loc_error);
93 g_set_error (error, BD_UTILS_EXEC_ERROR, BD_UTILS_EXEC_ERROR_UTIL_LOW_VER,
94 "Too low version of %s. At least %s required.",
95 dep.name, dep.version);
96 return FALSE;
97 }
98 } else {
99 /* just ignore other errors (e.g. version was detected but is still
100 too low) -- check_deps call below will cover this and create a
101 better error message for these cases */
102 g_clear_error (&loc_error);
103 }
104
105 return TRUE;
106 }
107
108 /**
109 * bd_fs_f2fs_is_tech_avail:
110 * @tech: the queried tech
111 * @mode: a bit mask of queried modes of operation (#BDFSTechMode) for @tech
112 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
113 *
114 * Returns: whether the @tech-@mode combination is available -- supported by the
115 * plugin implementation and having all the runtime dependencies available
116 */
117 G_GNUC_INTERNAL gboolean
118 bd_fs_f2fs_is_tech_avail (BDFSTech tech G_GNUC_UNUSED, guint64 mode, GError **error) {
119 guint32 required = 0;
120 guint i = 0;
121
122 if (mode & BD_FS_TECH_MODE_SET_LABEL) {
123 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_TECH_UNAVAIL,
124 "F2FS doesn't support setting label for an existing device.");
125 return FALSE;
126 }
127
128 if (mode & BD_FS_TECH_MODE_SET_UUID) {
129 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_TECH_UNAVAIL,
130 "F2FS doesn't support setting UUID for an existing device.");
131 return FALSE;
132 }
133
134 if (mode & BD_FS_TECH_MODE_CHECK) {
135 if (!can_check_f2fs_version (deps[DEPS_CHECKF2FS], error))
136 return FALSE;
137 }
138
139 for (i = 0; i <= BD_FS_MODE_LAST; i++)
140 if (mode & (1 << i))
141 required |= fs_mode_util[i];
142
143 return check_deps (&avail_deps, required, deps, DEPS_LAST, &deps_check_lock, error);
144 }
145
146 /**
147 * bd_fs_f2fs_info_copy: (skip)
148 * @data: (nullable): %BDFSF2FSInfo to copy
149 *
150 * Creates a new copy of @data.
151 */
152 BDFSF2FSInfo* bd_fs_f2fs_info_copy (BDFSF2FSInfo *data) {
153 if (data == NULL)
154 return NULL;
155
156 BDFSF2FSInfo *ret = g_new0 (BDFSF2FSInfo, 1);
157
158 ret->label = g_strdup (data->label);
159 ret->uuid = g_strdup (data->uuid);
160 ret->sector_size = data->sector_size;
161 ret->sector_count = data->sector_count;
162 ret->features = data->features;
163
164 return ret;
165 }
166
167 /**
168 * bd_fs_f2fs_info_free: (skip)
169 * @data: (nullable): %BDFSF2FSInfo to free
170 *
171 * Frees @data.
172 */
173 void bd_fs_f2fs_info_free (BDFSF2FSInfo *data) {
174 if (data == NULL)
175 return;
176
177 g_free (data->label);
178 g_free (data->uuid);
179 g_free (data);
180 }
181
182 G_GNUC_INTERNAL BDExtraArg **
183 bd_fs_f2fs_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
184 GPtrArray *options_array = g_ptr_array_new ();
185 const BDExtraArg **extra_p = NULL;
186
187 if (options->label && g_strcmp0 (options->label, "") != 0)
188 g_ptr_array_add (options_array, bd_extra_arg_new ("-l", options->label));
189
190 if (options->no_discard)
191 g_ptr_array_add (options_array, bd_extra_arg_new ("-t", "nodiscard"));
192
193 if (options->force)
194 g_ptr_array_add (options_array, bd_extra_arg_new ("-f", ""));
195
196 if (extra) {
197 for (extra_p = extra; *extra_p; extra_p++)
198 g_ptr_array_add (options_array, bd_extra_arg_copy ((BDExtraArg *) *extra_p));
199 }
200
201 g_ptr_array_add (options_array, NULL);
202
203 return (BDExtraArg **) g_ptr_array_free (options_array, FALSE);
204 }
205
206
207 /**
208 * bd_fs_f2fs_mkfs:
209 * @device: the device to create a new f2fs fs on
210 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
211 * passed to the 'mkfs.f2fs' utility)
212 * @error: (out) (optional): place to store error (if any)
213 *
214 * Returns: whether a new f2fs fs was successfully created on @device or not
215 *
216 * Tech category: %BD_FS_TECH_F2FS-%BD_FS_TECH_MODE_MKFS
217 */
218 gboolean bd_fs_f2fs_mkfs (const gchar *device, const BDExtraArg **extra, GError **error) {
219 const gchar *args[3] = {"mkfs.f2fs", device, NULL};
220
221 if (!check_deps (&avail_deps, DEPS_MKFSF2FS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
222 return FALSE;
223
224 return bd_utils_exec_and_report_error (args, extra, error);
225 }
226
227 /**
228 * bd_fs_f2fs_check:
229 * @device: the device containing the file system to check
230 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
231 * passed to the 'fsck.f2fs' utility)
232 * @error: (out) (optional): place to store error (if any)
233 *
234 * Returns: whether an f2fs file system on the @device is clean or not
235 *
236 * Tech category: %BD_FS_TECH_F2FS-%BD_FS_TECH_MODE_CHECK
237 */
238 gboolean bd_fs_f2fs_check (const gchar *device, const BDExtraArg **extra, GError **error) {
239 const gchar *args[4] = {"fsck.f2fs", "--dry-run", device, NULL};
240 gint status = 0;
241 gboolean ret = FALSE;
242
243 if (!bd_fs_f2fs_is_tech_avail (BD_FS_TECH_F2FS, BD_FS_TECH_MODE_CHECK, error))
244 return FALSE;
245
246 ret = bd_utils_exec_and_report_status_error (args, extra, &status, error);
247 if (!ret && (status == 255)) {
248 /* no error should be reported for exit code 255 -- there are errors on the filesystem */
249 g_clear_error (error);
250 }
251 return ret;
252 }
253
254 /**
255 * bd_fs_f2fs_repair:
256 * @device: the device containing the file system to repair
257 * @extra: (nullable) (array zero-terminated=1): extra options for the repair (right now
258 * passed to the 'fsck.f2fs' utility)
259 * @error: (out) (optional): place to store error (if any)
260 *
261 * Returns: whether an f2fs file system on the @device was successfully repaired
262 * (if needed) or not (error is set in that case)
263 *
264 * Tech category: %BD_FS_TECH_F2FS-%BD_FS_TECH_MODE_REPAIR
265 */
266 gboolean bd_fs_f2fs_repair (const gchar *device, const BDExtraArg **extra, GError **error) {
267 const gchar *args[4] = {"fsck.f2fs", "-a", device, NULL};
268
269 if (!check_deps (&avail_deps, DEPS_FSCKF2FS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
270 return FALSE;
271
272 return bd_utils_exec_and_report_error (args, extra, error);
273 }
274
275 /**
276 * bd_fs_f2fs_get_info:
277 * @device: the device containing the file system to get info for
278 * @error: (out) (optional): place to store error (if any)
279 *
280 * Returns: (transfer full): information about the file system on @device or
281 * %NULL in case of error
282 *
283 * Tech category: %BD_FS_TECH_F2FS-%BD_FS_TECH_MODE_QUERY
284 */
285 BDFSF2FSInfo* bd_fs_f2fs_get_info (const gchar *device, GError **error) {
286 const gchar *argv[3] = {"dump.f2fs", device, NULL};
287 gchar *output = NULL;
288 gboolean success = FALSE;
289 BDFSF2FSInfo*ret = NULL;
290 gchar **lines = NULL;
291 gchar **line_p = NULL;
292 gchar *val_start = NULL;
293
294 if (!check_deps (&avail_deps, DEPS_DUMPF2FS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
295 return NULL;
296
297 success = bd_utils_exec_and_capture_output (argv, NULL, &output, error);
298 if (!success) {
299 /* error is already populated from the call above or just empty
300 output */
301 return NULL;
302 }
303
304 ret = g_new0 (BDFSF2FSInfo, 1);
305
306 success = get_uuid_label (device, &(ret->uuid), &(ret->label), error);
307 if (!success) {
308 /* error is already populated */
309 bd_fs_f2fs_info_free (ret);
310 g_free (output);
311 return NULL;
312 }
313
314 lines = g_strsplit (output, "\n", 0);
315 g_free (output);
316 line_p = lines;
317
318 while (line_p && *line_p && !g_str_has_prefix (*line_p, "Info: sector size"))
319 line_p++;
320 if (!line_p || !(*line_p)) {
321 /* Sector size is not printed with dump.f2fs 1.15 */
322 ret->sector_size = 0;
323 } else {
324 /* extract data from something like this: "Info: sector size = 4096" */
325 val_start = strchr (*line_p, '=');
326 val_start++;
327 ret->sector_size = g_ascii_strtoull (val_start, NULL, 0);
328 }
329
330 line_p = lines;
331 while (line_p && *line_p && !g_str_has_prefix (*line_p, "Info: total FS sectors"))
332 line_p++;
333 if (!line_p || !(*line_p)) {
334 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse F2FS file system information");
335 g_strfreev (lines);
336 bd_fs_f2fs_info_free (ret);
337 return NULL;
338 }
339
340 /* extract data from something like this: "Info: total sectors = 3932160 (15360 MB)" */
341 val_start = strchr (*line_p, '=');
342 val_start++;
343 ret->sector_count = g_ascii_strtoull (val_start, NULL, 0);
344
345 line_p = lines;
346 while (line_p && *line_p && !g_str_has_prefix (*line_p, "Info: superblock features"))
347 line_p++;
348 if (!line_p || !(*line_p)) {
349 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse F2FS file system information");
350 g_strfreev (lines);
351 bd_fs_f2fs_info_free (ret);
352 return NULL;
353 }
354
355 /* extract data from something like this: "Info: superblock features = 0" */
356 val_start = strchr (*line_p, '=');
357 val_start++;
358 ret->features = g_ascii_strtoull (val_start, NULL, 16);
359
360 g_strfreev (lines);
361 return ret;
362 }
363
364 /**
365 * bd_fs_f2fs_resize:
366 * @device: the device containing the file system to resize
367 * @new_size: new requested size for the file system *in file system sectors* (see bd_fs_f2fs_get_info())
368 * (if 0, the file system is adapted to the underlying block device)
369 * @safe: whether to perform safe resize or not (does not resize metadata)
370 * @extra: (nullable) (array zero-terminated=1): extra options for the resize (right now
371 * passed to the 'resize.f2fs' utility)
372 * @error: (out) (optional): place to store error (if any)
373 *
374 * Returns: whether the file system on @device was successfully resized or not
375 *
376 * Tech category: %BD_FS_TECH_F2FS-%BD_FS_TECH_MODE_RESIZE
377 */
378 gboolean bd_fs_f2fs_resize (const gchar *device, guint64 new_size, gboolean safe, const BDExtraArg **extra, GError **error) {
379 const gchar *args[6] = {"resize.f2fs", NULL, NULL, NULL, NULL, NULL};
380 gchar *size_str = NULL;
381 gboolean ret = FALSE;
382 guint next_arg = 1;
383 BDFSF2FSInfo *info = NULL;
384
385 if (!check_deps (&avail_deps, DEPS_RESIZEF2FS_MASK, deps, DEPS_LAST, &deps_check_lock, error))
386 return FALSE;
387
388 if (safe) {
389 if (!can_check_f2fs_version (shrink_deps[SHRINK_DEPS_RESIZEF2FS], error) ||
390 !check_deps (&avail_shrink_deps, SHRINK_DEPS_RESIZEF2FS_MASK, shrink_deps, SHRINK_DEPS_LAST, &deps_check_lock, error)) {
391 /* f2fs version is too low to either even check its version or
392 to perform safe resize (shrink) */
393 g_prefix_error (error, "Can't perform safe resize: ");
394 return FALSE;
395 }
396 }
397
398 info = bd_fs_f2fs_get_info (device, error);
399 if (!info) {
400 /* error is already populated */
401 return FALSE;
402 }
403
404 if (new_size != 0 && new_size < info->sector_count && !safe) {
405 /* resize.f2fs prints error and returns 0 in this case */
406 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_INVAL,
407 "F2FS filesystem doesn't support shrinking without using the 'safe' option");
408 bd_fs_f2fs_info_free (info);
409 return FALSE;
410 }
411 bd_fs_f2fs_info_free (info);
412
413 if (safe) {
414 args[next_arg++] = "-s";
415 }
416
417 if (new_size != 0) {
418 args[next_arg++] = "-t";
419 size_str = g_strdup_printf ("%"G_GUINT64_FORMAT, new_size);
420 args[next_arg++] = size_str;
421 args[next_arg++] = device;
422 } else
423 args[next_arg++] = device;
424
425 ret = bd_utils_exec_and_report_error (args, extra, error);
426
427 g_free (size_str);
428 return ret;
429 }
430
431 /**
432 * bd_fs_f2fs_check_label:
433 * @label: label to check
434 * @error: (out) (optional): place to store error
435 *
436 * Returns: whether @label is a valid label for the f2fs file system or not
437 * (reason is provided in @error)
438 *
439 * Tech category: always available
440 */
441 gboolean bd_fs_f2fs_check_label (const gchar *label, GError **error) {
442 size_t len = 0;
443
444 len = strlen (label);
445 if (len > 512) {
446 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
447 "Label for F2FS filesystem must be at most 512 characters long.");
448 return FALSE;
449 }
450
451 return TRUE;
452 }