Debian Codegraph @

src/plugins/fs/nilfs.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 <uuid.h>
23
24 #include "nilfs.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_nilfs_reset_avail_deps (void) {
33 g_atomic_int_set (&avail_deps, 0);
34 }
35
36 #define DEPS_MKFSNILFS2 0
37 #define DEPS_MKFSNILFS2_MASK (1 << DEPS_MKFSNILFS2)
38 #define DEPS_NILFSTUNE 1
39 #define DEPS_NILFSTUNE_MASK (1 << DEPS_NILFSTUNE)
40 #define DEPS_NILFSRESIZE 2
41 #define DEPS_NILFSRESIZE_MASK (1 << DEPS_NILFSRESIZE)
42
43 #define DEPS_LAST 3
44
45 static const UtilDep deps[DEPS_LAST] = {
46 {"mkfs.nilfs2", NULL, NULL, NULL},
47 {"nilfs-tune", NULL, NULL, NULL},
48 {"nilfs-resize", NULL, NULL, NULL},
49 };
50
51 static guint32 fs_mode_util[BD_FS_MODE_LAST+1] = {
52 DEPS_MKFSNILFS2_MASK, /* mkfs */
53 0, /* wipe */
54 0, /* check */
55 0, /* repair */
56 DEPS_NILFSTUNE_MASK, /* set-label */
57 DEPS_NILFSTUNE_MASK, /* query */
58 DEPS_NILFSRESIZE_MASK, /* resize */
59 DEPS_NILFSTUNE_MASK, /* set-uuid */
60 };
61
62
63 #ifdef __clang__
64 #define ZERO_INIT {}
65 #else
66 #define ZERO_INIT {0}
67 #endif
68
69 /**
70 * bd_fs_nilfs2_is_tech_avail:
71 * @tech: the queried tech
72 * @mode: a bit mask of queried modes of operation (#BDFSTechMode) for @tech
73 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
74 *
75 * Returns: whether the @tech-@mode combination is available -- supported by the
76 * plugin implementation and having all the runtime dependencies available
77 */
78 G_GNUC_INTERNAL gboolean
79 bd_fs_nilfs2_is_tech_avail (BDFSTech tech G_GNUC_UNUSED, guint64 mode, GError **error) {
80 guint32 required = 0;
81 guint i = 0;
82
83 if (mode & BD_FS_TECH_MODE_CHECK) {
84 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_TECH_UNAVAIL,
85 "NILFS2 doesn't support filesystem check.");
86 return FALSE;
87 }
88
89 if (mode & BD_FS_TECH_MODE_REPAIR) {
90 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_TECH_UNAVAIL,
91 "NILFS2 doesn't support filesystem repair.");
92 return FALSE;
93 }
94
95 for (i = 0; i <= BD_FS_MODE_LAST; i++)
96 if (mode & (1 << i))
97 required |= fs_mode_util[i];
98
99 return check_deps (&avail_deps, required, deps, DEPS_LAST, &deps_check_lock, error);
100 }
101
102 /**
103 * bd_fs_nilfs2_info_copy: (skip)
104 * @data: (nullable): %BDFSNILFS2Info to copy
105 *
106 * Creates a new copy of @data.
107 */
108 BDFSNILFS2Info* bd_fs_nilfs2_info_copy (BDFSNILFS2Info *data) {
109 if (data == NULL)
110 return NULL;
111
112 BDFSNILFS2Info *ret = g_new0 (BDFSNILFS2Info, 1);
113
114 ret->label = g_strdup (data->label);
115 ret->uuid = g_strdup (data->uuid);
116 ret->size = data->size;
117 ret->block_size = data->block_size;
118 ret->free_blocks = data->free_blocks;
119
120 return ret;
121 }
122
123 /**
124 * bd_fs_nilfs2_info_free: (skip)
125 * @data: (nullable): %BDFSNILFS2Info to free
126 *
127 * Frees @data.
128 */
129 void bd_fs_nilfs2_info_free (BDFSNILFS2Info *data) {
130 if (data == NULL)
131 return;
132
133 g_free (data->label);
134 g_free (data->uuid);
135 g_free (data);
136 }
137
138 G_GNUC_INTERNAL BDExtraArg **
139 bd_fs_nilfs2_mkfs_options (BDFSMkfsOptions *options, const BDExtraArg **extra) {
140 GPtrArray *options_array = g_ptr_array_new ();
141 const BDExtraArg **extra_p = NULL;
142
143 if (options->label && g_strcmp0 (options->label, "") != 0)
144 g_ptr_array_add (options_array, bd_extra_arg_new ("-L", options->label));
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 * bd_fs_nilfs2_mkfs:
167 * @device: the device to create a new nilfs fs on
168 * @extra: (nullable) (array zero-terminated=1): extra options for the creation (right now
169 * passed to the 'mkfs.nilfs2' utility)
170 * @error: (out) (optional): place to store error (if any)
171 *
172 * Returns: whether a new nilfs fs was successfully created on @device or not
173 *
174 * Tech category: %BD_FS_TECH_NILFS2-%BD_FS_TECH_MODE_MKFS
175 */
176 gboolean bd_fs_nilfs2_mkfs (const gchar *device, const BDExtraArg **extra, GError **error) {
177 const gchar *args[4] = {"mkfs.nilfs2", "-q", device, NULL};
178
179 if (!check_deps (&avail_deps, DEPS_MKFSNILFS2_MASK, deps, DEPS_LAST, &deps_check_lock, error))
180 return FALSE;
181
182 return bd_utils_exec_and_report_error (args, extra, error);
183 }
184
185 /**
186 * bd_fs_nilfs2_set_label:
187 * @device: the device containing the file system to set label for
188 * @label: label to set
189 * @error: (out) (optional): place to store error (if any)
190 *
191 * Returns: whether the label of nilfs file system on the @device was
192 * successfully set or not
193 *
194 * Tech category: %BD_FS_TECH_NILFS2-%BD_FS_TECH_MODE_SET_LABEL
195 */
196 gboolean bd_fs_nilfs2_set_label (const gchar *device, const gchar *label, GError **error) {
197 const gchar *args[5] = {"nilfs-tune", "-L", label, device, NULL};
198
199 if (!check_deps (&avail_deps, DEPS_NILFSTUNE_MASK, deps, DEPS_LAST, &deps_check_lock, error))
200 return FALSE;
201
202 return bd_utils_exec_and_report_error (args, NULL, error);
203 }
204
205 /**
206 * bd_fs_nilfs2_check_label:
207 * @label: label to check
208 * @error: (out) (optional): place to store error
209 *
210 * Returns: whether @label is a valid label for the nilfs2 file system or not
211 * (reason is provided in @error)
212 *
213 * Tech category: always available
214 */
215 gboolean bd_fs_nilfs2_check_label (const gchar *label, GError **error) {
216 if (strlen (label) > 80) {
217 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_LABEL_INVALID,
218 "Label for nilfs2 filesystem must be at most 80 characters long.");
219 return FALSE;
220 }
221
222 return TRUE;
223 }
224
225 /**
226 * bd_fs_nilfs2_set_uuid:
227 * @device: the device containing the file system to set UUID for
228 * @uuid: (nullable): UUID to set or %NULL to generate a new one
229 * @error: (out) (optional): place to store error (if any)
230 *
231 * Returns: whether the uuid of nilfs file system on the @device was
232 * successfully set or not
233 *
234 * Tech category: %BD_FS_TECH_NILFS2-%BD_FS_TECH_MODE_SET_UUID
235 */
236 gboolean bd_fs_nilfs2_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
237 const gchar *args[5] = {"nilfs-tune", "-U", uuid, device, NULL};
238 uuid_t uu;
239 gchar uuidbuf[37] = {0};
240
241 if (!uuid) {
242 uuid_generate (uu);
243 uuid_unparse (uu, uuidbuf);
244 args[2] = uuidbuf;
245 }
246
247 if (!check_deps (&avail_deps, DEPS_NILFSTUNE_MASK, deps, DEPS_LAST, &deps_check_lock, error))
248 return FALSE;
249
250 return bd_utils_exec_and_report_error (args, NULL, error);
251 }
252
253 /**
254 * bd_fs_nilfs2_check_uuid:
255 * @uuid: UUID to check
256 * @error: (out) (optional): place to store error
257 *
258 * Returns: whether @uuid is a valid UUID for the nilfs file system or not
259 * (reason is provided in @error)
260 *
261 * Tech category: always available
262 */
263 gboolean bd_fs_nilfs2_check_uuid (const gchar *uuid, GError **error) {
264 return check_uuid (uuid, error);
265 }
266
267 /**
268 * bd_fs_nilfs2_get_info:
269 * @device: the device containing the file system to get info for
270 * @error: (out) (optional): place to store error (if any)
271 *
272 * Returns: (transfer full): information about the file system on @device or
273 * %NULL in case of error
274 *
275 * Tech category: %BD_FS_TECH_NILFS2-%BD_FS_TECH_MODE_QUERY
276 */
277 BDFSNILFS2Info* bd_fs_nilfs2_get_info (const gchar *device, GError **error) {
278 const gchar *args[4] = {"nilfs-tune", "-l", device, NULL};
279 gboolean success = FALSE;
280 gchar *output = NULL;
281 BDFSNILFS2Info *ret = NULL;
282 gchar **lines = NULL;
283 gchar **line_p = NULL;
284 gchar *val_start = NULL;
285
286 if (!check_deps (&avail_deps, DEPS_NILFSTUNE_MASK, deps, DEPS_LAST, &deps_check_lock, error))
287 return NULL;
288
289 ret = g_new0 (BDFSNILFS2Info, 1);
290
291 success = get_uuid_label (device, &(ret->uuid), &(ret->label), error);
292 if (!success) {
293 /* error is already populated */
294 bd_fs_nilfs2_info_free (ret);
295 return NULL;
296 }
297
298 success = bd_utils_exec_and_capture_output (args, NULL, &output, error);
299 if (!success) {
300 /* error is already populated */
301 bd_fs_nilfs2_info_free (ret);
302 return NULL;
303 }
304
305 lines = g_strsplit (output, "\n", 0);
306 g_free (output);
307 line_p = lines;
308
309 while (line_p && *line_p && !g_str_has_prefix (*line_p, "Block size:"))
310 line_p++;
311 if (!line_p || !(*line_p)) {
312 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse NILFS2 file system information");
313 g_strfreev (lines);
314 bd_fs_nilfs2_info_free (ret);
315 return NULL;
316 }
317
318 /* * extract data from something like this: "Block size: 4096" */
319 val_start = strchr (*line_p, ':');
320 val_start++;
321 ret->block_size = g_ascii_strtoull (val_start, NULL, 0);
322
323 line_p = lines;
324 while (line_p && *line_p && !g_str_has_prefix (*line_p, "Device size"))
325 line_p++;
326 if (!line_p || !(*line_p)) {
327 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse NILFS2 file system information");
328 g_strfreev (lines);
329 bd_fs_nilfs2_info_free (ret);
330 return NULL;
331 }
332
333 /* extract data from something like this: "Device size: 167772160" */
334 val_start = strchr (*line_p, ':');
335 val_start++;
336 ret->size = g_ascii_strtoull (val_start, NULL, 0);
337
338 line_p = lines;
339 while (line_p && *line_p && !g_str_has_prefix (*line_p, "Free blocks count"))
340 line_p++;
341 if (!line_p || !(*line_p)) {
342 g_set_error_literal (error, BD_FS_ERROR, BD_FS_ERROR_PARSE, "Failed to parse NILFS2 file system information");
343 g_strfreev (lines);
344 bd_fs_nilfs2_info_free (ret);
345 return NULL;
346 }
347
348 /* extract data from something like this: "Free blocks count: 389120" */
349 val_start = strchr (*line_p, ':');
350 val_start++;
351 ret->free_blocks = g_ascii_strtoull (val_start, NULL, 0);
352
353 g_strfreev (lines);
354
355 return ret;
356 }
357
358 /**
359 * bd_fs_nilfs2_resize:
360 * @device: the device the file system of which to resize
361 * @new_size: new requested size for the file system (if 0, the file system is
362 * adapted to the underlying block device)
363 * @error: (out) (optional): place to store error (if any)
364 *
365 * Returns: whether the file system on @device was successfully resized or not
366 *
367 * Note: Filesystem must be mounted for the resize operation.
368 *
369 * Tech category: %BD_FS_TECH_NILFS2-%BD_FS_TECH_MODE_RESIZE
370 */
371 gboolean bd_fs_nilfs2_resize (const gchar *device, guint64 new_size, GError **error) {
372 const gchar *args[5] = {"nilfs-resize", "-y", device, NULL, NULL};
373 gboolean ret = FALSE;
374
375 if (!check_deps (&avail_deps, DEPS_NILFSRESIZE_MASK, deps, DEPS_LAST, &deps_check_lock, error))
376 return FALSE;
377
378 if (new_size != 0)
379 args[3] = g_strdup_printf ("%"G_GUINT64_FORMAT, new_size);
380
381 ret = bd_utils_exec_and_report_error (args, NULL, error);
382
383 g_free ((gchar *) args[3]);
384 return ret;
385 }